Get logged on user's name or email on Windows

2020-05-03 10:14发布

On Windows 7 to retrieve the name of a logged on user I can do this:

LPTSTR pUserName = NULL;
DWORD dwcbSzUserName = 0;

//'dwSessID' = user session ID
if(WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessID, WTSUserName, &pUserName, &dwcbSzUserName))
{
    //Got user name in 'pUserName'
}

if(pUserName)
    WTSFreeMemory(pUserName);

But on Windows 8 it returns some abbreviated name, for instance, "john_000" when the actual user's name is "John A. Doe".

So what is the way to retrieve the name of the logged on user (and possibly their email) on Windows 8 with C++ using WinAPIs as it's shown at log-on screen?

1条回答
Animai°情兽
2楼-- · 2020-05-03 10:51

You could try NetUserGetInfo with USER_INFO_23 to get full name.

Something basically like:

    //Got user name in 'pUserName'
    NetUserGetInfo(NULL, pUserName, 23, my_USER_INFO_23);
    //Got display name in my_USER_INFO_23.usri23_full_name
查看更多
登录 后发表回答