开发者

How do you use LsaEnumerateLogonSessions in C#?

开发者 https://www.devze.com 2023-02-26 06:15 出处:网络
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 \"

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 开发者_Python百科any clues.


This article shows how to use LsaEnumerateLogonSessons:
http://www.codeproject.com/Articles/18179/Using-the-Local-Security-Authority-to-Enumerate-Us


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[].

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号