How to get the Windows System login username

Source: Internet
Author: User
Tags bool thread

Generally use the GetUserName (or GetUserNameEx) function to get the current login login username (but not always, the following analysis), this system function in the Win95, WinNT and later all 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 exact number of this letter is the user name of the current thread (MSDN: Retrieves the "user name"). If the NT service (NT Server) starts this process, the result is the user name of the NT service process, that is, "SYSTEM" rather than the login username; Similarly, if this process was created through CreateProcessAsUser, The user that GetUserName gets will be the user name of "Asuser". In addition, if the current line one thread impersonate other user environments (which can be achieved with a function impersonateloggedonuser), it gets the other user names. Therefore, this function can only get the login username in a specific environment.

How do you get the login username exactly because the process itself is different from the operating environment?

Let's take a look at the Windows XP operating system, which provides a wtsquerysessioninformation function that gets information about sessions (session), one of which is to get the login user for 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;
}

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.