This question asks how to retrieve the handle of the DLL that contains the currently executed code. A link in one of the answers suggests taking the address of __ImageBase
as module handle. This works for me.
My question is: Given that it is so astonishingly simple to retrieve the current module's handle, why do frameworks like MFC (and even the code base I inherited) usually store the instance handle passed to DllMain()
in some global variable? Is there a reason not to rely on __ImageBase
?
Edit: According to Raymond Chen, __ImageBase
is for Microsoft linkers only. There is another question that has more precise answers, including a linker-independent way using GetModuleHandleEx()
. There is also a solution for Win2000 and earlier using VirtualQuery()
. The question remains valid: Why saving the base address when it can be retrieved easily?