dll loading process will first initialize global variables

See the code a file only one global variable is initialized in a generation dll project, wonder why this code will be executed when dll is loaded, so breakpoint debugging found

__declspec(noinline)
BOOL __cdecl
__DllMainCRTStartup(
        HANDLE  hDllHandle,
        DWORD   dwReason,
        LPVOID  lpreserved
        )
{
        BOOL retcode = TRUE;

    __try {
         __native_dllmain_reason = dwReason;
        __try{
            /*
             * If this is a process detach notification, check that there has
             * been a prior process attach notification.
             */
            if ( (dwReason == DLL_PROCESS_DETACH) && (__proc_attached == 0) ) {
                retcode = FALSE;
                the __leave; 
            } 

            IF (|| dwReason the DLL_PROCESS_ATTACH dwReason == == DLL_THREAD_ATTACH) {
                 IF (_pRawDllMain) 
                    retcode = (* _pRawDllMain) (hDllHandle, dwReason, lpReserved); 

                IF (retcode) 
                    retcode = _CRT_INIT (hDllHandle, dwReason, lpReserved); // global variable initialization sequence looks like there is no guarantee

                 if (! retcode) 
                    __leave; 
            } 

            retcode = DllMain (hDllHandle, dwReason, lpReserved); // dll entry function, which is usually loud noise in the dll in their written

             if ((dwReason == DLL_PROCESS_ATTACH) &&!retcode ) {
                /*
                 * The user's DllMain routine returned failure.  Unwind the init.
                 */
                DllMain(hDllHandle, DLL_PROCESS_DETACH, lpreserved);
                _CRT_INIT(hDllHandle, DLL_PROCESS_DETACH, lpreserved);
                if ( _pRawDllMain )
                    (*_pRawDllMain)(hDllHandle, DLL_PROCESS_DETACH, lpreserved);
            }

            if ( (dwReason == DLL_PROCESS_DETACH) ||
                 (dwReason == DLL_THREAD_DETACH) ) {
                if ( _CRT_INIT(hDllHandle, dwReason, lpreserved) == FALSE ) {
                    retcode = FALSE ;
                }

                if ( retcode && _pRawDllMain ) {
                    retcode = (*_pRawDllMain)(hDllHandle, dwReason, lpreserved);
                }
            }
        } __except ( __CppXcptFilter(GetExceptionCode(), GetExceptionInformation()) ) {
            retcode = FALSE;
        }
    } __finally
    {
        __native_dllmain_reason = __NO_REASON;
    }

        return retcode ;
}

This is the dll loading part of the code, the focus of the text marked red; it is possible to find the global variables before entering dllmain function will initialize;

It can also extend a problem when initializing global variables interdependence will cause bug, so a good design are:

Only a dll initialization class object is initialized as a global variable, others need to initialize the object initialization sequence in this class is initialized, so initialization sequence can guarantee the

Guess you like

Origin www.cnblogs.com/likemao/p/11226437.html