Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SHGetKnownFolderPath to get path to localappdata for current user #4998

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

stevenbrix
Copy link

On Windows, it's not guaranteed that USERPROFILE is equivalent to %SYSTEMDRIVE%\Users\%USERNAME%. So it's possible that UserDefaults tries to write to a non-existent location, in which case it will never work. When this happens, all writes silently no-op.

This PR uses the Shell API SHGetKnownFolderPath to get the location of %LOCALAPPDATA% instead of manually building the path.

See documentation for this API:
image

Fixes #4997

@ShikiSuen
Copy link

@stevenbrix You might want to remove the format changes introduced on your computer.
This minifies the modifications that only necessary changes are kept.

P.S.: This PR is really important and I don't know why it gets abandoned here for months.

@ShikiSuen
Copy link

ShikiSuen commented Feb 4, 2025

@stevenbrix This PR is fallen behind the main branch too far.
The file to modified has been relocated to this folder:
/Sources/CoreFoundation/CFKnownLocations.c
And the part I guess you might want to modify is limited to this scope:

case _kCFKnownLocationUserCurrent:
username = CFGetUserName();
// fallthrough
case _kCFKnownLocationUserByName: {
DWORD size = 0;
GetProfilesDirectoryW(NULL, &size);
wchar_t* path = (wchar_t*)malloc(size * sizeof(wchar_t));
GetProfilesDirectoryW(path, &size);
CFStringRef pathRef = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, path, size - 1);
free(path);
CFURLRef profilesDir = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, pathRef, kCFURLWindowsPathStyle, true);
CFRelease(pathRef);
CFURLRef usernameDir = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, profilesDir, username, true);
CFURLRef appdataDir = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, usernameDir, CFSTR("AppData"), true);
location = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, appdataDir, CFSTR("Local"), true);
CFRelease(usernameDir);
CFRelease(appdataDir);
CFRelease(profilesDir);
if (user == _kCFKnownLocationUserCurrent) {
CFRelease(username);
}
break;

@parkera parkera requested a review from compnerd February 4, 2025 19:28
}
wchar_t* path = NULL;
SHGetKnownFolderPath(&FOLDERID_LocalAppData, 0, NULL, &path);
location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, path, kCFURLWindowsPathStyle, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems incorrect. The path parameter is a wide C String not a CFStrimg. This should do the creation of the CFString and pass that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UserDefaults doesn't work on Windows when USERNAME and USERPROFILE don't match
4 participants