BOOL APIENTRY DllMain( HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch ( ul_reason_for_call ) { case DLL_PROCESS_ATTACH: DisableThreadLibraryCalls( hModule ); if( !prepareHeap()) return FALSE; DebugWriteA("*** DLL Attach (" VERSIONNUMBER "-Debugbuild | built on " __DATE__ " at " __TIME__")\n"); ZeroMemory( &g_strEmuInfo, sizeof(g_strEmuInfo) ); ZeroMemory( g_devList, sizeof(g_devList) ); ZeroMemory( &g_sysMouse, sizeof(g_sysMouse) ); ZeroMemory( g_aszDefFolders, sizeof(g_aszDefFolders) ); ZeroMemory( g_aszLastBrowse, sizeof(g_aszLastBrowse) ); g_strEmuInfo.hinst = hModule; g_strEmuInfo.fDisplayShortPop = true; // display pak switching message windows by default #ifdef _UNICODE { g_strEmuInfo.Language = GetLanguageFromINI(); if ( g_strEmuInfo.Language == 0 ) { g_strEmuInfo.Language = DetectLanguage(); DebugWriteA("Autoselect language: %d\n", g_strEmuInfo.Language); } g_hResourceDLL = LoadLanguageDLL(g_strEmuInfo.Language); // HACK: it's theoretically not safe to call LoadLibraryEx from DllMain... --rabid if( g_hResourceDLL == NULL ) { g_strEmuInfo.Language = 0; g_hResourceDLL = hModule; DebugWriteA("couldn't load language DLL, falling back to defaults\n"); } } #else DebugWriteA(" (compiled in ANSI mode, language detection DISABLED.)\n"); g_strEmuInfo.Language = 0; g_hResourceDLL = hModule; #endif // #ifndef _UNICODE InitializeCriticalSection( &g_critical ); break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; case DLL_PROCESS_DETACH: //CloseDLL(); if (g_hResourceDLL != g_strEmuInfo.hinst) FreeLibrary(g_hResourceDLL); // HACK: it's not safe to call FreeLibrary from DllMain... but screw it DebugWriteA("*** DLL Detach\n"); CloseDebugFile(); // Moved here from CloseDll DeleteCriticalSection( &g_critical ); // Moved here from CloseDll... Heap is created from DllMain, // and now it's destroyed by DllMain... just safer code --rabid if( g_hHeap != NULL ) { HeapDestroy( g_hHeap ); g_hHeap = NULL; } break; } return TRUE; }
BOOL CTortoiseSIProcApp::InitInstance() { CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": InitInstance()\n")); // Set the visual manage that is used to redraw the application CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows)); // Specifies that the style of the button border corresponds to the current Windows theme CMFCButton::EnableWindowsTheming(); // Initialize Windows GDI+ (must be called before any GDI+ call) Gdiplus::GdiplusStartupInput gdiplusStartupInput; Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL); LoadLanguageDLL(); SetHelpFile(); setlocale(LC_ALL, ""); // TODO: Check that TortoiseSI exists? For now we assume it does // TODO: Check that we have the minimum required version of TortoiseSI InitializeUIComponents(); CWinAppEx::InitInstance(); // Application settings to be stored in registry instead of INI files. // This also sets m_pszRegistryKey SetRegistryKey(_T("TortoiseSIProc")); /* * Create a named mutex to limit application to a single instance. * TODO: Create a randomly named mutex and store the name so that it can only be obtained * by an authorized user to prevent a malicious application from creating the * named mutex before us and purposely preventing our application from running. * Alternatively, create a locked file in the users profile directory. */ CAutoGeneralHandle TGitMutex = ::CreateMutex(NULL, FALSE, _T("TortoiseSIProc.exe")); /* Establish Integrity server connection */ int port = getIntegrationPort(); if (port > 0 && port <= std::numeric_limits<unsigned short>::max()) { EventLog::writeInformation(L"TortoiseSIProcApp connecting to Integrity client via localhost:" + std::to_wstring(port)); m_integritySession = std::unique_ptr<IntegritySession>(new IntegritySession("localhost", port)); } else { EventLog::writeInformation(L"TortoiseSIProcApp connecting to Integrity client via localintegration point"); m_integritySession = std::unique_ptr<IntegritySession>(new IntegritySession()); } m_serverConnectionsCache = std::unique_ptr<ServerConnections>(new ServerConnections(*m_integritySession)); if (!m_serverConnectionsCache->isOnline()) { EventLog::writeInformation(L"TortoiseSIProcApp::InitInstance() bailing out, unable to connected to server"); return FALSE; } if (!ProcessCommandLine()) { return FALSE; } return TRUE; }