开发者

C# application postpones Win7 switch user

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:

  1. in the WndProc(): the value of wParamValue is only 1 or 2 when I switch user. It should be 0x7 as MSDN mentioned.
  2. 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


As MSDN mentiones, this messages is a notification message sent after the operation for your convenience, not in advance so that you can prevent it from happening.

Preventing a user from doing a switch seems explicitly malicious to me and I would be surprised to know Windows supports that in a form other than a group policy. The Windows team learned their lessons the hard way. You can't even cancel system stand by mode any more because the feature has been abused a lot.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜