Click to See Complete Forum and Search --> : HMODULE from HINSTANCE


Ernest
April 5th, 1999, 09:43 AM
I'm loading custom resources in a dll, and I'm trying to figure out the best way to acquire an HMODULE pointer. I can get the HMODULE by doing AfxLoadLibrary("dllname");, but I'd rather not hardcode the dll name into my code. I've noticed that the win32 helper functions that are provided (like ::LoadAccelerators) take the HINSTANCE pointer, but for a custom resource I think I need to use FindResource, which requires an HMODULE. Can anybody point me to a way to get the HMODULE pointer that doesn't require the dll name in a string?

Alek
April 5th, 1999, 10:23 AM
// You are right. You can't get HMODULE from HINSTANCE without hardcode of the dll name. See this below

// I think as you that this is the only way
HINSTANCE hInstance = LoadLibrary("dllname"); // Mapping module to memory
HMODULE hModule = GetModuleHandle("dllname"); // Getting module handle

// If you ever understand any more easy way of this write me please to alek@web.bg.
// Thanks.

Ernest
April 5th, 1999, 12:36 PM
Well, I must have done something stupid this morning, because I can apparently use the HINSTANCE in the HMODULE parameter of FindResource(). I don't know if they are equivalent everywhere or not, but you might try using one for the other. I get my HINSTANCE parameter from the dll initialization function (I save it in a static variable and then extern it for future reference.)

Jerry Coffin
April 6th, 1999, 01:39 AM
An HMODULE and an HINSTANCE are the same thing and you can use them interchangeably.

For example, if you look at the startup code in the standard library, the hInstance that get's passed to WinMain is obtained with GetModuleHandle(NULL);



The universe is a figment of its own imagination.