private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (OnSessionSwitch == null)
            {
                return(IntPtr.Zero);
            }

            // handler of console display state system event
            if (msg == WM_POWERBROADCAST)
            {
                if (wParam.ToInt32() == PBT_POWERSETTINGCHANGE)
                {
                    var s = (POWERBROADCAST_SETTING)Marshal.PtrToStructure(lParam, typeof(POWERBROADCAST_SETTING));
                    if (s.PowerSetting == GUID_CONSOLE_DISPLAY_STATE)
                    {
                        Microsoft.Win32.SessionSwitchReason reason = new Microsoft.Win32.SessionSwitchReason();

                        switch (s.Data)
                        {
                        case (0x0):
                            reason = Microsoft.Win32.SessionSwitchReason.SessionLock;
                            break;

                        case (0x1):
                        case (0x2):
                            reason = Microsoft.Win32.SessionSwitchReason.SessionUnlock;
                            break;
                        }

                        Microsoft.Win32.SessionSwitchEventArgs args = new Microsoft.Win32.SessionSwitchEventArgs(reason);
                        OnSessionSwitch(this, args);
                    }
                }
            }

            return(IntPtr.Zero);
        }
Example #2
0
 public SessionSwitchEventArgs(Microsoft.Win32.SessionSwitchReason reason)
 {
 }