To complement the other answers, for the sake of completness.
The actual signature of DllMain
has an HINSTANCE
parameter, instead of a HMODULE
parameter. The Visual Studio DLL template generates the signature with HMODULE
since at least Visual Studio 2008 however, but I believe this to be a minor bug more than anything. VC6 generated the code with HANDLE
(even though both HINSTANCE
and HMODULE
exist). The reason that doesn't cause problems is because HINSTANCE
and HMODULE
are now exactly the same thing. Unfortunately I was unable to find an ancient enough version of the MSDN documetnation that could have confirmed this.
So the answer is: You get your HINSTANCE
as an argument to your DllMain
.
Personally I sort of like the distinction between HMODULE and HINSTANCE because it appeals to me as being good code hygiene. It's a bit like using const
. But then, a new question arises: Given your HINSTANCE
, how do you get your HMODULE
in the "hygienic" way?
The windowsx.h
header defines GetInstanceModule
, which is now a macro that just casts the HINSTANCE
to HMODULE
. It only exists for code compatibility, along with a bunch of very similar macros.