代码之家  ›  专栏  ›  技术社区  ›  Christopher

使用c检查工作站锁定/解锁更改#

  •  10
  • Christopher  · 技术社区  · 17 年前

    How can I programmatically determine if my workstation is locked?

    4 回复  |  直到 9 年前
        1
  •  16
  •   Ben S    17 年前

    A. SessionSwitch 这次活动可能是你最好的选择。检查 SessionSwitchReason 通过了 SessionSwitchEventArgs

        2
  •  3
  •   Andrew Grant    17 年前

    您可以通过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

    http://bytes.com/groups/net-c/276963-trapping-when-workstation-locked

        3
  •  2
  •   Owen Johnson    12 年前

    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);
            }
        }
    }
    
        4
  •  -3
  •   henri4 henri4    17 年前

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

    推荐文章