1
votes

A call to LoadImage() in the first steps of my program returns NULL. Just after it, GetLastError() is called, and it surprisingly returns 0. I wondered why LoadImage() is failing, since GetLastError() clearly reveals that no error code is set after the failed function. This is a snippet of the code:

if ( (hbitmap = (HBITMAP) LoadImage(hThisInstance, MAKEINTRESOURCE(MY_BITMAP), 
                                    IMAGE_BITMAP, 0, 0, 
                                    LR_CREATEDIBSECTION)) == NULL) 
    printf("Last error: %d\n", GetLastError());

With HBITMAP hbitmap, HINSTANCE hThisInstance (argument of WinMain), and MY_BITMAP a valid bitmap resource.

2
Hmm... what does FindResourcereturn for that bitmap? - Dabbler
If LoadImage() is returning NULL, GetLastError() can only return 0 if another API function is called before you call GetLastError() and it clears the current error code. GetLastError() is only meaningful IMMEDIATELY after the failed function. Which makes me think the code posted is not the real code being used. - Remy Lebeau
You either found a bug in Windows or we're not looking at the real code. Given the very unhelpful error reporting, especially in a GUI app, I'll but a buck on "not the real code". - Hans Passant
I call it immediately after it. Why should I not post the whole meaningful code? I have been stuck over this for too many hours, I want to understand why it doesn't work. To tell the truth, it also happened a similar thing once called CreateDIBSection (in another program), handling the error in the same way. - Stencil
Requested size is 0 x 0 pixels and you are not requesting LR_DEFAULTSIZE, was this the intent? - Roman R.

2 Answers

5
votes

Apparently, LoadImage returns NULL and does not set any error when there's a problem with the file format.

A copy of the bitmap I was trying to load can be found here. I created it using GIMP, which in the current version is quite buggy.

The solution was to open the file in Paint and save it again. Now LoadImage loads the image without any complaints.

3
votes

I found exactly the same behaviour with the latest version of GIMP. When you're doing an export from Gimp, select the Compatibility Options [+] button and check the "Do not write colourspace information" checkbox, and the bitmap will load OK using LoadImage. Edit: I see a prior comment to that effect now, otherwise I might not have bothered writing!