Featured post
dll - Which is called first, DllMain() or global static object constructor? -
i writing dll defines global static object.
in object's constructor doing initialization may or may not succeed.
is possible signal success or failure of initialization process in dllmain() ? of 2 called first ?
thank you.
msdn's dllmain documentation says:
if dll linked c run-time library (crt), entry point provided crt calls constructors , destructors global , static c++ objects. therefore, these restrictions dllmain apply constructors , destructors , code called them.
since code within dllmain may use static objects, static objects must constructed before dllmain run dll_process_attach, , destroyed after run dll_process_detach.
you can verify simple test exe , test dll.
exe:
int _tmain(int argc, _tchar* argv[]) { wprintf(l"main, loading library\n"); hmodule h = loadlibrary(l"test.dll"); if (h) { wprintf(l"main, freeing library\n"); freelibrary(h); } wprintf(l"main, exiting\n"); return 0; }
dll:
struct moo { moo() { wprintf(l"moo, constructor\n"); } ~moo() { wprintf(l"moo, destructor\n"); } }; moo m; bool apientry dllmain( hmodule hmodule, dword ul_reason_for_call, lpvoid lpreserved) { switch (ul_reason_for_call) { case dll_process_attach: wprintf(l"dllmain, dll_process_attach\n"); break; case dll_thread_attach: wprintf(l"dllmain, dll_thread_attach\n"); break; case dll_thread_detach: wprintf(l"dllmain, dll_thread_detach\n"); break; case dll_process_detach: wprintf(l"dllmain, dll_process_detach\n"); break; default: wprintf(l"dllmain, ????\n"); break; } return true; }
together print:
main, loading library moo, constructor dllmain, dll_process_attach main, freeing library dllmain, dll_process_detach moo, destructor main, exiting
as can see, static object constructed before dllmain(...,dll_process_attach,...)
, destroyed after dllmain(...,dll_process_detach,...)
- Get link
- X
- Other Apps
Comments
Post a Comment