Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HBITMAP to BITMAP with GetObject fails

358 views
Skip to first unread message

myprasanna

unread,
Sep 1, 2009, 10:32:01 PM9/1/09
to

Hi folks,

I'm trying to capture screen and get it's BITMAP structure.
GetObject returns 0, indicating an error.
But GetLastError() is returning 0 as well.
This does not confirm with the API:
http://msdn.microsoft.com/en-us/library/ms929450.aspx

Any help is really appreciated.
Code:

void ScreenSnap::TakeScreenShot()
{
HWND hdesktopWnd = ::GetDesktopWindow();
VERIFY(hdesktopWnd);

RECT rectDesktop;
VERIFY(::GetWindowRect(hdesktopWnd, &rectDesktop));

HDC hDesktopDC = ::GetDC(hdesktopWnd);
VERIFY(hDesktopDC);

HBITMAP hDesktopBitmap = (HBITMAP)::GetCurrentObject(hDesktopDC, OBJ_BITMAP);
VERIFY(hDesktopBitmap);

BITMAP desktopBitmap;
if(!GetObject(hDesktopBitmap, sizeof(BITMAP), &desktopBitmap))
{
DWORD err = ::GetLastError(); // err value is 0 !

Richard Russell

unread,
Sep 2, 2009, 4:41:08 AM9/2/09
to
On Sep 2, 3:32 am, myprasanna <myprasa...@discussions.microsoft.com>
wrote:

> This does not confirm with the API:
> http://msdn.microsoft.com/en-us/library/ms929450.aspx

That link is for Windows CE. The docs for the regular Win32 version
of GetObject are here:

http://msdn.microsoft.com/en-us/library/dd144904.aspx

> GetObject returns 0, indicating an error.
> But GetLastError() is returning 0 as well.

The Win32 page doesn't mention anything about calling GetLastError, so
arguably the behavior you observed is in accordance with the API.

Richard.
http://www.rtrussell.co.uk/
To reply by email change 'news' to my forename.

Prasanna

unread,
Sep 2, 2009, 7:18:06 AM9/2/09
to
Thanks a lot for your mail Richard.
Oh, I use Win CE docs a lot then I guess. I'll be careful from now on.
- Thanks
Ok, but now that my function fails, and the above code doesnt seem all
that illegitimate, how do I figure out, what caused an error.
I changed the code to select a bitmap I create using CreateBitmap and
it works fine.
The only difference is, I make a memDC and make a
CreateCompatibleBitmap;
The GetObject then works.

I usually tend to get stuck up, and there's nothing in docs that can
help me.
How do you usually approach such deadlocks?

On Sep 2, 1:41 am, Richard Russell <n...@rtrussell.co.uk> wrote:
> On Sep 2, 3:32 am, myprasanna <myprasa...@discussions.microsoft.com>
> wrote:
>
> > This does not confirm with the API:
> >http://msdn.microsoft.com/en-us/library/ms929450.aspx
>
> That link is for Windows CE.  The docs for the regular Win32 version
> of GetObject are here:
>
> http://msdn.microsoft.com/en-us/library/dd144904.aspx
>
> > GetObject returns 0, indicating an error.
> > But GetLastError() is returning 0 as well.
>
> The Win32 page doesn't mention anything about calling GetLastError, so
> arguably the behavior you observed is in accordance with the API.
>

> Richard.http://www.rtrussell.co.uk/

Remy Lebeau

unread,
Sep 2, 2009, 1:58:21 PM9/2/09
to

"Richard Russell" <ne...@rtrussell.co.uk> wrote in message
news:e6605a05-26c8-43b6...@c37g2000yqi.googlegroups.com...

> The Win32 page doesn't mention anything about calling GetLastError

It used to. For instance, in the Win32 .hlp file that shipped with BCB6:

"If the function fails, the return value is zero. To get extended error
information, call GetLastError."

--
Remy Lebeau (TeamB)


Remy Lebeau

unread,
Sep 2, 2009, 2:03:05 PM9/2/09
to

"myprasanna" <mypra...@discussions.microsoft.com> wrote in message
news:743E2A21-CD13-4DA4...@microsoft.com...

> BITMAP desktopBitmap;
> if(!GetObject(hDesktopBitmap, sizeof(BITMAP), &desktopBitmap))
> {

Does GetObject() still return 0 when you pass NULL as the last parameter?
If so, then hDesktopBitmap likely does not actually have a valid bitmap
selected into it at that time, even though GetCurrentObject() is apparently
returning one.

Instead of using GetCurrentObject() and GetObject(), you could alternatively
use CreateCompatibleDc(), CreateCompatibleBitmap(), SelectObject(), and
BitBlt() to copy the contents of hDesktopDC to your own bitmap instead.

--
Remy Lebeau (TeamB)


Prasanna

unread,
Sep 3, 2009, 11:58:34 AM9/3/09
to
Can we have a DC with no bitmap? If yes, when will it happen?
Where are it's contents stored..

On Sep 2, 11:03 am, "Remy Lebeau" <no.s...@no.spam.com> wrote:
> "myprasanna" <myprasa...@discussions.microsoft.com> wrote in message

Remy Lebeau

unread,
Sep 3, 2009, 1:46:02 PM9/3/09
to

"Prasanna" <mypra...@gmail.com> wrote in message
news:1e4aa7c5-29b1-4af0...@x25g2000prf.googlegroups.com...

> Can we have a DC with no bitmap?

Not if you want to draw on the DC, no. The bitmap provides the physical
storage for the drawn pixel data.

--
Remy Lebeau (TeamB)

Chris Becke

unread,
Sep 17, 2009, 9:11:24 AM9/17/09
to

Prasanna wrote:
> Can we have a DC with no bitmap? If yes, when will it happen?
> Where are it's contents stored..


According to MSDN there are 4 types of GDI device contexts. The two of
interest to us are 'display' contexts and represent an area of display
memory, and 'memory' contexts that represent an area of memory called a
bitmap.

You cannot select an HBITMAP into a display DC, only a memory DC.

Basically, GetCurrentObject must be returning some kind of fake handle
from GetCurrentObject - the lie is revealed when you call GetObject with
that handle.

If you want a screenshot, you need to create a screen sized bitmap
yourself and BitBlt from the display to that.

0 new messages