Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than wtsapi32, prefix the name with the module name and a period.
WTSRegisterSessionNotification (wtsapi32)
.
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;
// 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.
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);
}
Used to unsubscribe to session related events, such as locking and unlocking the workstation
8/30/2018 1:04:32 PM - -67.71.231.162
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).