|
16 | 16 |
|
17 | 17 | #include <assert.h>
|
18 | 18 |
|
| 19 | +#if TARGET_OS_WIN32 |
| 20 | +#include <userenv.h> |
| 21 | +#endif |
| 22 | + |
19 | 23 | CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUser user, CFStringRef _Nullable username) {
|
20 | 24 | CFURLRef location = NULL;
|
21 | 25 |
|
@@ -76,20 +80,48 @@ CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUs
|
76 | 80 | #elif TARGET_OS_WIN32
|
77 | 81 |
|
78 | 82 | switch (user) {
|
79 |
| - case _kCFKnownLocationUserAny: |
80 |
| - location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, CFSTR("\\Users\\All Users\\AppData\\Local"), kCFURLWindowsPathStyle, true); |
| 83 | + case _kCFKnownLocationUserAny: { |
| 84 | + DWORD size = 0; |
| 85 | + GetAllUsersProfileDirectoryW(NULL, &size); |
| 86 | + |
| 87 | + wchar_t* path = (wchar_t*)malloc(size * sizeof(wchar_t)); |
| 88 | + GetAllUsersProfileDirectoryW(path, &size); |
| 89 | + |
| 90 | + CFStringRef allUsersPath = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, path, size - 1); |
| 91 | + free(path); |
| 92 | + |
| 93 | + location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, allUsersPath, kCFURLWindowsPathStyle, true); |
| 94 | + CFRelease(allUsersPath); |
81 | 95 | break;
|
| 96 | + } |
82 | 97 | case _kCFKnownLocationUserCurrent:
|
83 | 98 | username = CFGetUserName();
|
84 | 99 | // fallthrough
|
85 |
| - case _kCFKnownLocationUserByName: |
86 |
| - const char *user = CFStringGetCStringPtr(username, kCFStringEncodingUTF8); |
87 |
| - CFURLRef userdir = CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault, (const unsigned char *)user, strlen(user), true); |
88 |
| - CFURLRef homedir = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, CFSTR("\\Users"), kCFURLWindowsPathStyle, true, userdir); |
89 |
| - location = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, CFSTR("\\AppData\\Local"), kCFURLWindowsPathStyle, true, homedir); |
90 |
| - CFRelease(homedir); |
91 |
| - CFRelease(userdir); |
| 100 | + 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); |
| 118 | + |
| 119 | + CFRelease(profilesDir); |
| 120 | + if (user == _kCFKnownLocationUserCurrent) { |
| 121 | + CFRelease(username); |
| 122 | + } |
92 | 123 | break;
|
| 124 | + } |
93 | 125 | }
|
94 | 126 |
|
95 | 127 | #elif TARGET_OS_ANDROID
|
|
0 commit comments