Search code examples
gdiembedded-resource

Is it valid to use HBITMAP after calling FreeLibrary?


I have a case where I need to load bitmap from a resource dll and release handle to it. The update mechanism may update the dll, and having it open will fail overwriting it.

So lets say I have something like this:

HINSTANCE hInst = LoadLibraryEx(resourceDll, NULL, LOAD_LIBRARY_AS_DATAFILE);
HBITMAP hBitmap = LoadBitmap(hInst, "some.bmp")
FreeLibrary(hInst);

is it valid to use hBitmap after calling FreeLibrary ?

Thanks in advance.


Solution

  • Yes, it's valid to release the DLL.

    LoadBitmap does the equivalent of CreateCompatibleBitmap, which creates a new bitmap for your process. The bitmap image is initialized with the data from the DLLs resource. That is, the pixel information is copied to the bitmap. The bitmap does not hold a pointer to the resource data. Once created, you can release the DLL.