This section of Baidu Quiz, to me related to AH!!! ----How to get the Windows system login user name

Source: Internet
Author: User

How to get the Windows system login user name

Http://zhidao.baidu.com/link?url= Hva9pkvwyzv8ksewftsqtwe8fqm1dhoq59burnfadmcovfjfgjuonb2kq4krjuf5kjotxjcf5sqkynlhcu_dbmzggbxfxjcrfxcmamijuji

=======================

Generally use the GetUserName (or GetUserNameEx) function to get the current login login user name (but not always get, the following will be analyzed), this system function in Win95, WinNT and all subsequent operating systems are available. The code is 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 number of this letter is accurate to get the user name of the current thread (MSDN: Retrieves the user name of the "the"). If the NT service (NT Services program) starts this process, the result is the user name of the NT service process, "SYSTEM", not the login user name, and, similarly, if the process was created through CreateProcessAsUser, The user getusername gets will be the user name of "Asuser". In addition, if the current line is impersonating impersonate other user environments (this can be achieved with a function impersonateloggedonuser), it will get another user name. Therefore, this function can only get the login user name in a specific environment.
How do you get the login user name exactly as the process itself is different from the operating environment?
Let's look at the Windows XP operating system first, which provides the Wtsquerysessioninformation function, which can get information about the session, one of which is to get the logged-in 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 in, the obtained user name is empty (for example, in an NT service program). While MSDN indicates that wtsquerysessioninformation can be used in Win2000 Pro, the terminal when installing Win2000 Professional The service is not installed (unless the Terminal Service can be installed with a special method such as a third-party tool), so calling this function will fail and need to look for other methods.
Look at Win2000: read a lot of data, failed to find in the Win2000 directly access to the login user name System functions, it seems only the curve to save the nation. Since the user of the Explorer.exe process is definitely the current login user, obtaining its username is equal to getting the login user name. Implementation: First enumerate all processes of the system, find the Explorer.exe process ID, and then obtain the token of this process through the ID, and then obtain the user information of the token, that is, the login user name. The code is as follows://Get 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 = CreateToolhelp32Snapshot (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 ("explorer.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;
}

Gets 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 certainly find that this method is flawed: The Explorer.exe process may not exist (by the user killed or himself interrupted), this method will not get the login user name. But before there is no better way, you can only.

Summarize
Therefore, if you need to obtain the login user name in the software, choose a different method according to the specific situation. If you are sure that your process must be started in the login user environment, then getusername, otherwise, you need to use the following two methods, of course, you need to determine the type of operating system before using.

This section of Baidu Quiz, to me related to AH!!! ----How to get the Windows system login user name

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.