C ++ - GetUserName () when a process starts as an administrator

I have a simple C ++ program that asks for a username

#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 () executes as expected in administrator accounts, i.e. prints the real username.

However, when running as an administrator in a non-administrator account> I get the name of the administrator, not the real registered user.

I believe this behavior is expected since it is documented in GetUserName () :
If the current thread represents another client, the GetUserName function returns the client username representing the stream.

Question

(-), ?

+4
2

, , Windows, - " ".

ProcessIdToSessionId() , .

WTSQuerySessionInformation() WTSUserName .

+7

, . . Windows , , admin.

, , GetUserName(), . .

, "", . , , , . , , , , , .

+4

All Articles