void AFXAPI AfxLockGlobals(int nLockType) { ASSERT((UINT)nLockType < CRIT_MAX); // intialize global state, if necessary if (!_afxCriticalInit) { AfxCriticalInit(); ASSERT(_afxCriticalInit); } // nothing necessary on Win32s (no multiple threads) if (_afxCriticalWin32s) return; // initialize specific resource if necessary if (!_afxLockInit[nLockType]) { EnterCriticalSection(&_afxLockInitLock); if (!_afxLockInit[nLockType]) { InitializeCriticalSection(&_afxResourceLock[nLockType]); VERIFY(++_afxLockInit[nLockType]); } LeaveCriticalSection(&_afxLockInitLock); } // lock specific resource EnterCriticalSection(&_afxResourceLock[nLockType]); #ifdef _DEBUG ASSERT(++_afxResourceLocked[nLockType] > 0); #endif }
BOOL WINAPI RawDllMain(HINSTANCE /*hInstance*/, DWORD dwReason, LPVOID) { if (dwReason == DLL_PROCESS_ATTACH) { // Prevent the C runtime DLL from being unloaded prematurely LoadLibraryA(MSVCRT_DLL); #ifdef _UNICODE // give error message and exit if running Unicode on non-Unicode system if (GetVersion() & 0x80000000) { // Note: this message is for programmers who can read english ::MessageBoxA(NULL, "This application or DLL can not be loaded " "on Windows 95 or on Windows 3.1. It takes advantage " "of Unicode features only available on Windows NT.", "MFC Runtime Module", MB_ICONSTOP|MB_OK); return FALSE; // and fail } #endif SetErrorMode(SetErrorMode(0) | SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX); // add a reference to thread local storage data AfxTlsAddRef(); // make sure we have enough memory to attempt to start (8kb) void* pMinHeap = LocalAlloc(NONZEROLPTR, 0x2000); if (pMinHeap == NULL) return FALSE; // fail if memory alloc fails LocalFree(pMinHeap); // cause early initialization of _afxCriticalSection if (!AfxCriticalInit()) return FALSE; #ifdef _AFX_OLE_IMPL // set module state before initialization AFX_MODULE_STATE* pModuleState = _AfxGetOleModuleState(); _AFX_THREAD_STATE* pState = AfxGetThreadState(); pState->m_pPrevModuleState = AfxSetModuleState(pModuleState); #endif } else if (dwReason == DLL_PROCESS_DETACH) { #ifdef _AFX_OLE_IMPL _AFX_THREAD_STATE* pThreadState = _afxThreadState.GetDataNA(); if (pThreadState != NULL) { // restore previously-saved module state VERIFY(AfxSetModuleState(pThreadState->m_pPrevModuleState) == _AfxGetOleModuleState()); DEBUG_ONLY(pThreadState->m_pPrevModuleState = NULL); } #endif // free up the _afxCriticalSection AfxCriticalTerm(); // Now it's OK for C runtime DLL to be unloaded (see LoadLibrary above) FreeLibrary(GetModuleHandleA(MSVCRT_DLL)); // remove reference from thread local data AfxTlsRelease(); } return TRUE; // ok }