views:

706

answers:

3

Hi,

My question is not exactly the same as this one (it's not theoretical, there is only a main thread without message loop, InitInstance and ExitInstance are no fitting calls).

I'm using a console app without message loop; this app loads an exe with the LoadLibrary function, so that it can use its exported functions. Bad news: the DllMain function of the exe is not called (and I verified the symbols tables, using a def file, DllMain appears correctly); the doc says it's called if the loaded module is a DLL (too bad).

What are the conditions (if they exist) which could lead to the execution of the exe's DllMain function when LoadLibrary is called (and maybe again when FreeLibrary is called)?

Best regards

A: 

This doc can help you :
Best Practices for Creating DLLs http://www.microsoft.com/whdc/driver/kernel/DLL_bestprac.mspx

lsalamon
+1  A: 

The most obvious condition is that the process calling LoadLibrary() explicitly gets GetProcAddress("DllMain") and then calls it.

MSalters
A: 

Completing the good answer of MSalters:

So, then, call the "fake" DllMain with DLL_XXX_ATTACH just after LoadLibrary and with DLL_XXX_DETACH just before FreeLibrary, and make manually the other calls.

Another following implementation would be to build and load an interface DLL which could callback automatically the EXE on its fake DllMain (I don't know if it could work); but it may be more complicated than just manually calling the fake DllMain in a number of cases. (can't LoadLibrary in a DllMain)

moala
You can't call LoadLibrary("your.exe") from the DllMain in your helper DLL - part of the long list of things you can't do there. That makes your helper DLL rather tricky to use; it can't be a simple wrapper/forwarder,
MSalters