Domanda

I am working on a DLL which contains a bitmap resource. I imported that bitmap through the Resource View in Visual Studio. The DLL also exports a class which has a function that tries to load the bitmap:

BOOL CMyExportedClass::Create(CWnd* pParentWnd /*= NULL*/)
{
    if (!m_bitmap.LoadBitmap(IDB_BITMAP1))
        return FALSE;
    // ...
}

From an MFC application I load the DLL. Inside the applications InitInstance() function I then invoke an exported function of the DLL which eventually calls CMyExportedClass::Create().

However, LoadBitmap() always returns 0, which according to the documentation indicates either insufficient memory (not the case) or that the resource does not exist.

The bitmap file is part of my DLL project and IDB_BITMAP1 is defined in the .rc file as well as in resource.h:

MyDLL.rc:

IDB_BITMAP1             BITMAP                  "res\\mybitmap.bmp"

resource.h:

#define IDB_BITMAP1                     1034

I already checked that the ID has been defined only once. FWIW: At the time LoadBitmap() is invoked, the DLLs InitiInstance() function has already returned successfully.

What are possible reasons why LoadBitmap() refuses to load the resource and how can I fix this?


Edit:
I can successfully load the resource if instead of m_bitmap.LoadBitmap(IDB_BITMAP1) I do the following:

HBITMAP hBitmap = (HBITMAP)LoadImage(hCurrentModule, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0, 0);
if(hBitmap)
    m_bitmap.Attach((HBITMAP)hBitmap);

So what's the difference between CBitmap::LoadBitmap() and the LoadImage() functions here?

È stato utile?

Soluzione

The most likely explanation is that some other module in the process has a resource with that ID. And the MFC code that searches for the resource is looking in this other module for the module which calls LoadBitmap.

My recommendation is to use LoadImage and so exert precise control over which module's resources are searched.

Altri suggerimenti

Actually, no search is performed. MFC loads bitmap from the executable file instead of dll according to documentation:

Loads the bitmap resource named by lpszResourceName or identified by the ID number in nIDResource from the application's executable file.

Edit (to complete answer):

Instead use this (or LoadImage() as you have done):

m_bitmap.Attach(::LoadBitmap(hCurrentModule, MAKEINTRESOURCE(IDB_BITMAP1)));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top