Search code examples
c++sessionvisual-c++processwindows-xp

How to find user name from the session ID?


I have an user win32 application . That enumerates all the process details along with ProcessId. I need to print the User name along with the session ID. SessionID is enumerated using ProcessIdToSessionId() method. I want to print the User name of corresponding SessionID. This application is specially designed for WindowsXP , so the API should support WindowsXP. If anyone knows the solution please share it.

DWORD dwSessionId,dwPid,dwErr;
char* Uname;
ProcessIdToSessionId(dwPid,&dwSessionId);

Solution

  • WTSQuerySessionInformation() with the WTSInfoClass parameter set to WTSUserName:

    A null-terminated string that contains the name of the user associated with the session.