DllMain safety
-
Is it safe to call GetModuleHandle and GetProcAddress inside DllMain?
The difficult we do right away... ...the impossible takes slightly longer.
-
Is it safe to call GetModuleHandle and GetProcAddress inside DllMain?
The difficult we do right away... ...the impossible takes slightly longer.
If you're trying to get the address of functions in the same DLL, I don't see why you need to call these APIs. Instead you could directly get the address of the functions. If you're trying to get the address of functions from a different module, then calling
GetModuleHandle
fromDllMain
is not safe and can deadlock because of a loader lock being acquired. Here is a best practice guide to DLLs - Dynamic-Link Library Best Practices[^] Here are some interesting reads with regards to DllMain - Does creating a thread from DllMain deadlock or doesn't it?[^] Another reason not to do anything scary in your DllMain: Inadvertent deadlock[^]«_Superman_» _I love work. It gives me something to do between weekends.
_Microsoft MVP (Visual C++) (October 2009 - September 2013)
-
If you're trying to get the address of functions in the same DLL, I don't see why you need to call these APIs. Instead you could directly get the address of the functions. If you're trying to get the address of functions from a different module, then calling
GetModuleHandle
fromDllMain
is not safe and can deadlock because of a loader lock being acquired. Here is a best practice guide to DLLs - Dynamic-Link Library Best Practices[^] Here are some interesting reads with regards to DllMain - Does creating a thread from DllMain deadlock or doesn't it?[^] Another reason not to do anything scary in your DllMain: Inadvertent deadlock[^]«_Superman_» _I love work. It gives me something to do between weekends.
_Microsoft MVP (Visual C++) (October 2009 - September 2013)
Thanks Superman!
The difficult we do right away... ...the impossible takes slightly longer.