Article ID: 111544 - Last Review: November 21, 2006 - Revision: 5.1 How to retrieve current user and domain names on Windows NT, Windows 2000, or Windows XPThis article was previously published under Q111544 On This PageSUMMARY
A program may sometimes need to display the user name and domain name for the current thread. This article demonstrates how to do this on Microsoft Windows NT, Windows 2000, or Windows XP by using security functions within the Win32 Application Programming Interface (API). MORE INFORMATION
Prior to Windows NT, it could be assumed that a thread was running under the account of the interactively logged on user. Windows NT, however, allows threads to run under multiple security contexts, potentially representing multiple users. For instance, in a client/server application, a thread in the server might impersonate a client through the ImpersonateNamedPipeClient function. In this case, it runs under the user context of the client. Another example of a thread running in a different security context is a service thread, which has a domain name of NT AUTHORITY and a user name of SYSTEM, assuming that the service is running in the local system account. If you need to obtain both the user name and the domain name for the current thread, you must first extract the user's security identifier (SID) from the thread's access token by using the GetTokenInformation function. Then, a call to the LookupAccountSid function can be used to retrieve the account name and domain name associated with the SID. The sample code at the end of this article demonstrates this technique. The 32-bit functions just mentioned are not available on Microsoft Windows 95 or Microsoft Windows 98. To retrieve the name and domain of the interactive user on Windows 95 or Windows 98, you must call a 16-bit LAN Manager function. Note If only the user name is required, the GetUserName function can be used on Windows 95, Windows 98, Windows NT, and Windows 2000. On Windows NT and Windows 2000, this function first checks to see if the calling thread has a specific access token, which is typically the result of an impersonation call. In this case, the user name associated with the calling thread is returned. Otherwise, the user name associated with the calling process is returned. Sample codeThe following sample code demonstrates how to programmatically retrieve the user name and domain name on Windows NT.APPLIES TO
| Article Translations
|
Back to the top
