How to get windows session name?

Windows has cmd " request session , it displays session information on the terminal server, session_name".

I want to use the Windows API ------ LsaEnumerateLogonSessions and LsaGetLogonSessionData strong> to get this session information as follows:

int main() { int i = 0; ULONG count = 0; PLUID list = NULL; PSECURITY_LOGON_SESSION_DATA data; LsaEnumerateLogonSessions(&count, &list); for (i = 0; i < (int)count; i++) { LsaGetLogonSessionData(&list[i], &data); } return 0; } 

I can get the SECURITY_LOGON_SESSION_DATA structure:

 typedef struct _SECURITY_LOGON_SESSION_DATA { ULONG Size; LUID LogonId; LSA_UNICODE_STRING UserName; LSA_UNICODE_STRING LogonDomain; LSA_UNICODE_STRING AuthenticationPackage; ULONG LogonType; ULONG Session; PSID Sid; LARGE_INTEGER LogonTime; LSA_UNICODE_STRING LogonServer; LSA_UNICODE_STRING DnsDomainName; LSA_UNICODE_STRING Upn; ULONG UserFlags; LSA_LAST_INTER_LOGON_INFO LastLogonInfo; LSA_UNICODE_STRING LogonScript; LSA_UNICODE_STRING ProfilePath; LSA_UNICODE_STRING HomeDirectory; LSA_UNICODE_STRING HomeDirectoryDrive; LARGE_INTEGER LogoffTime; LARGE_INTEGER KickOffTime; LARGE_INTEGER PasswordLastSet; LARGE_INTEGER PasswordCanChange; LARGE_INTEGER PasswordMustChange; } SECURITY_LOGON_SESSION_DATA, *PSECURITY_LOGON_SESSION_DATA; 

But it does not contain the name of the session !

Any idea on how to get "sessonname"?

+4
source share
2 answers

The "session name" is called the "winstation name" in the API docs (very vague because it is definitely not Window Station, since docs apologize ).

WTSQuerySessionInformation (WTSWinStationName) will get it for you using standard WTS features.

I'm not sure why you are using LsaEnumerateLogonSessions because it will return not only sessions, but also any login, including network logins that did not create sessions. If you have any special reason for the need for a LUID, you will have to filter the list, select those where the session member is non-zero, and then do WTSQuerySessionInformation to get the name winstation. Or you can filter for all inputs where LogonType is one of four interactive types. It is also possible even in Vista and then for the console in session 0, and this is normal for XP, so remember also to check session 0 and WTSGetActiveConsoleSessionId to make sure that you include in your list all the sessions that you are interested in. This it will be much easier, I suspect that I am just using the WTSEnumerateSessions function!

+3
source

WTSEnumerateSessions or WTSQuerySessionInformation

0
source

All Articles