使用c#检查工作站锁定/解锁更改
-
03-07-2019 - |
题
DUPLICATE: 如何以编程方式确定我的工作站是否已锁定?
如何在Windows用户锁定屏幕(Windows + L)并再次解锁时检测(在运行时)。我知道我可以全局跟踪键盘输入,但是可以用环境变量来检查这个吗?
解决方案
您可以通过WM_WTSSESSION_CHANGE消息获取此通知。您必须通过WTSRegisterSessionNotification通知Windows您要接收这些消息,并使用WTSUnRegisterSessionNotification取消注册。
这些帖子应该对C#实现有所帮助。
http://pinvoke.net/default.aspx/wtsapi32.WTSRegisterSessionNotification
http://blogs.msdn.com/shawnfa /archive/2005/05/17/418891.aspx
其他提示
SessionSwitch 活动可能是您最好的选择为了这。检查通过 SessionSwitchReason http://msdn.microsoft.com/en-us/library/microsoft.win32.sessionswitcheventargs.aspx"rel =“noreferrer”> SessionSwitchEventArgs 找出它是什么样的开关并做出适当的反应。
您可以使用 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);
}
}
}
你绝对不需要WM_WTSSESSION_CHANGE 只需使用内部WTTS apis。
不隶属于 StackOverflow