From 1bbc5e96e2020fd12785a01865cdcb39c43fc5ad Mon Sep 17 00:00:00 2001 From: Rom Walton Date: Tue, 6 May 2014 11:28:47 -0400 Subject: [PATCH] client & MGR: On Windows, handle expansion of environment variables in the registry value that stores DATADIR. --- clientgui/BOINCGUIApp.cpp | 37 +++++++++++++++++++++++++++---------- lib/win_util.cpp | 33 +++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/clientgui/BOINCGUIApp.cpp b/clientgui/BOINCGUIApp.cpp index 21182883f4..2cbc4daacb 100644 --- a/clientgui/BOINCGUIApp.cpp +++ b/clientgui/BOINCGUIApp.cpp @@ -815,9 +815,11 @@ void CBOINCGUIApp::DetectDataDirectory() { // LONG lReturnValue; HKEY hkSetupHive; - LPTSTR lpszRegistryValue = NULL; TCHAR szPath[MAX_PATH]; - DWORD dwSize = 0; + LPTSTR lpszValue = NULL; + LPTSTR lpszExpandedValue = NULL; + DWORD dwValueType = REG_EXPAND_SZ; + DWORD dwSize = 0; // change the current directory to the boinc data directory if it exists lReturnValue = RegOpenKeyEx( @@ -833,27 +835,41 @@ void CBOINCGUIApp::DetectDataDirectory() { hkSetupHive, _T("DATADIR"), NULL, - NULL, + &dwValueType, NULL, &dwSize ); if (lReturnValue != ERROR_FILE_NOT_FOUND) { // Allocate the buffer space. - lpszRegistryValue = (LPTSTR) malloc(dwSize); - (*lpszRegistryValue) = NULL; + lpszValue = (LPTSTR) malloc(dwSize); + (*lpszValue) = NULL; // Now get the data lReturnValue = RegQueryValueEx( hkSetupHive, _T("DATADIR"), NULL, - NULL, - (LPBYTE)lpszRegistryValue, + &dwValueType, + (LPBYTE)lpszValue, &dwSize ); - // Store the root directory for later use. - m_strBOINCMGRDataDirectory = lpszRegistryValue; + // Expand the Strings + // We need to get the size of the buffer needed + dwSize = 0; + lReturnValue = ExpandEnvironmentStrings(lpszValue, NULL, dwSize); + + if (lReturnValue) { + // Make the buffer big enough for the expanded string + lpszExpandedValue = (LPTSTR) malloc(lReturnValue*sizeof(TCHAR)); + (*lpszExpandedValue) = NULL; + dwSize = lReturnValue; + + ExpandEnvironmentStrings(lpszValue, lpszExpandedValue, dwSize); + + // Store the root directory for later use. + m_strBOINCMGRDataDirectory = lpszExpandedValue; + } } } else { if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA|CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, szPath))) { @@ -867,7 +883,8 @@ void CBOINCGUIApp::DetectDataDirectory() { // Cleanup if (hkSetupHive) RegCloseKey(hkSetupHive); - if (lpszRegistryValue) free(lpszRegistryValue); + if (lpszValue) free(lpszValue); + if (lpszExpandedValue) free(lpszExpandedValue); #endif #ifdef __WXMAC__ m_strBOINCMGRDataDirectory = wxT("/Library/Application Support/BOINC Data"); diff --git a/lib/win_util.cpp b/lib/win_util.cpp index c5d050f8f1..1916ce530d 100644 --- a/lib/win_util.cpp +++ b/lib/win_util.cpp @@ -52,8 +52,10 @@ BOOL TerminateProcessById( DWORD dwProcessID ) { void chdir_to_data_dir() { LONG lReturnValue; HKEY hkSetupHive; - LPSTR lpszRegistryValue = NULL; char szPath[MAX_PATH]; + LPSTR lpszValue = NULL; + LPSTR lpszExpandedValue = NULL; + DWORD dwValueType = REG_EXPAND_SZ; DWORD dwSize = 0; // change the current directory to the boinc data directory if it exists @@ -70,26 +72,40 @@ void chdir_to_data_dir() { hkSetupHive, "DATADIR", NULL, - NULL, + &dwValueType, NULL, &dwSize ); if (lReturnValue != ERROR_FILE_NOT_FOUND) { // Allocate the buffer space. - lpszRegistryValue = (LPSTR) malloc(dwSize); - (*lpszRegistryValue) = NULL; + lpszValue = (LPSTR) malloc(dwSize); + (*lpszValue) = NULL; // Now get the data lReturnValue = RegQueryValueExA( hkSetupHive, "DATADIR", NULL, - NULL, - (LPBYTE)lpszRegistryValue, + &dwValueType, + (LPBYTE)lpszValue, &dwSize ); - SetCurrentDirectoryA(lpszRegistryValue); + // Expand the Strings + // We need to get the size of the buffer needed + dwSize = 0; + lReturnValue = ExpandEnvironmentStringsA(lpszValue, NULL, dwSize); + + if (lReturnValue) { + // Make the buffer big enough for the expanded string + lpszExpandedValue = (LPSTR) malloc(lReturnValue); + (*lpszExpandedValue) = NULL; + dwSize = lReturnValue; + + ExpandEnvironmentStringsA(lpszValue, lpszExpandedValue, dwSize); + + SetCurrentDirectoryA(lpszExpandedValue); + } } } else { if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_COMMON_APPDATA|CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, szPath))) { @@ -101,7 +117,8 @@ void chdir_to_data_dir() { } if (hkSetupHive) RegCloseKey(hkSetupHive); - if (lpszRegistryValue) free(lpszRegistryValue); + if (lpszValue) free(lpszValue); + if (lpszExpandedValue) free(lpszExpandedValue); } std::wstring A2W(const std::string& str) {