使用 c# 检查工作站锁定/解锁更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/603484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking for workstation lock/unlock change with c#
提问by
DUPLICATE:How can I programmatically determine if my workstation is locked?
How can I detect (during runtime) when a Windows user has locked their screen (Windows+L) and unlocked it again. I know I could globally track keyboard input, but is it possible to check such thing with environment variables?
我如何检测(在运行时)Windows 用户何时锁定了他们的屏幕 (Windows+L) 并再次解锁。我知道我可以全局跟踪键盘输入,但是是否可以使用环境变量检查此类内容?
采纳答案by Andrew Grant
You can get this notification via a WM_WTSSESSION_CHANGE message. You must notify Windows that you want to receive these messages via WTSRegisterSessionNotification and unregister with WTSUnRegisterSessionNotification.
您可以通过 WM_WTSSESSION_CHANGE 消息获得此通知。您必须通过 WTSRegisterSessionNotification 通知 Windows 您希望接收这些消息,并使用 WTSUnRegisterSessionNotification 取消注册。
These posts should be helpful for a C# implementation.
这些帖子应该对 C# 实现有帮助。
http://pinvoke.net/default.aspx/wtsapi32.WTSRegisterSessionNotification
http://pinvoke.net/default.aspx/wtsapi32.WTSRegisterSessionNotification
http://blogs.msdn.com/shawnfa/archive/2005/05/17/418891.aspx
http://blogs.msdn.com/shawnfa/archive/2005/05/17/418891.aspx
http://bytes.com/groups/net-c/276963-trapping-when-workstation-locked
http://bytes.com/groups/net-c/276963-trapping-when-workstation-locked
回答by Ben S
A SessionSwitchevent may be your best bet for this. Check the SessionSwitchReasonpassed through the SessionSwitchEventArgsto find out what kind of switch it is and react appropriately.
一个SessionSwitch事件可能是这个你最好的选择。检查通过SessionSwitchEventArgs传递的SessionSwitchReason以找出它是哪种开关并做出适当的反应。
回答by Andrew Grant
You absolutely don't need WM_WTSSESSION_CHANGE Just use internal WTTS apis.
您绝对不需要 WM_WTSSESSION_CHANGE 只需使用内部 WTTS api。
回答by Owen Johnson
You can use ComponentDispatcheras an alternative way to get those events.
您可以将其ComponentDispatcher用作获取这些事件的替代方法。
Here's an example class to wrap that.
这是一个包装它的示例类。
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);
}
}
}

