除已知方法外,通过Win32 API获取Windows当前用户登录名的其他方式有哪些?
Great question! You’ve already covered the most common go-to methods, but there are several other reliable Win32 API-based approaches and niche techniques to retrieve the current logged-on username on Windows. Let’s break them down with practical details:
1. Process Token + SID Lookup
This method pulls the current process’s access token, extracts the user’s security identifier (SID), then resolves that SID to a human-readable username using LookupAccountSid. It’s robust across nearly all Windows versions:
#include <windows.h> #include <sddl.h> #include <iostream> bool GetUsernameFromToken() { HANDLE hToken; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) { return false; } DWORD bufferSize = 0; GetTokenInformation(hToken, TokenUser, nullptr, 0, &bufferSize); LPTOKEN_USER pTokenUser = (LPTOKEN_USER)LocalAlloc(LPTR, bufferSize); if (GetTokenInformation(hToken, TokenUser, pTokenUser, bufferSize, &bufferSize)) { WCHAR username[256], domain[256]; DWORD usernameLen = 256, domainLen = 256; SID_NAME_USE sidType; if (LookupAccountSid(nullptr, pTokenUser->User.Sid, username, &usernameLen, domain, &domainLen, &sidType)) { std::wcout << L"Username: " << domain << L"\\" << username << std::endl; } } LocalFree(pTokenUser); CloseHandle(hToken); return true; }
Note: Most user-mode processes have the required TOKEN_QUERY access right by default, so this works without extra permissions for standard scenarios.
2. WTSQuerySessionInformation (Multi-Session Environments)
Perfect for terminal services, Remote Desktop, or multi-user server setups—this method directly fetches the username tied to the current active session:
#include <windows.h> #include <wtsapi32.h> #pragma comment(lib, "wtsapi32.lib") bool GetSessionUsername() { WCHAR* pUsername = nullptr; DWORD bytesReturned; if (WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSUserName, &pUsername, &bytesReturned)) { std::wcout << L"Session Username: " << pUsername << std::endl; WTSFreeMemory(pUsername); return true; } return false; }
Pro tip: Pair this with the WTSDomainName info class to get the full domain\username string.
3. NetUserGetInfo (Network Management API)
This uses Windows’ Network Management API to pull detailed user account data, including the username. You’ll need to link against netapi32.lib:
#include <windows.h> #include <lmaccess.h> #pragma comment(lib, "netapi32.lib") bool GetUsernameViaNetAPI() { WCHAR username[256]; DWORD usernameLen = 256; if (!GetUserNameW(username, &usernameLen)) { return false; } USER_INFO_2* pUserInfo = nullptr; NET_API_STATUS status = NetUserGetInfo(nullptr, username, 2, (LPBYTE*)&pUserInfo); if (status == NERR_Success) { std::wcout << L"Username (NetAPI): " << pUserInfo->usri2_name << std::endl; NetApiBufferFree(pUserInfo); return true; } return false; }
Note: Regular users have default read access to their own account info, so this works without elevated privileges.
4. GetThreadToken (Impersonation Scenarios)
If your app is impersonating another user (via ImpersonateLoggedOnUser), use this to fetch the thread’s impersonation token instead of the process token—this gives you the username of the impersonated user rather than the process’s original user. The flow matches the process token method, just swap OpenProcessToken with:
HANDLE hThreadToken; if (GetThreadToken(GetCurrentThread(), &hThreadToken)) { // Use hThreadToken with GetTokenInformation + LookupAccountSid as before CloseHandle(hThreadToken); }
内容的提问来源于stack exchange,提问作者dummzeuch

