本文介绍了你如何在 C# 中使用 LsaEnumerateLogonSessions?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# Windows 窗体应用程序中,我试图获取当前登录到工作站的用户(本地和域用户)的列表.关于这个的每次搜索都让我看到提到只需使用 LsaEnumerateLogonSessions".

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".

那么……你实际上如何使用它?MSDN 页面非常稀疏,似乎没有提供任何线索.

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

推荐答案

你应该使用 Cassia,一个开源包装器.

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);
    }
}

我不确定这将如何处理域用户;在 LINQPad 中尝试一下.

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

要回答您的问题,您需要将其声明为 P/Invoke 方法,该方法采用 out inout long[].

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

这篇关于你如何在 C# 中使用 LsaEnumerateLogonSessions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 02:29