Hello all,
I have a C# application, I want my application postponing the Windows Switch User by a MessageBox and wait for a response from the MessageBox. If my application is not ready for Switch User then I cancel the Windows Switch User.
I have tried WTSRegisterSessionNotification() and receive WM_WTSSESSION_CHANGE message in method WndProc() as following:
[DllImport("WtsApi32.dll")]
public static extern bool WTSRegisterSessionNotification(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)]int dwFlags);
[DllImport("WtsApi32.dll")]
public static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd);
WTSRegisterSessionNotification(this.Handle, 1); // all sessions
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case Unmanaged.Constants.WM_WTSSESSION_CHANGE: // 0x02B1
{
try
{
int wParamValue = m.WParam.ToInt32();
File.AppendAllText(@"d:\temp\logoff.txt", wParamValue.ToString());
if (wParamValue == Unmanaged.Constants.SESSION_LOCK)// SESSION_LOCK = 0x7;
{
DialogResult dialogResult = MessageBox.Show(this,
"Do you really want to switch user?",
"title", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.No)
{
// cancel switch user
return;
}
}
}
catch (Exception exc)
{
break;
}
break;
}
}
base.WndProc(ref m);
}
My issues:
- in the WndProc(): the value of wParamValue is only 1 or 2 when I switch user. It should be 0x7 as MSDN mentioned.
- I received the WM_WTSSESSION_CHANGE message but Windows still Switch User (MessageBox is not shown). It seems WM_WTSSESSION_CHANGE is sent after Windows switching user.
Is there any solution to postpone and cancel Windows Switch User?
Thanks, Thuan