Search code examples
c++windows-7windows-xpwindows-vista

C++ - GetUserName() when process is run as administrator


I have a simple C++ program that prompt the user name

#include <windows.h>
#include <Lmcons.h>
#include <winbase.h>

int _tmain(int argc, _TCHAR* argv[])
{
    wchar_t username[UNLEN + 1];
    DWORD username_len = UNLEN + 1;
    ::GetUserName(username, &username_len);

    MessageBox(NULL, username, NULL, 1);
    return 1;
}

GetUserName() performs as expected in administrator accounts, meaning print the real user name.

However, when run as administrator in a non-administrator account, I get the administrator name, and not the the real logged user.

I believe this behavior is expected since it is documented in GetUserName():
If the current thread is impersonating another client, the GetUserName function returns the user name of the client that the thread is impersonating.

Question

Is there a way to get the real logged in user (the non-admin one), even if the process run as administrator?


Solution

  • I believe the question you want to ask Windows is "which user is logged into the current session".

    To do this, call ProcessIdToSessionId() with your own process's ID to determine the current session ID.

    Then call WTSQuerySessionInformation() with the WTSUserName option to fetch the user name.