I'm trying to load a bitmap by ID from resources (which works fine - I can then display it) and then get its dimensions with GetObject function. However, GetObject always returns NULL, which denotes an error. A call to GetLastError also returns NULL, which denotes NO error... Any idea what might be wrong?
Posted on 2004-05-20 15:01:54 by marcinbu
How about showing us how you are doing it ? It should be like this


hBmp dd ?
dibstruct DIBSECTION <>

invoke LoadImage, [hInstance], IDR_IMAGE, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION
mov [hBmp], eax
invoke GetObject, [hBmp], SIZEOF DIBSECTION, offset dibstruct
mov eax, [dibstruct.dsBm.bmBits]
mov [pDIBits], eax

Replace DIBSECTION for BITMAP if you are not using DIB sections.
Posted on 2004-05-20 15:19:38 by donkey
Thanks for the answer. I'm doing it almost the same way:

bm BITMAP <>

invoke LoadBitmap, edi, 2 ; edi = hInstance, 2 = RESOURCE_ID
mov , eax ; the bitmap actually gets loaded with no errors - I can display it

invoke GetObject, eax, (sizeof BITMAP), (offset bm) ; this call fails - it returns 0
invoke GetLastError ; this call also returns 0, which means no error

I copied and pasted your code and it also doesn't work, however GetLastError now returns 8 (not enough memory). My bitmap seems OK because the same happens with another one.
Posted on 2004-05-22 01:13:19 by marcinbu
The high level code certainly looks correct, but I have a quick question...

invoke GetObject, eax, (sizeof BITMAP), (offset bm) ; this call fails - it returns 0

Doesnt offset use eax behind the scenes? Perhaps your over writing (eax-hWnd)
by accident. Then again maybe not, I thought the compiler warned of such things...
Posted on 2004-05-22 08:15:10 by Graebel
Hi Graebel,

That's ADDR that can use EAX (lea eax,etc...), offset is equivalent to an immediate.

Hi marcinbu,

I don't ever use LoadBitmap (I only ever use DIB sections) but a quick test shows that I have no problem with the following code, it returns 24:

invoke LoadBitmap,[hInstance],IDR_TBBITMAP

push eax
invoke GetObject,eax,SIZEOF BITMAP,offset sbmp
pop eax
invoke DeleteObject,eax

So I can't really help, sorry
Posted on 2004-05-22 09:07:31 by donkey
I've got no idea what might be wrong.... Anyways, thanks for your help.
Posted on 2004-05-22 10:06:35 by marcinbu
Hi :)
How about calling GetObject with a NULL pointer, to see how many bytes you need to allocate? That might give you a hint on what's going on. :?:
Posted on 2004-05-22 13:05:05 by QvasiModo
Hi Qvasimodo,

It should always be SIZEOF BITMAP, that is the only valid value for a bitmap handle. I would suggest that you run the program under NT/2K or XP so that GetLastError will tell you what the problem is. I assume that as part of the older GDI that GetLastError does not work under Win9x, as is the case in many of those kind of functions.
Posted on 2004-05-22 13:14:18 by donkey
Forgot to mention: I'm running XP. I even tested it on another XP machine. In both cases GetObject returns 0 and GetLastError also returns null. How about if I sent you my executable so you can run it under a debugger and see it for yourself?
Posted on 2004-05-22 13:34:10 by marcinbu
Got it! :) This is pretty odd but described situation happens if the BITMAP structure which is to be filled by GetObject is located at na odd address - align it to a word boundary and everything works fine... Seems I discovered a bug in Windows or something.... Anyways, I'd like to thank you all of you for trying to solve the problem with me.
Posted on 2004-05-22 14:05:58 by marcinbu
Most of data used by Windows must be DWORD aligned ;)
Posted on 2004-05-22 14:50:27 by omega_red

Yeah, not a bug but a rule.

I try to avoid misaligned data like the plague. It slows down your program by an enormous degree. Usually I put ALIGN 4 (ALIGN 16 for SIMD stuff) after strings have been declared, I wrote an article for the Wiki book about it, what ever happened to that thing ???
Posted on 2004-05-22 16:23:38 by donkey
the wiki? or your article?

have a nice day or night.
Posted on 2004-05-22 16:32:48 by rea
Both really,

But I found it...


It's grown a bit since I wrote it, there was a big push for the book for a while and now it seems mostly silent about it. Don't hear much at all anymore.
Posted on 2004-05-22 16:37:20 by donkey
Yes, I still have some ideas :) but first before write it, I need read a book that I see time a go :) a big one. Also there are things mostly time :S. But hey... if I write for answer a post, for what not write in the book other time ;).

Have a nice day or night
Posted on 2004-05-22 16:47:54 by rea