Get HModule from inside a DLL

I need to load some resource from my DLL (I need to load them from the DLL code), for this I use FindResource.

For this I need an HModule DLL. How to find it?

(I do not know the name (file name) of the DLL (the user can change it))

+3
source share
3 answers

The first argument DllMain()is a HMODULEDLL.

+9
source

You get it from the entry point of DllMain (), the first argument. Write one, save it in a global variable:

HMODULE DllHandle;

BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
  if (dwReason == DLL_PROCESS_ATTACH) DllHandle = hModule;
  return TRUE;
}

, 32- 64- Windows, . HMODULE DLL :

static HMODULE GetThisDllHandle()
{
  MEMORY_BASIC_INFORMATION info;
  size_t len = VirtualQueryEx(GetCurrentProcess(), (void*)GetThisDllHandle, &info, sizeof(info));
  assert(len == sizeof(info));
  return len ? (HMODULE)info.AllocationBase : NULL;
}
+8

, , DllMain , , DLL exe! ​​

DLLMain DLL.   .

, , :

// Determine the module handle by locating a function
// you know resides in that DLL or exe
HMODULE hModule;
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
                   GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
                   (LPCSTR)&myDLLfuncName, &hModule);

HRSRC hRscr = FindResource(hModule, ............);
0

Source: https://habr.com/ru/post/1735786/


All Articles