Skip to content

Commit 2679669

Browse files
committed
Replaced usage of deprecated SHGetFolderPathW with SHGetKnownFolderPath. Added fallback if SHGetKnownFolderPath fails
SHGetKnownFolderPath fails on Nano
1 parent 2adee00 commit 2679669

File tree

1 file changed

+17
-31
lines changed
  • contrib/win32/win32compat

1 file changed

+17
-31
lines changed

contrib/win32/win32compat/pwd.c

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ char *GetHomeDirFromToken(char *userName, HANDLE token)
130130
{
131131
UCHAR domain[200];
132132
wchar_t pw_buf[MAX_PATH] = { L'\0' };
133+
PWSTR tmp;
133134

134135
debug("-> GetHomeDirFromToken()...");
135136

@@ -152,49 +153,34 @@ char *GetHomeDirFromToken(char *userName, HANDLE token)
152153
profileInfo.hProfile = NULL;
153154
profileInfo.dwSize = sizeof(profileInfo);
154155

155-
156-
157-
if (LoadUserProfile(token, &profileInfo) == FALSE)
158-
{
159-
debug("<- GetHomeDirFromToken()...");
160-
debug("LoadUserProfile failure: %d", GetLastError());
161-
162-
return NULL;
163-
}
164-
165156
/*
166157
* And retrieve homedir from profile.
167158
*/
168-
169-
if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PROFILE, token, 0, pw_homedir)))
159+
160+
if (SUCCEEDED(SHGetKnownFolderPath(&FOLDERID_Documents, 0, token, &tmp)))
170161
{
171-
debug("<- GetHomeDirFromToken()...");
172-
debug("SHGetFolderPath failed");
173-
174-
return NULL;
162+
wcscpy_s(pw_homedir, MAX_PATH, tmp);
163+
CoTaskMemFree(tmp);
164+
} else
165+
{
166+
debug("SHGetKnownFolderPath on FOLDERID_Documents failed");
167+
GetWindowsDirectoryW(pw_homedir, MAX_PATH);
175168
}
176-
177-
// update APPDATA user's env variable
178-
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA, token, 0, pw_buf)))
169+
170+
// update APPDATA user's env variable
171+
if (SUCCEEDED(SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, token, &tmp)))
179172
{
180-
SetEnvironmentVariableW(L"APPDATA", pw_buf);
173+
SetEnvironmentVariableW(L"APPDATA", tmp);
174+
CoTaskMemFree(tmp);
181175
}
182176

183177
// update LOCALAPPDATA user's env variable
184-
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, token, 0, pw_buf)))
178+
if (SUCCEEDED(SHGetKnownFolderPath(&FOLDERID_LocalAppData, 0, token, &tmp)))
185179
{
186-
SetEnvironmentVariableW(L"LOCALAPPDATA", pw_buf);
180+
SetEnvironmentVariableW(L"LOCALAPPDATA", tmp);
181+
CoTaskMemFree(tmp);
187182
}
188183

189-
/*
190-
* Unload user profile.
191-
*/
192-
193-
if (UnloadUserProfile(token, profileInfo.hProfile) == FALSE)
194-
{
195-
debug("WARNING. Cannot unload user profile (%u).", GetLastError());
196-
}
197-
198184
debug("<- GetHomeDirFromToken()...");
199185

200186
return pw_homedir;

0 commit comments

Comments
 (0)