1

I have a bitmap defined in my resource.h and .rc file:

#define IDB_BITMAP1                     130
IDB_BITMAP1             BITMAP  DISCARDABLE     "bitmap1.bmp"

When I try to load it with

hBMP = LoadBitmap(0, MAKEINTRESOURCE(IDB_BITMAP1));

it fails, and GetLastError() returns 1814 (or 0x716 in hex), which stands for ERROR_RESOURCE_NAME_NOT_FOUND. But the bitmap is there. Is it because of the hInstance? I thought that 0 works if the resource is in the .exe, and it is.

sashoalm
  • 75,001
  • 122
  • 434
  • 781

2 Answers2

4

It is a mistake to pass 0 as the instance. The documentation says:

An application can use the LoadBitmap function to access predefined bitmaps. To do so, the application must set the hInstance parameter to NULL and the lpBitmapName parameter to one of the following values.

...... table removed......

You are not loading a predefined bitmap and so need to pass the instance handle which contains the resource.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

It turned out that the instance handle is needed, it worked after I changed it to:

hBMP = LoadBitmap(GetModuleHandle(0), MAKEINTRESOURCE(IDB_BITMAP1));
sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • 3
    I'd advise against `GetModuleHandle(0)` because that will stop working when you compiler the code into a DLL. The instance handle is not hard to come by. You should pass it explicitly. – David Heffernan Jun 27 '14 at 07:32
  • @DavidHeffernan Yes, I know that, but in my case it's an EXE, not a DLL, and I know that code in particular won't be going into a DLL. – sashoalm Jul 07 '14 at 08:31
  • My point is that since it's easy to obtain the instance handle of the module containing the executing code, doing so makes your code future proof. – David Heffernan Jul 07 '14 at 08:49
  • But this code was a test project that I know I won't use in the future. I've already deleted it, because I gathered the data I needed. It was a throwaway code and it got thrown away. I get what you're telling me, but I'm tired of people thinking I've missed something, when in fact, I simply know the constraints of my requirements, and I don't make my code more complex than it needs to be. I was responding because of the downvote, anyway. If you want to, just edit my question with the "proper" DLL-friendly solution, at least it might help someone else. – sashoalm Jul 07 '14 at 10:02
  • I can understand that. When you post code here you can always expect it to be criticised. For the benefit of future readers I left that comment. I'm not your downvoter. Not sure why you got a downvote. The instance handle issue is covered here: http://stackoverflow.com/questions/557081/how-do-i-get-the-hmodule-for-the-currently-executing-code – David Heffernan Jul 07 '14 at 10:08