Skip to content

Commit 9aeb428

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

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,16 +1173,40 @@ void GlobalData::parseGameDataDefinition( INI* ini )
11731173

11741174
TheWritableGlobalData->m_userDataDir.clear();
11751175

1176+
#if defined(_MSC_VER) && _MSC_VER < 1300
11761177
char temp[_MAX_PATH];
11771178
if (::SHGetSpecialFolderPath(nullptr, temp, CSIDL_PERSONAL, true))
11781179
{
1179-
if (temp[strlen(temp)-1] != '\\')
1180+
if (temp[strlen(temp) - 1] != '\\')
11801181
strcat(temp, "\\");
11811182
strcat(temp, TheWritableGlobalData->m_userDataLeafName.str());
11821183
strcat(temp, "\\");
11831184
CreateDirectory(temp, nullptr);
11841185
TheWritableGlobalData->m_userDataDir = temp;
11851186
}
1187+
#else
1188+
// TheSuperHackers @bugfix Mauller 20/03/2026 Fix the handling of folder redirection
1189+
// OneDrive and Group Policy folder redirection is better supported by SHGetKnownFolderPath()
1190+
PWSTR pszPath = nullptr;
1191+
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &pszPath);
1192+
if (SUCCEEDED(hr) && pszPath)
1193+
{
1194+
AsciiString myDocumentsDirectory;
1195+
myDocumentsDirectory.translate(UnicodeString(pszPath));
1196+
1197+
if (myDocumentsDirectory.getCharAt(myDocumentsDirectory.getLength() -1) != '\\')
1198+
myDocumentsDirectory.concat( '\\' );
1199+
1200+
myDocumentsDirectory.concat(TheWritableGlobalData->m_userDataLeafName.str());
1201+
1202+
if (myDocumentsDirectory.getCharAt( myDocumentsDirectory.getLength() - 1) != '\\')
1203+
myDocumentsDirectory.concat( '\\' );
1204+
1205+
CreateDirectory(myDocumentsDirectory.str(), nullptr);
1206+
TheWritableGlobalData->m_userDataDir = myDocumentsDirectory;
1207+
}
1208+
CoTaskMemFree(pszPath);
1209+
#endif
11861210

11871211
// override INI values with user preferences
11881212
OptionPreferences optionPref;

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,10 +1038,21 @@ 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+
#if defined(_MSC_VER) && _MSC_VER < 1300
1042+
char temp[_MAX_PATH + 1];
1043+
if (::SHGetSpecialFolderPath(nullptr, temp, CSIDL_PERSONAL, true))
1044+
{
1045+
AsciiString myDocumentsDirectory = temp;
1046+
#else
1047+
// TheSuperHackers @bugfix Mauller 20/03/2026 Fix the handling of folder redirection
1048+
// OneDrive and Group Policy folder redirection is better supported by SHGetKnownFolderPath()
1049+
PWSTR pszPath = nullptr;
1050+
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &pszPath);
1051+
if (SUCCEEDED(hr) && pszPath)
1052+
{
1053+
AsciiString myDocumentsDirectory;
1054+
myDocumentsDirectory.translate(UnicodeString(pszPath));
1055+
#endif
10451056

10461057
if (myDocumentsDirectory.getCharAt(myDocumentsDirectory.getLength() -1) != '\\')
10471058
myDocumentsDirectory.concat( '\\' );
@@ -1062,6 +1073,9 @@ GlobalData::GlobalData()
10621073
CreateDirectory(myDocumentsDirectory.str(), nullptr);
10631074
m_userDataDir = myDocumentsDirectory;
10641075
}
1076+
#if defined(_MSC_VER) && _MSC_VER > 1300
1077+
CoTaskMemFree(pszPath);
1078+
#endif
10651079

10661080
//-allAdvice feature
10671081
//m_allAdvice = FALSE;

0 commit comments

Comments
 (0)