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 7020059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:16:06+00:00 2026-05-27T23:16:06+00:00

I have an application that I’d like to shutdown gracefully when Windows shuts down

  • 0

I have an application that I’d like to shutdown gracefully when Windows shuts down (or user logs off). This used to work (in xp) but sometime in the last year it broke without anyone noticing. It’s also broke (but differently) under Windows 7.

Our product has a main process (server.exe) that starts many other processes. A graceful shutdown would have server.exe asking all of the process that it starts to shut down. However, when I debug this code it seems that the other processes have already been terminated. Our main process (server.exe) is the only process that is handling the WM_QUERYENDSESSION and the WM_ENDSESSION messages. Code below (this used to work under XP but doesn’t any more):

LRESULT CALLBACK master_wnd_proc
(
   HWND hwnd,      /* (in) handle to window */
   UINT uMsg,      /* (in) message identifier */
   WPARAM wParam,  /* (in) first message parameter */
   LPARAM lParam   /* (in) second message parameter */
)
{
   LRESULT result;   /* return value */
   long msg_code;

   switch (uMsg)
   {
      case WM_ENDSESSION:
         if (wParam)
         {
            msg_code = PCS_WINDOWS_SHUTDOWN;
            if( lParam & 0x01L )
               msg_code = WINDOWS_SHUT_CLOSE;
            if( lParam & 0x40000000L )
               msg_code = WINDOWS_SHUT_CRIT;
            if( (unsigned long)lParam & 0x80000000 )
               msg_code = WINDOWS_SHUT_LOGOFF;
            MsgGenerate(msg_code, MSG_SEVERE, MSG_LOG, "");

            ipc_declare_shutdown( msg_code );

            //We need one more message in the message queue
            //to force the message loop, below, to exit.
            PostQuitMessage(EXIT_SUCCESS);

            /* WARNING:  Don't call MsgGenerate() after this point! */
         }
         result = 0;
         break;

      case WM_QUERYENDSESSION:

         /* return TRUE to say "okay to shutdown"
          * If FALSE is returned, then other processes are not stopped
          * and the session isn't ended.
          */
         result = TRUE;
         break;

      /* for a Windows TIMER or for an IPC prompt, handle
       * the old server code and tcall messages and
       * once-per-second work.  Notice that the
       * once-per-second work could just be done on the WM_TIMER
       * and the tcall work could just be done on the WM_APP_IPC_POSTED
       * but I've merged them together here.  The merge isn't
       * necessary to fix a bug or anything, but rather to
       * make the code more robust in the face of unexpected
       * conditions.
       */
      case WM_TIMER:
      case WM_APP_IPC_POSTED:
         /* now handle tcall messages */
         (void) server();

         result = FALSE;
         break;

      default:
         result = DefWindowProc (hwnd, uMsg, wParam, lParam);
         break;
   }

   return result;
}

It seems as though we’ve changed something in the last year the would require all the child processes to handle the WM_QUERYENDSESSION message (I’d really like to avoid this). I don’t seem to be able to find any any information as to when processes do or don’t get this message.

I’ve make it work under Windows 7 using the new API but would like to figure out why it’s broke under XP so I can have a solution that works for both OS’s.

Any help?

  • 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-05-27T23:16:07+00:00Added an answer on May 27, 2026 at 11:16 pm

    Things changed around Vista time, not so sure how that would affect your code. The best thing to do is to not leave it up to Windows to determine the shutdown order. Just ask it to have your server get the shutdown notification before the helper processes:

       DWORD dwLevel, dwFlags;
       BOOL fOkay = GetProcessShutdownParameters(&dwLevel, &dwFlags);
       ASSERT(fOkay);
       if (fOkay && dwLevel > 0x100) {
           fOkay = SetProcessShutdownParameters(dwLevel + 1, SHUTDOWN_NORETRY);
           ASSERT(fOkay);
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that writes its own stylesheet, based off user input. It
I have an application that displays an image inside of a Windows Forms PictureBox
I have an application that I would like to embed inside our companies CMS.
I have a application that like firefox, can be enhanced from plugins available from
I have application that produces files. I would like to connect those files with
I have application that look like below without spring (prior) UI-> service --> javabean
i have application that generate php/html/asp web sites and i like to be able
I have application that has to check multiple data from user table, in order
I have application that have button i want when click this button every 1
We have an application that stores a sparse matrix. This matrix has entries that

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.