0

In a C# Windows Forms Application, I'm trying to get a list of the users currently logged into a workstation (both local and domain users). Every search about this has led me to threads mentioning "just use LsaEnumerateLogonSessions".

So...how do you actually use this? The MSDN page is very sparse and doesn't seem to offer any clues.

Force Flow
  • 714
  • 2
  • 14
  • 34

1 Answers1

0

You should use Cassia, an open source wrapper.

ITerminalServicesManager manager = new TerminalServicesManager();
using (ITerminalServer server = manager.GetRemoteServer("your-server-name"))
{
    server.Open();
    foreach (ITerminalServicesSession session in server.GetSessions())
    {
        Console.WriteLine("Session ID: " + session.SessionId);
        Console.WriteLine("User: " + session.UserAccount);
        Console.WriteLine("State: " + session.ConnectionState);
        Console.WriteLine("Logon Time: " + session.LoginTime);
    }
}

I'm not sure how this will handle domain users; try it in LINQPad.

To answer your question, you need to declare it as a P/Invoke method that takes an out in and an out long[].

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Ok, I seem to have loaded the library. Looks like I'll be working with it for a little while to familiarize myself with it. As for your comment about P/Invoke...what do I do there? – Force Flow Apr 14 '11 at 22:35
  • If you want to call `LsaEnumerateLogonSessions` directly without Cassia, you'll need to learn P/Invoke first. – SLaks Apr 14 '11 at 23:28
  • 1
    Cassia doesn't use LsaEnumerateLogonSessions at all. It uses only WTSEnumerateSessions. – Johnny Jun 19 '13 at 09:18