Quotes:
From the document "Best Practices for Creating DLLs" http://download.microsoft.com/download/a/f/7/af7777e5-7dcd-4800-8a0a-b18336565f5b/DLL_bestprac.doc of Microsoft:
"DLLs often have complex interdependencies that implicitly define the order in which they must be loaded. The library loader efficiently analyzes these dependencies, calculates the correct load order, and loads the DLLs in that order."[1]
"(within DLLMain) Use the memory management function from the dynamic C Run- Time (CRT). If the CRT DLL is not initialized, calls to these functions can cause the process to crash."[2]
From MSDN: http://msdn.microsoft.com/en-us/library/988ye33t.aspx
"The _DllMainCRTStartup function does several things, including calling _CRT_INIT, which initializes the C/C++ run-time library and invokes C++ constructors on static, non-local variables. Without this function, the run- time library would be left in an uninitialized state."[3]
"In addition to initializing the C run-time library, _DllMainCRTStartup calls a function called DllMain."[4]
Questions:
If your DLL depends on CRT DLLs, based on [1], CRT DLLs will be loaded first (be initialized first), so how [2] could happen ?
Based on [3] and [4], _DllMainCRTStartup will calls _CRT_INIT which initializes the CRT, so how [2] could happen ?
If an executeable file loads your DLL by "Implicit linking", the _DllMainCRTStartup (and DLLMain) of your DLL will be called before the entry point (mainCRTStartup or WinMainCRTStartup) of the executeable file, based on [3] - _DllMainCRTStartup calls _CRT_INIT which initializes the CRT, and the mainCRTStartup is going to initialize the CRT too, so what actually happend with the CRT ?
If your DLL will be loaded before mainCRTStartup, the calling to CRT functions within DLLMain or other export functions is safe or not ?
Who will initialize CRT DLLs actually ?