|
1 | 1 | #include "FFPlatform_private.h" |
2 | 2 | #include "common/io.h" |
3 | 3 | #include "common/library.h" |
4 | | -#include "common/mallocHelper.h" |
5 | 4 | #include "common/stringUtils.h" |
6 | 5 | #include "common/windows/unicode.h" |
7 | 6 | #include "common/windows/registry.h" |
8 | 7 | #include "common/windows/nt.h" |
9 | 8 |
|
| 9 | +#include <stdalign.h> |
10 | 10 | #include <windows.h> |
11 | 11 | #include <shlobj.h> |
12 | 12 | #include <sddl.h> |
@@ -145,30 +145,18 @@ static void getUserName(FFPlatform* platform) |
145 | 145 |
|
146 | 146 | size = ARRAY_SIZE(buffer); |
147 | 147 | if (GetUserNameW(buffer, &size)) // GetUserNameExW(10002)? |
148 | | - { |
149 | 148 | ffStrbufSetWS(&platform->userName, buffer); |
150 | | - |
151 | | - size = 0; |
152 | | - DWORD refDomainSize = 0; |
153 | | - SID_NAME_USE sidNameUse = SidTypeUnknown; |
154 | | - LookupAccountNameW(NULL, buffer, NULL, &size, NULL, &refDomainSize, &sidNameUse); |
155 | | - if (size > 0) |
156 | | - { |
157 | | - FF_AUTO_FREE PSID sid = (PSID) malloc(size); |
158 | | - FF_AUTO_FREE LPWSTR refDomain = (LPWSTR) malloc(refDomainSize * sizeof(wchar_t)); |
159 | | - if (LookupAccountNameW(NULL, buffer, sid, &size, refDomain, &refDomainSize, &sidNameUse)) |
160 | | - { |
161 | | - LPWSTR sidString; |
162 | | - if (ConvertSidToStringSidW(sid, &sidString)) |
163 | | - { |
164 | | - ffStrbufSetWS(&platform->sid, sidString); |
165 | | - LocalFree(sidString); |
166 | | - } |
167 | | - } |
168 | | - } |
169 | | - } |
170 | 149 | else |
171 | 150 | ffStrbufSetS(&platform->userName, getenv("USERNAME")); |
| 151 | + |
| 152 | + alignas(TOKEN_USER) char buf[SECURITY_MAX_SID_SIZE + sizeof(TOKEN_USER)]; |
| 153 | + if (NT_SUCCESS(NtQueryInformationToken(NtCurrentProcessToken(), TokenUser, buf, sizeof(buf), &size))) |
| 154 | + { |
| 155 | + TOKEN_USER* tokenUser = (TOKEN_USER*) buf; |
| 156 | + UNICODE_STRING sidString = { .Buffer = buffer, .Length = 0, .MaximumLength = sizeof(buffer) }; |
| 157 | + if (NT_SUCCESS(RtlConvertSidToUnicodeString(&sidString, tokenUser->User.Sid, FALSE))) |
| 158 | + ffStrbufSetNWS(&platform->sid, sidString.Length / sizeof(wchar_t), sidString.Buffer); |
| 159 | + } |
172 | 160 | } |
173 | 161 |
|
174 | 162 | static void getHostName(FFPlatform* platform) |
|
0 commit comments