Skip to content

Base: add support for querying user home directory on Windows #4624

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

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions CoreFoundation/Base.subproj/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@


#if TARGET_OS_WIN32
#include <lm.h>
#include <shellapi.h>
#include <shlobj.h>
#include <WinIoCtl.h>
Expand Down Expand Up @@ -577,15 +578,44 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {
return result;
}
#elif TARGET_OS_WIN32
// This code can only get the directory for the current user
CFStringRef userName = uName ? CFCopyUserName() : NULL;
if (uName && !CFEqual(uName, userName)) {
CFLog(kCFLogLevelError, CFSTR("CFCopyHomeDirectoryURLForUser(): Unable to get home directory for other user"));
if (userName) CFRelease(userName);
return NULL;
}
if (userName) CFRelease(userName);
return CFCopyHomeDirectoryURL();
if (uName == NULL) {
return CFCopyHomeDirectoryURL();
}

CFStringRef home = NULL;
LPUSER_INFO_1 pBuffer = NULL;
DWORD dwEntriesRead = 0;
DWORD dwEntries = 0;
DWORD dwToken = 0;
NTSTATUS nStatus;
do {
nStatus = NetUserEnum(NULL, 1, FILTER_NORMAL_ACCOUNT, (LPBYTE*)&pBuffer,
MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwEntries,
&dwToken);
if (nStatus == NERR_Success || nStatus == ERROR_MORE_DATA) {
for (unsigned uiEntry = 0; !home && uiEntry < dwEntriesRead; ++uiEntry) {
CFStringRef name =
CFStringCreateWithCStringNoCopy(kCFAllocatorSystemDefault,
pBuffer[uiEntry].usri1_name,
kCFStringEncodingUTF16,
kCFAllocatorNull);

if (CFEqual(uName, name)) {
home = pBuffer[uiEntry].usri1_home_dir
? CFStringCreateWithCString(kCFAllocatorSystemDefault,
pBuffer[uiEntry].usri1_home_dir,
kCFStringEncodingUTF16)
: CFSTR("");
}

CFRelease(name);
}
}
NetApiBufferFree(pBuffer);
pBuffer = NULL;
} while (nStatus == ERROR_MORE_DATA);

return home;
#else
#error Dont know how to compute users home directories on this platform
#endif
Expand Down