使用C#检测工作站锁定/解锁变化

10

这个问题的答案可能为您提供一个起点。 https://dev59.com/g3VD5IYBdhLWcg3wO5AD - Timothy Carter
感谢大家,你们一如既往地非常有帮助 :) - Christopher
4个回答

20

3

-1:这个的托管代码版本在 Win32 版本发布之前就已经发布了...为什么这甚至被标记为正确答案? - Powerlord
2
在我看来,pinvoke 是在使用 .NET 时的最后一种解决方案。最好先使用框架内置的方法。 - Powerlord
1
我并不反对,只是不知道有另一种 .net 方法。但我不明白为什么你们这些哭哭啼啼的人会把这当作开始点踩的理由。 - Andrew Grant

2

您可以使用ComponentDispatcher作为获取这些事件的另一种方式。

以下是一个示例类来包装它。

public class Win32Session
{
    private const int NOTIFY_FOR_THIS_SESSION = 0;
    private const int WM_WTSSESSION_CHANGE = 0x2b1;
    private const int WTS_SESSION_LOCK = 0x7;
    private const int WTS_SESSION_UNLOCK = 0x8;

    public event EventHandler MachineLocked;
    public event EventHandler MachineUnlocked;

    public Win32Session()
    {
        ComponentDispatcher.ThreadFilterMessage += ComponentDispatcher_ThreadFilterMessage;
    }

    void ComponentDispatcher_ThreadFilterMessage(ref MSG msg, ref bool handled)
    {
        if (msg.message == WM_WTSSESSION_CHANGE)
        {
            int value = msg.wParam.ToInt32();
            if (value == WTS_SESSION_LOCK)
            {
                OnMachineLocked(EventArgs.Empty);
            }
            else if (value == WTS_SESSION_UNLOCK)
            {
                OnMachineUnlocked(EventArgs.Empty);
            }
        }
    }

    protected virtual void OnMachineLocked(EventArgs e)
    {
        EventHandler temp = MachineLocked;
        if (temp != null)
        {
            temp(this, e);
        }
    }

    protected virtual void OnMachineUnlocked(EventArgs e)
    {
        EventHandler temp = MachineUnlocked;
        if (temp != null)
        {
            temp(this, e);
        }
    }
}

-3

你绝对不需要使用WM_WTSSESSION_CHANGE,只需使用内部WTTS API即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接