How to obtain the login user name for Windows

Source: Internet
Author: User

How to obtain the login user name for Windows
Keyword: Get login username GetUserName wtsquerysessioninformation
Generally, you can use the GetUserName (or getusernameex) function to obtain the current login username (but not always get it, which will be analyzed below). This system function is available in Win95, WINNT, and all subsequent operating systems. Code As follows:
Bool csecuritytool: getcurrprocessuser (cstring & strname)
{
Bool Bret (true );
Strname = _ T ("");
DWORD dwsize = max_path;
Tchar * pszname = new tchar [dwsize];
If (! GetUserName (pszname, & dwsize ))
{
Delete [] pszname;
Pszname = new tchar [dwsize];
Bret = GetUserName (pszname, & dwsize );
}
Strname = pszname;
Delete [] pszname;
Return Bret;
}
The purpose of this function is to obtain the User Name of the current thread (msdn: retrieves the User Name of the current thread ). For NT Service (NT Service Program ) Start the process, and the result is the user name of the NT service process, that is, "system", rather than the Login User Name. Similarly, if the process is created through createprocessasuser, the user obtained by GetUserName will be the "asuser" user name. In addition, if the current thread is impersonate another user environment (this can be achieved using the impersonateloggedonuser function), it will obtain another user name. Therefore, this function can only obtain the login user name in a specific environment. So how can we get the Login User Name accurately without the difference in the running environment of the process itself? First, let's take a look at the Windows XP operating system. It provides the wtsquerysessioninformation function, which can obtain session-related information, one of which is used to obtain the login user of the session. The Code is as follows:
Bool csecuritytool: getloguserxp (cstring & strname)
{
Bool Bret = false;
Strname = _ T (""); // For XP or above
Tchar * szlogname = NULL;
DWORD dwsize = 0;
If (wtsquerysessioninformation (wts_current_server_handle, wts_current_session, wtsusername, & szlogname, & dwsize ))
{
Strname = szlogname;
Wtsfreememory (szlogname );
Bret = true;
}
Return Bret;
}
If the user has not logged on, the user name obtained will be blank (for example, in the NT Service Program ). Although wtsquerysessioninformation specified in msdn can be used in Win2000 pro, the terminal service is not installed when Win2000 professional is installed (unless terminal service can be installed using a special method such as a third-party tool ), therefore, calling this function will fail and you need to find other methods.
Look at Win2000 again: I checked a lot of information and could not find the system function that directly obtained the login username in Win2000. It seems that only the curve saved the country.
The user of the assumer.exe process must be the current login user, so the user name obtained is equivalent to the Login User Name obtained. Specific implementation: first, let's look at all the processes in the system, find the mongoer.exe process ID, then get the token of the process through the ID, and then get the user information of the token, that is, the login user name. The Code is as follows:
// Obtain the Win2000 Login User
Bool csecuritytool: getloguser2k (cstring & strname)
{
Bool Bret = false;
Handle hsnapshot = NULL;
Strname = _ T ("");
_ Try
{
// Get a snapshot of the processes in the system
Hsnapshot = createconlhelp32snapshot (th32cs_snapprocess, 0 );
If (hsnapshot = NULL)
{
_ Leave;
}
Processentry32 pe32;
Pe32.dwsize = sizeof (pe32); // find the "System" Process
Bool fprocess = process32first (hsnapshot, & pe32 );
While (fprocess)
{
If (lstrcmpi (pe32.szexefile, text ("assumer.exe") = 0)
{
Tchar szusername [max_path];
If (getprocessuser (pe32.th32processid, szusername, max_path ))
{
Bret = true;
Strname = szusername;
}
Break;
}
Fprocess = process32next (hsnapshot, & pe32 );
}
If (! Fprocess)
{
_ Leave; // didn't find "system" Process
}
}
_ Finally
{
// Cleanup the snapshot
If (hsnapshot! = NULL) closehandle (hsnapshot );
}
Return Bret;
}

// Obtain the User Name of the process
Bool csecuritytool: getprocessuser (DWORD dwprocessid, tchar * szusername, DWORD nnamelen)
{
Bool fresult = false;
Handle hproc = NULL;
Handle htoken = NULL;
Token_user * ptokenuser = NULL;
_ Try {
// Open the process with process_query_information access
Hproc = OpenProcess (process_query_information, false, dwprocessid );
If (hproc = NULL) {_ leave ;}
Fresult = openprocesstoken (hproc, token_query, & htoken );
If (! Fresult)
{
_ Leave;
}
DWORD dwneedlen = 0;
Fresult = gettokeninformation (htoken, tokenuser, null, 0, & dwneedlen );
If (dwneedlen> 0)
{
Ptokenuser = (token_user *) New byte [dwneedlen];
Fresult = gettokeninformation (htoken, tokenuser, ptokenuser, dwneedlen, & dwneedlen );
If (! Fresult)
{
_ Leave;
}
}
Else
{
_ Leave;
}

Sid_name_use Sn;
Tchar szdomainname [max_path];
DWORD dwdmlen = max_path;
Fresult = lookupaccountsid (null, ptokenuser-> User. Sid, szusername, & nnamelen, szdomainname, & dwdmlen, & Sn );
}
_ Finally
{
If (hproc)
: Closehandle (hproc );
If (htoken)
: Closehandle (htoken );
If (ptokenuser)
Delete [] (char *) ptokenuser;
Return fresult;
}
}
Colleagues familiar with the win2000 system will surely find that this method has a defect: The assumer.exe process may not exist (it is killed by the user or interrupted by the user). At this time, this method cannot obtain the login user name. However, you can only do this before you have a better method. Summary
Therefore, if you need to obtain the login user name in the software, you must select a different method based on the specific situation. If you are sure that your process must be started in the login user environment, you can use GetUserName. Otherwise, you need to use the following two methods. Of course, you need to determine the type of the operating system before use.

you are welcome to share other methods.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.