How to get my own code's module handle?

dll, winapi

Solution

Store the module handle away when it is given to you in DllMain and then use it later when you actually need it. A lot of frameworks (e.g., MFC) do this automatically.

Problem

Possible Duplicate: How do I get the HMODULE for the currently executing code? I'm trying to find a resource in my own module. If this module is an executable, that's trivial - `GetModuleHandle(NULL)` returns the handle of the "main" module. My module, however, is a DLL that is loaded by another executable. So `GetModuleHandle(NULL)` will return the module handle to that executable, which is obviously not what I want. Is there any way to determine the module handle of the module that contains the currently running code? Using the DLL's name in a call to `GetModuleHandle()` seems like a hack to me (and is not easily maintainable in case the code in question is transplanted into a different DLL).

Original source

Related problems