8

I am trying to find a way to get a list of Windows sessions? I need the same information as the one displayed in the Task Manager on the User tab. I need to know if the user is active or not and if s/he is logged on in the Remote Desktop session.

Any idea on how to do that with C# / Windows XP Pro?

rae1
  • 6,066
  • 4
  • 27
  • 48
Martin
  • 39,309
  • 62
  • 192
  • 278

5 Answers5

12

As a starting point you can get a list of users logged on by running the command

qwinsta

From the command prompt.

This will give output like

C:\WINDOWS\system32>qwinsta
SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
>console           me                       0  Active  wdcon
rdp-tcp                                 65536  Listen  rdpwd

and will list any remote sessions.

pjp
  • 17,039
  • 6
  • 33
  • 58
5

Use LsaEnumerateLogonSessions via P/Invoke. You'll also need LsaFreeReturnBuffer to cleanup after enumerating.

Kevin Montrose
  • 22,191
  • 9
  • 88
  • 137
4

I believe you'll need to use P/Invoke to retrieve this information.

The relevant APIs are documented in this MSDN page.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
3

Another way is to use the Logonsessions utility from Sysinternals:

http://technet.microsoft.com/en-us/sysinternals/bb896769.aspx

xpmatteo
  • 11,156
  • 3
  • 26
  • 25
2

You do not need to use Pinvoke. WMI does it, and well: "select Name, SessionId from Win32_Process" in the root\cimv2 namespace. And, it can be called from a remote machine. Simpler. Add in a where clause in the select to fine tune what you get back.

JRrelyea
  • 139
  • 1
  • 4
  • This does not return the same information as on the Task Manager on the User tab. It returns an integer identifier of the session id, not the username or SID of the account under which the process was created. – codekaizen Dec 29 '17 at 03:53