1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| static bool changeSystemTempDir(const std::string& newTempPath, std::error_code& ec) { DWORD dwRet = 0; auto wNewTempPath = s2ws(newTempPath); auto szNewTempPath = wNewTempPath.c_str(); DWORD dwlen = wNewTempPath.size() * sizeof(wchar_t); HKEY hKey = NULL; DWORD dwtype = REG_EXPAND_SZ; DWORD dwMsgRet = 0; dwRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", 0, KEY_WRITE | KEY_READ, &hKey); if (dwRet != ERROR_SUCCESS) { ec.assign(::GetLastError(), std::system_category()); ERRW("[ChangeTempDir]RegOpenKeyEx failed %s", std::to_string(ec).c_str()); return FALSE; } ON_SCOPE_EXIT({ if (hKey) { RegCloseKey(hKey); hKey = NULL; } }); dwRet = RegSetValueEx(hKey, L"TEMP", 0, dwtype, (LPBYTE)szNewTempPath, dwlen); if (dwRet != ERROR_SUCCESS) { ec.assign(::GetLastError(), std::system_category()); ERRW("[ChangeTempDir]RegSetValueEx TEMP %s failed %s", szNewTempPath, std::to_string(ec).c_str()); return false; } INFOW("[ChangeTempDir]RegSetValueEx TEMP %s success", szNewTempPath);
dwRet = RegSetValueEx(hKey, L"TMP", 0, dwtype, (LPBYTE)szNewTempPath, dwlen); if (dwRet != ERROR_SUCCESS) { ec.assign(::GetLastError(), std::system_category()); ERRW("[ChangeTempDir]RegSetValueEx TMP %s failed %s", szNewTempPath, std::to_string(ec).c_str()); return false; } INFOW("[ChangeTempDir]RegSetValueEx TMP %s success", szNewTempPath);
RegFlushKey(hKey); PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, NULL, NULL); SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, NULL, (LPARAM)(L"Environment"), SMTO_NORMAL, INFINITE, &dwMsgRet); SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, NULL, (LPARAM)(L"Environment"), SMTO_NOTIMEOUTIFNOTHUNG, INFINITE, &dwMsgRet); SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, NULL, (LPARAM)(L"Environment"), SMTO_ABORTIFHUNG, INFINITE, &dwMsgRet); INFO("[ChangeTempDir]dwMsgRet is %d", dwMsgRet); return true; }
|