HMODULE LoadCoreClr() { errno_t errno = 0; TCHAR szCoreClrDirectory[MAX_PATH]; DWORD dwCoreClrDirectory = GetEnvironmentVariableW(L"CORECLR_DIR", szCoreClrDirectory, MAX_PATH); HMODULE hCoreCLRModule = nullptr; if (dwCoreClrDirectory != 0) { WCHAR wszClrPath[MAX_PATH]; wszClrPath[0] = L'\0'; errno = wcscpy_s(wszClrPath, _countof(wszClrPath), szCoreClrDirectory); CHECK_RETURN_VALUE_FAIL_EXIT_VIA_FINISHED(errno); if (wszClrPath[wcslen(wszClrPath) - 1] != L'\\') { errno = wcscat_s(wszClrPath, _countof(wszClrPath), L"\\"); CHECK_RETURN_VALUE_FAIL_EXIT_VIA_FINISHED(errno); } errno = wcscat_s(wszClrPath, _countof(wszClrPath), L"coreclr.dll"); CHECK_RETURN_VALUE_FAIL_EXIT_VIA_FINISHED(errno); // Add the core clr directory to the list of dll search paths AddDllDirectory(szCoreClrDirectory); // Modify the default dll flags so that dependencies can be found in this path SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_USER_DIRS); // Continue loading as usual hCoreCLRModule = ::LoadLibraryExW(wszClrPath, NULL, 0); } if (hCoreCLRModule == nullptr) { // This is used when developing #if AMD64 hCoreCLRModule = ::LoadLibraryExW(L"..\\..\\..\\artifacts\\build\\ProjectK\\Runtime\\amd64\\coreclr.dll", NULL, 0); #else hCoreCLRModule = ::LoadLibraryExW(L"..\\..\\..\\artifacts\\build\\ProjectK\\Runtime\\x86\\coreclr.dll", NULL, 0); #endif } if (hCoreCLRModule == nullptr) { // Try the relative location based in install hCoreCLRModule = ::LoadLibraryExW(L"coreclr.dll", NULL, 0); } Finished: return hCoreCLRModule; }
void MainWindow::loadModules() { const QString appDirPath = QApplication::applicationDirPath(); const QDir dir(appDirPath + "/modules"); const QDir sharedDir(appDirPath + "/shared"); const auto modules = dir.entryList(QDir::Dirs, QDir::Name); auto& factory = DocumentFactory::instance(); // Just to make sure it is created here and now #ifdef WIN32 SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); QString sharedDirPath = sharedDir.absolutePath(); auto sharedDllDir = AddDllDirectory(sharedDirPath.toStdWString().c_str()); #endif for (const auto& module : modules) { if (module.startsWith(".")) continue; QDir moduleDir = dir; moduleDir.cd(module); QString dirPath = moduleDir.absolutePath(); factory.setModuleDirPath(dirPath.toStdString()); #ifdef WIN32 auto moduleDllDir = AddDllDirectory(dirPath.toStdWString().c_str()); #endif QFileInfo file(moduleDir, module); QString path = file.absoluteFilePath(); QLibrary lib(path); lib.load(); #ifdef WIN32 RemoveDllDirectory(moduleDllDir); #endif } #ifdef WIN32 RemoveDllDirectory(sharedDllDir); #endif }
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { // Attempt to mitigate http://textslashplain.com/2015/12/18/dll-hijacking-just-wont-die SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32); int exitCode = -1; CString cmdLine(lpCmdLine); if (cmdLine.Find(L"--checkInstall") >= 0) { // If we're already installed, exit as fast as possible if (!MachineInstaller::ShouldSilentInstall()) { exitCode = 0; goto out; } // Make sure update.exe gets silent wcscat(lpCmdLine, L" --silent"); } HRESULT hr = ::CoInitialize(NULL); ATLASSERT(SUCCEEDED(hr)); AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES); hr = _Module.Init(NULL, hInstance); bool isQuiet = (cmdLine.Find(L"-s") >= 0); bool weAreUACElevated = CUpdateRunner::AreWeUACElevated() == S_OK; bool explicitMachineInstall = (cmdLine.Find(L"--machine") >= 0); if (explicitMachineInstall || weAreUACElevated) { exitCode = MachineInstaller::PerformMachineInstallSetup(); if (exitCode != 0) goto out; isQuiet = true; // Make sure update.exe gets silent if (explicitMachineInstall) { wcscat(lpCmdLine, L" --silent"); printf("Machine-wide installation was successful! Users will see the app once they log out / log in again.\n"); } } if (!CFxHelper::CanInstallDotNet4_5()) { // Explain this as nicely as possible and give up. MessageBox(0L, L"This program cannot run on Windows XP or before; it requires a later version of Windows.", L"Incompatible Operating System", 0); exitCode = E_FAIL; goto out; } if (!CFxHelper::IsDotNet45OrHigherInstalled()) { hr = CFxHelper::InstallDotNetFramework(isQuiet); if (FAILED(hr)) { exitCode = hr; // #yolo CUpdateRunner::DisplayErrorMessage(CString(L"Failed to install the .NET Framework, try installing .NET 4.5 or higher manually"), NULL); goto out; } // S_FALSE isn't failure, but we still shouldn't try to install if (hr != S_OK) { exitCode = 0; goto out; } } // If we're UAC-elevated, we shouldn't be because it will give us permissions // problems later. Just silently rerun ourselves. if (weAreUACElevated) { wchar_t buf[4096]; HMODULE hMod = GetModuleHandle(NULL); GetModuleFileNameW(hMod, buf, 4096); CUpdateRunner::ShellExecuteFromExplorer(buf, lpCmdLine); exitCode = 0; goto out; } exitCode = CUpdateRunner::ExtractUpdaterAndRun(lpCmdLine, false); out: _Module.Term(); ::CoUninitialize(); return exitCode; }