Which is called first, DllMain() or global static object constructor?

后端 未结 1 844
盖世英雄少女心
盖世英雄少女心 2020-12-24 15:14

I am writing a DLL which defines a global static object.

In the object\'s constructor I am doing some initialization that may or may not succeed.

Is it possi

相关标签:
1条回答
  • 2020-12-24 15:49

    MSDN's DllMain documentation says:

    If your DLL is linked with the C run-time library (CRT), the entry point provided by the CRT calls the constructors and destructors for global and static C++ objects. Therefore, these restrictions for DllMain also apply to constructors and destructors and any code that is called from them.

    Since the code within DllMain may use the static objects, the static objects must be constructed before DllMain is run for DLL_PROCESS_ATTACH, and destroyed after it is run for DLL_PROCESS_DETACH.

    You can verify this with a simple test exe and 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 those will print:

    Main, loading library
    Moo, constructor
    DllMain, DLL_PROCESS_ATTACH
    Main, freeing library
    DllMain, DLL_PROCESS_DETACH
    Moo, destructor
    Main, exiting
    

    As you can see, the static object is constructed before DllMain(...,DLL_PROCESS_ATTACH,...) and destroyed after DllMain(...,DLL_PROCESS_DETACH,...)

    0 讨论(0)
提交回复
热议问题