WTSRegisterSessionNotification (wtsapi32)
Last changed: anonymous

.
Summary
Can be used to register a window to receive session change notifications, such as when the machine is locked or unlocked.

C# Signature:

[DllImport("wtsapi32.dll", SetLastError=true)]
static extern bool WTSRegisterSessionNotification(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] int dwFlags);

VB Signature:

Declare Function WTSRegisterSessionNotification Lib "wtsapi32.dll" (ByVal hWnd as IntPtr, ByVal dwFlags as Int32) As Boolean

User-Defined Types:

None.

Notes:

    // constants that can be passed for the dwFlags parameter
    const int NOTIFY_FOR_THIS_SESSION = 0;
    const int NOTIFY_FOR_ALL_SESSIONS = 1;

    // message id to look for when processing the message (see sample code)
    const int WM_WTSSESSION_CHANGE = 0x2b1;

http://msdn2.microsoft.com/en-us/library/aa383828.aspx

    // WParam values that can be received:
    const int WTS_CONSOLE_CONNECT        = 0x1; // A session was connected to the console terminal.
    const int WTS_CONSOLE_DISCONNECT     = 0x2; // A session was disconnected from the console terminal.
    const int WTS_REMOTE_CONNECT         = 0x3; // A session was connected to the remote terminal.
    const int WTS_REMOTE_DISCONNECT      = 0x4; // A session was disconnected from the remote terminal.
    const int WTS_SESSION_LOGON          = 0x5; // A user has logged on to the session.
    const int WTS_SESSION_LOGOFF         = 0x6; // A user has logged off the session.
    const int WTS_SESSION_LOCK           = 0x7; // A session has been locked.
    const int WTS_SESSION_UNLOCK         = 0x8; // A session has been unlocked.
    const int WTS_SESSION_REMOTE_CONTROL = 0x9; // A session has changed its remote controlled status.

Tips & Tricks:

Use in conjuction with WTSUnRegisterSessionNotification

Sample Code:

    class SessionChangeHandler : Control // allows us to override WndProc
    {
        [DllImport("WtsApi32.dll")]
        private static extern bool WTSRegisterSessionNotification(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)]int dwFlags);
        [DllImport("WtsApi32.dll")]
        private static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd);

        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 SessionChangeHandler()
        {
            if (!WTSRegisterSessionNotification(this.Handle, NOTIFY_FOR_THIS_SESSION))
            {
                Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
            }
        }

        protected override void OnHandleDestroyed(EventArgs e)
        {
            // unregister the handle before it gets destroyed
            WTSUnRegisterSessionNotification(this.Handle);
            base.OnHandleDestroyed(e);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_WTSSESSION_CHANGE)
            {
                int value = m.WParam.ToInt32();
                if (value == WTS_SESSION_LOCK)
                {
                    OnMachineLocked(EventArgs.Empty);
                }
                else if (value == WTS_SESSION_UNLOCK)
                {
                    OnMachineUnlocked(EventArgs.Empty);
                }
            }
            base.WndProc(ref m);
        }

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

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

Alternative Managed API:

Do you know one? Please contribute it!

Documentation