如何在Windows中获得当前登录用户的域

How to get the domain of currently logged on user in Windows?

本文关键字:登录 用户 Windows      更新时间:2023-10-16

我知道我可以通过调用GetUserName获得当前登录的用户名。我正在寻找一个API函数,返回此用户的域。

参见如何在Windows NT、Windows 2000或Windows XP上检索当前用户名和域名

的例子:

#define UNICODE
#define _UNICODE
#include <windows.h>
#include <tchar.h>
BOOL GetCurrentUserAndDomain(PTSTR szUser, PDWORD pcchUser,
      PTSTR szDomain, PDWORD pcchDomain) {
   BOOL         fSuccess = FALSE;
   HANDLE       hToken   = NULL;
   PTOKEN_USER  ptiUser  = NULL;
   DWORD        cbti     = 0;
   SID_NAME_USE snu;
   __try {
      // Get the calling thread's access token.
      if (!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE,
            &hToken)) {
         if (GetLastError() != ERROR_NO_TOKEN)
            __leave;
         // Retry against process token if no thread token exists.
         if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY,
               &hToken))
            __leave;
      }
      // Obtain the size of the user information in the token.
      if (GetTokenInformation(hToken, TokenUser, NULL, 0, &cbti)) {
         // Call should have failed due to zero-length buffer.
         __leave;
      } else {
         // Call should have failed due to zero-length buffer.
         if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
            __leave;
      }
      // Allocate buffer for user information in the token.
      ptiUser = (PTOKEN_USER) HeapAlloc(GetProcessHeap(), 0, cbti);
      if (!ptiUser)
         __leave;
      // Retrieve the user information from the token.
      if (!GetTokenInformation(hToken, TokenUser, ptiUser, cbti, &cbti))
         __leave;
      // Retrieve user name and domain name based on user's SID.
      if (!LookupAccountSid(NULL, ptiUser->User.Sid, szUser, pcchUser,
            szDomain, pcchDomain, &snu))
         __leave;
      fSuccess = TRUE;
   } __finally {
      // Free resources.
      if (hToken)
         CloseHandle(hToken);
      if (ptiUser)
         HeapFree(GetProcessHeap(), 0, ptiUser);
   }
   return fSuccess;
}
int main()
{
    TCHAR user[1024], domain[1024];
    DWORD chUser = sizeof(user), chDomain = sizeof(domain);
    if (GetCurrentUserAndDomain(user, &chUser, domain, &chDomain))
    {
        _tprintf(TEXT("user:%sndomain:%sn"), user, domain);
    }
}

在我的例子中输出:

user:dlaru_000
domain:IKH-WIN81

注意您必须将程序链接到advapi32.lib。

相关文章: