diff --git a/src/common/impl/FFPlatform_windows.c b/src/common/impl/FFPlatform_windows.c index ad93d2338..c3a06f3c8 100644 --- a/src/common/impl/FFPlatform_windows.c +++ b/src/common/impl/FFPlatform_windows.c @@ -1,12 +1,12 @@ #include "FFPlatform_private.h" #include "common/io.h" #include "common/library.h" -#include "common/mallocHelper.h" #include "common/stringUtils.h" #include "common/windows/unicode.h" #include "common/windows/registry.h" #include "common/windows/nt.h" +#include #include #include #include @@ -145,30 +145,18 @@ static void getUserName(FFPlatform* platform) size = ARRAY_SIZE(buffer); if (GetUserNameW(buffer, &size)) // GetUserNameExW(10002)? - { ffStrbufSetWS(&platform->userName, buffer); - - size = 0; - DWORD refDomainSize = 0; - SID_NAME_USE sidNameUse = SidTypeUnknown; - LookupAccountNameW(NULL, buffer, NULL, &size, NULL, &refDomainSize, &sidNameUse); - if (size > 0) - { - FF_AUTO_FREE PSID sid = (PSID) malloc(size); - FF_AUTO_FREE LPWSTR refDomain = (LPWSTR) malloc(refDomainSize * sizeof(wchar_t)); - if (LookupAccountNameW(NULL, buffer, sid, &size, refDomain, &refDomainSize, &sidNameUse)) - { - LPWSTR sidString; - if (ConvertSidToStringSidW(sid, &sidString)) - { - ffStrbufSetWS(&platform->sid, sidString); - LocalFree(sidString); - } - } - } - } else ffStrbufSetS(&platform->userName, getenv("USERNAME")); + + alignas(TOKEN_USER) char buf[SECURITY_MAX_SID_SIZE + sizeof(TOKEN_USER)]; + if (NT_SUCCESS(NtQueryInformationToken(NtCurrentProcessToken(), TokenUser, buf, sizeof(buf), &size))) + { + TOKEN_USER* tokenUser = (TOKEN_USER*) buf; + UNICODE_STRING sidString = { .Buffer = buffer, .Length = 0, .MaximumLength = sizeof(buffer) }; + if (NT_SUCCESS(RtlConvertSidToUnicodeString(&sidString, tokenUser->User.Sid, FALSE))) + ffStrbufSetNWS(&platform->sid, sidString.Length / sizeof(wchar_t), sidString.Buffer); + } } static void getHostName(FFPlatform* platform) diff --git a/src/common/windows/nt.h b/src/common/windows/nt.h index e6636d6f8..9ec6e9ef0 100644 --- a/src/common/windows/nt.h +++ b/src/common/windows/nt.h @@ -892,3 +892,4 @@ NTSYSAPI NTSTATUS NTAPI NtQueryInformationToken( _In_ ULONG TokenInformationLength, _Out_ PULONG ReturnLength ); +#define NtCurrentProcessToken() ((HANDLE)(LONG_PTR)-4) // for NtQueryInformationToken only; Windows 8+