Tuesday, September 6, 2011

DLL Init/Cleanup sequence

See ctrdll.c 
- crtdll.c: BOOL WINAPI _CRT_INIT()
- crtdll.c: BOOL __cdecl __DllMainCRTStartup()
  ... this call _CRT_INIT()
- crtexe.c: int __tmainCRTStartup()
  ... similar to __DllMainCRTStartup()


Basic sequence for DLL_PROCESS_ATTACH
for each DLL
 - Call __CRT_INIT() -> invoke C initializers then C++ constructors
 - Call DllMain hook


Basic sequence for DLL_PROCESS_DETACH
for each DLL
 - Call DllMain hook
 - Call __CRT_INIT() -> invoke C+_ destructors and C atexit()/_onexit() ... reverse order of initialization


Basic sequence for DLL_THREAD_ATTACH and DLL_THREAD_DETACH
for each DLL
 - Call DllMain hook


CWinApp::InitInstance and CWinApp::ExitInstance are called as part of DllMain hook (they are only called for DLL_PROCESS_ATTACH and DLL_PROCESS_DETACH).


Article regarding the order of DLL loading:
http://blogs.msdn.com/b/oleglv/archive/2003/10/28/56142.aspx

"Well, turns out that the loader seems to be preserving the order in which the imported DLLs are listed in the Imports Section of the loading executable."







No comments:

Post a Comment