WM_QUERYENDSESSION给我带来了麻烦

x06*_*16e 6 c# wndproc

制作一个简单的应用程序,因此当用户退出Windows时,它当然会关闭应用程序.我们正在制作一个简单的USB警报应用程序,如果在用户注销时检测到USB,STOPS将关闭

到目前为止这是代码.

public Form1()
    {
        InitializeComponent();
    }

    private static int WM_QUERYENDSESSION = 0x11;
    private static bool systemShutdown = false;
    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        if (m.Msg == WM_QUERYENDSESSION)
        {
            //MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot");
            systemShutdown = true;
            m.Result = (IntPtr)0;
        }

        // If this is WM_QUERYENDSESSION, the closing event should be
        // raised in the base WndProc.
        m.Result = (IntPtr)0;
        base.WndProc(ref m);

    } //WndProc 

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (systemShutdown)
        {
            systemShutdown = false;
            bool hasUSB = false;

            foreach (DriveInfo Drive in DriveInfo.GetDrives())
            {
                if (Drive.DriveType == DriveType.Removable)
                {
                    hasUSB = true;
                }
            }

            if (hasUSB)
            {
                e.Cancel = true;
                MessageBox.Show("You still have USB device plugged in, please unplug it and log off again");
            }
            else
            {
                e.Cancel = false;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

发生了什么是正在显示Windows强制退出程序屏幕,我在某处读到如果你返回0到WM_QUERYENDSESSION它没有显示这个,但它仍然显示这个......

有任何想法吗?

编辑:

我们使用了某人回复的代码,但我们仍然在使用此屏幕.

我们想要避免的屏幕!

Bug*_*der 4

你有没有尝试过

[DllImport("advapi32.dll", SetLastError=true)]
static extern bool AbortSystemShutdown(string lpMachineName);
Run Code Online (Sandbox Code Playgroud)

应中止关闭。