diff --git a/Sources/CoreFoundation/CFKnownLocations.c b/Sources/CoreFoundation/CFKnownLocations.c index 4ed726b891..c5c269342b 100644 --- a/Sources/CoreFoundation/CFKnownLocations.c +++ b/Sources/CoreFoundation/CFKnownLocations.c @@ -18,6 +18,7 @@ #if TARGET_OS_WIN32 #include +#include #endif CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUser user, CFStringRef _Nullable username) { @@ -96,30 +97,16 @@ CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUs } case _kCFKnownLocationUserCurrent: username = CFGetUserName(); - // fallthrough + break; 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); + wchar_t* path = NULL; + SHGetKnownFolderPath(&FOLDERID_LocalAppData, 0, NULL, &path); + + CFStringRef userPath = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, path, wcslen(path)); + CoTaskMemFree(path); - CFRelease(profilesDir); - if (user == _kCFKnownLocationUserCurrent) { - CFRelease(username); - } + location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, userPath, kCFURLWindowsPathStyle, true); + CFRelease(userPath); break; } }