Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

WTSRegisterSessionNotification (wtsapi32)
 
.
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

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions