Skip to content

Commit 0c251b8

Browse files
committed
bugfix(globaldata): Fix the handling of documents folder redirection by using SHGetKnownFolderPath() - Vista+ required
1 parent c8c809c commit 0c251b8

2 files changed

Lines changed: 24 additions & 12 deletions

File tree

Generals/Code/GameEngine/Source/Common/GlobalData.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,15 +1173,24 @@ void GlobalData::parseGameDataDefinition( INI* ini )
11731173

11741174
TheWritableGlobalData->m_userDataDir.clear();
11751175

1176-
char temp[_MAX_PATH];
1177-
if (::SHGetSpecialFolderPath(nullptr, temp, CSIDL_PERSONAL, true))
1176+
PWSTR pszPath = nullptr;
1177+
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &pszPath);
1178+
if (SUCCEEDED(hr) && pszPath)
11781179
{
1179-
if (temp[strlen(temp)-1] != '\\')
1180-
strcat(temp, "\\");
1181-
strcat(temp, TheWritableGlobalData->m_userDataLeafName.str());
1182-
strcat(temp, "\\");
1183-
CreateDirectory(temp, nullptr);
1184-
TheWritableGlobalData->m_userDataDir = temp;
1180+
AsciiString myDocumentsDirectory;
1181+
myDocumentsDirectory.translate(UnicodeString(pszPath));
1182+
CoTaskMemFree(pszPath);
1183+
1184+
if (myDocumentsDirectory.getCharAt(myDocumentsDirectory.getLength() -1) != '\\')
1185+
myDocumentsDirectory.concat( '\\' );
1186+
1187+
myDocumentsDirectory.concat(TheWritableGlobalData->m_userDataLeafName.str());
1188+
1189+
if (myDocumentsDirectory.getCharAt( myDocumentsDirectory.getLength() - 1) != '\\')
1190+
myDocumentsDirectory.concat( '\\' );
1191+
1192+
CreateDirectory(myDocumentsDirectory.str(), nullptr);
1193+
TheWritableGlobalData->m_userDataDir = myDocumentsDirectory;
11851194
}
11861195

11871196
// override INI values with user preferences

GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,10 +1038,13 @@ GlobalData::GlobalData()
10381038

10391039
// Set user data directory based on registry settings instead of INI parameters. This allows us to
10401040
// localize the leaf name.
1041-
char temp[_MAX_PATH + 1];
1042-
if (::SHGetSpecialFolderPath(nullptr, temp, CSIDL_PERSONAL, true))
1043-
{
1044-
AsciiString myDocumentsDirectory = temp;
1041+
PWSTR pszPath = nullptr;
1042+
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &pszPath);
1043+
if (SUCCEEDED(hr) && pszPath)
1044+
{
1045+
AsciiString myDocumentsDirectory;
1046+
myDocumentsDirectory.translate(UnicodeString(pszPath));
1047+
CoTaskMemFree(pszPath);
10451048

10461049
if (myDocumentsDirectory.getCharAt(myDocumentsDirectory.getLength() -1) != '\\')
10471050
myDocumentsDirectory.concat( '\\' );

0 commit comments

Comments
 (0)