Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8144879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:29:50+00:00 2026-06-06T13:29:50+00:00

Making a simple application, so when the user logs out of Windows, it of

  • 0

Making a simple application, so when the user logs out of Windows, it of course shuts the application down. We are making a simple USB Alert application which STOPS shutdown if a USB is detected when the user is logging off

This is the code so far.

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;
            }
        }
    }

What is happening is that the Windows Force Programs to Quit screen is being displayed, I read somewhere if you return 0 to WM_QUERYENDSESSION it does not display this, but it is still displaying this…

Any ideas?

EDIT:

We used the code that someone responded with, but we are still getting this screen.

The screen we want to avoid!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:29:53+00:00Added an answer on June 6, 2026 at 1:29 pm

    Have you tried

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

    Should abort the shutdown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a small simple windows application. This is my main function: static void
I'm making a simple game using the Windows Phone Silverlight and XNA Application type
I am making a simple chat application and I am trying to find out
i am making a simple c#.net winform application with a form1, which will connect
I'm making a very simple application which has to perform an action when the
I have a simple web application which lets the user upload local user data
I'm making a simple winforms application in C# , with VS2010 . My winforms
I am making a simple javascript application where I need to load an xml
I'm making a simple client server chat application. I want to be able to
in making a simple cgi server for a course. To do that in some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.