, Windows API " ", WTSRegisterSessionNotification WTSUnRegisterSessionNotification. , Windows , , , .
: http://support.microsoft.com/kb/310153
:
private const int NOTIFY_FOR_THIS_SESSION = 0x0;
private const int WM_WTSSESSION_CHANGE = 0x2B1;
private const int WTS_CONSOLE_CONNECT = 0x1;
private const int WTS_CONSOLE_DISCONNECT = 0x2;
private const int WTS_SESSION_LOCK = 0x7;
private const int WTS_SESSION_UNLOCK = 0x8;
private const int WM_DESTROY = 0x2;
private const int WM_ACTIVATEAPP = 0x1C;
[DllImport("Wtsapi32.dll")]
private static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd);
[DllImport("Wtsapi32.dll")]
private static extern bool WTSRegisterSessionNotification(IntPtr hWnd, Int32 dwFlags);
[DllImport("user32.dll")]
private static extern void PostQuitMessage(Int32 nExitCode);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr PostMessage(HandleRef hwnd, int msg, int wparam, int lparam);
private void Unsubscribe() {
if (this.Handle != IntPtr.Zero) {
WTSUnRegisterSessionNotification(this.Handle);
}
}
private void Subscribe() {
if (this.Handle != IntPtr.Zero) {
WTSRegisterSessionNotification(this.Handle, NOTIFY_FOR_THIS_SESSION);
}
}
protected override void WndProc(ref Message m) {
switch (m.Msg) {
case WM_WTSSESSION_CHANGE:
switch (m.WParam.ToInt32()) {
case WTS_CONSOLE_CONNECT:
break;
case WTS_CONSOLE_DISCONNECT:
break;
case WTS_SESSION_LOCK:
break;
case WTS_SESSION_UNLOCK:
break;
default:
break;
}
break;
default:
break;
}
base.WndProc(ref m);
}