Say I have one executable: app.exe
I use 2 different 3rd party DLLs in this executable: foo.dll
bar.dll
and the Application must link implicitly to these DLLs, that is I cannot use ::LoadLibrary
to load them.
(Note: It's not that I cannot call LoadLibrary
, but these DLLs require static linking (C++ DLLs with __declspec(dllexport)
), so me calling LoadLibrary
doesn't make any sense because the excutable loader has already called it.)
These two DLLs do not have any dependencies on each other, that is, their load order is undefined as far as I can tell (and should be irrelevant). (Dependencies of both are basically only on the standard windows dlls (kernel32, msvcrt, etc.)
I now have the problem that I wish to control the load order of these DLLs, that is I wish that foo.dll is always loaded (DLL_PROCESS_ATTACH
) before bar.dll.
Is it somehow possible to tell the Windows DLL Loader to load one DLL before another?
Edit: To check the DLL load order of an executable, one can use the DUMPBIN.exe
utility: (Just launch the Visual Studio Command Prompt)
Edit: As per this answer / this blog entry, the NT Loader does walk the import section sequentially. (Which will result in independent DLLs being loaded in the order they appear in the import section.)
C:\path\to\program> dumpbin /IMPORTS app.exe | grep -i \.dll
MSVCR80D.dll
KERNEL32.dll
OLEAUT32.dll
MSVCP80D.dll
foo.dll
bar.DLL
This output means that MSVCR80D.dll (and its dependecies[a]) will be loaded first and that bar.DLL will be loaded last. Unload will happen in reverse order.
What I haven't found out yet is how to influence this load order ...
(Notes)
[a] : This means of course that e.g. kernel32.dll will be loaded first, because msvcr80d.dll will depend on kernel32.dll.
As per some requests, I'm adding a rationale for this: (But please, I'm still interested in this generally. I know how to work around the MFC problem.)
The Microsoft MFC DLL in it's debug version has memory leak detection built in. (As far as I can tell, it's the same mechanism used by _CrtSetDbgFlag and related tools.)
The MFC debug DLL will dump all unfreed memory when it is unloaded. Now, if you have a second DLL in your process, that is independent of MFC, and this second DLL deallocates memory on DLL_PROCESS_DETACH, the MFC reporting mechanism will report false memory leaks, if the MFC DLL is unloaded before the other dll.
If one could make sure that the debug MFC DLL is loaded first / unloaded last of all independent DLLs, then all other DLLs would already have cleaned up after themselves and MFC wouldn't report false leaks.
kernel32.dll
is not going to be loaded after a lowly CRT DLL (msvcr*.dll
) :P You can use ProcMon or WinDbg to find out. – kizzx2