Skip to content

Commit 0a1a7c8

Browse files
committed
use shgetknownfolderpath instead of hand building the path
1 parent db6474a commit 0a1a7c8

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

Sources/CoreFoundation/CFKnownLocations.c

+10-23
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#if TARGET_OS_WIN32
2020
#include <userenv.h>
21+
#include <shlobj_core.h>
2122
#endif
2223

2324
CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUser user, CFStringRef _Nullable username) {
@@ -48,7 +49,7 @@ CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUs
4849

4950
break;
5051
}
51-
52+
5253
}
5354
#elif !DEPLOYMENT_RUNTIME_OBJC && !TARGET_OS_WIN32 && !TARGET_OS_ANDROID
5455

@@ -96,30 +97,16 @@ CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUs
9697
}
9798
case _kCFKnownLocationUserCurrent:
9899
username = CFGetUserName();
99-
// fallthrough
100+
break;
100101
case _kCFKnownLocationUserByName: {
101-
DWORD size = 0;
102-
GetProfilesDirectoryW(NULL, &size);
103-
104-
wchar_t* path = (wchar_t*)malloc(size * sizeof(wchar_t));
105-
GetProfilesDirectoryW(path, &size);
106-
107-
CFStringRef pathRef = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, path, size - 1);
108-
free(path);
109-
110-
CFURLRef profilesDir = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, pathRef, kCFURLWindowsPathStyle, true);
111-
CFRelease(pathRef);
112-
113-
CFURLRef usernameDir = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, profilesDir, username, true);
114-
CFURLRef appdataDir = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, usernameDir, CFSTR("AppData"), true);
115-
location = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, appdataDir, CFSTR("Local"), true);
116-
CFRelease(usernameDir);
117-
CFRelease(appdataDir);
102+
wchar_t* path = NULL;
103+
SHGetKnownFolderPath(&FOLDERID_LocalAppData, 0, NULL, &path);
104+
105+
CFStringRef userPath = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, path, wcslen(path));
106+
CoTaskMemFree(path);
118107

119-
CFRelease(profilesDir);
120-
if (user == _kCFKnownLocationUserCurrent) {
121-
CFRelease(username);
122-
}
108+
location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, userPath, kCFURLWindowsPathStyle, true);
109+
CFRelease(userPath);
123110
break;
124111
}
125112
}

0 commit comments

Comments
 (0)