0

What could be the possible reasons of getting return code as 0 from GetBitmap()?

BITMAP bmInfo;
int rc = bitmap->GetBitmap (&bmInfo);
int ec = GetLastError();

The value returned by GetLastError() is also 0. MSDN doesn't give any help and all the forums where similar questions have been asked are silent.

To give you some context, I have an instance of CBitmap and I am attaching a second instance of CBitmap to the same HBITMAP using code similar to the following:

CBitmap first;
:
:
CBitmap second;
second.Attach ((HBITMAP)first);

BITMAP bmInfo;
second.GetBitmap (&bmInfo);

The call to GetBitmap() fails for second and not for first.

Jaywalker
  • 3,079
  • 3
  • 28
  • 44
  • It is unsafe to attach a handle from object that owns it to another to be a new owner. Eventually one of them will be owning an invalid handle. – Roman R. Apr 18 '12 at 10:37
  • @RomanR., this is a valid comment but the above code is a simplified version of what I am trying to do. So, I am taking care of when each object gets destroyed. – Jaywalker Apr 18 '12 at 10:42

1 Answers1

1

The call to GetBitmap() fails for second and not for first.

If so, there is no way the two class instances hold the same handle, and your code snippet suggests exactly this. You can break with debugger to check your first and second to find out what they are actually holding inside.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • Your answer hinted me on the type casting I was done for `HBITMAP`, and that was the culprit. Thanks a lot! I still wonder why `GetLastError()` was returning 0. Shouldn't it give me something like invalid handle on Attach call? – Jaywalker Apr 18 '12 at 11:19
  • To guess off the cuff without bothering to look into source code, I'd say your `CBitmap` might be holding `NULL` and GetBitmap would do no API call at all. Hence, `GetLastError` would still hold some successful result from past. – Roman R. Apr 18 '12 at 11:21