Win2K Forces Shutdown

I have a couple of Services that pop up Yes/No MessageBox() in response to a WM_QUERYENDSESSION message. Windows 2000 appears to use the ExitWindowsEx() api function with a EWX_FORCEIFHUNG option, which thinks my Services are hung and shuts down without waiting for a response. This happens when logging off or shutting down.
Does anyone know anyway to change this? A registry entry etc...? Or a way around it? Thanks!
Who is Participating?
JanvoConnect With a Mentor Commented:
Andrew, the WM_QUERYENDSESSION message returns asyncronously, meaning that this message is sent, and then it shuts the process down, if it is hung or not.  If you want to keep this from happening you must respond to the SERVICE_CONTROL_SHUTDOWN message inside the callback handler for your service, and update the SERVICE_STATUS using
::SetServiceStatus(SERVICE_STATUS_HANDLE h, LPSERVICE_STATUS lh).  You must keep the SCM (Service Control Manager) aware of the service's status by constantly updating this status.  You do this by updating the dwCheckPoint member of the LPSERVICE_STATUS structure in your callback handler routine.  This will allow you to notify the SCM of any change in the status of a lengthy start, stop, pause, continue operation and is needed to prevent the SCM from timing out and shutting down the process immediately.  Follow the guide for SetServiceStatus in the MSDN library, or feel free to contact me if you need any more help.
Good luck!
You say that W2K calls the ExitWindowsEx(EWX_FORCE) API to restart the computer. The only way to interrupt the restart is to click "Don't restart now" when you are prompted.
If you recieve this message.

But I think that you must work on the program that pop/up the message box.

AndrewRAuthor Commented:
Sorry, I should have said it looks like ExitWindowsEx(EWX_FORCEIFHUNG) so I don't get prompted to "Don't restart now". Management wants a reminder to come up, when shutting down the machine, that our services are running. So I am looking for a work around that will still allow this feature...
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Asta CuCommented:

PRB: Top Level Window Does Not Receive WM_ENDSESSION Messages

The information in this article applies to:

Microsoft Win32 Software Development Kit (SDK)
on the following platforms: NT
Microsoft Windows 2000

Control-by-Control Validation in MFC


Press these keys (keyboard) in this order:


Any entries that come up you should see about editing or even deleting. I'm not sure what to actually do with them though.

I know it's basic, but hey, you never know!


  Have you considered making a special
shortcut with the exact parameters you want, in this form:

C:\WINDOWS\RUNDLL.EXE user.exe,exitwindows

  Of course, you'd need to OR the options manually.

  If that doesn't fit the necessary usage, maybe the best way would be to make an small app that accepted the wm_QueryEndSession message, refused to end (by returning false), and then remind the user that services are running.

   It could also offer to shutdown again and/or timeout after a while and let the shutdown happen this time.
AndrewRAuthor Commented:
I am afraid this wouldn't be a satifactory solution on our customers pc's. I really need to find any operating system parameters that will let me change this behavior.
Another Suggestion...

I realise it invloves multiple threads, however...

One thread of your service is 'continually running' (some timer type loop) so as not to appear hung, then have another pop up your messages.
Then in the first loop, jump out based on the users response.

Or Another suggestion:
-Have the loop activate ONLY when wm_QueryEndSession is received.


When the user responds, kill the T1 thread. (I don't believe the is an order precedence involved so you could IN THEORY start the loop second)

To quote the help file for the ExitWindowEx function:

"The ExitWindowsEx function sends a separate notification message, CTRL_SHUTDOWN_EVENT or CTRL_LOGOFF_EVENT as the situation warrants, to console processes. A console process routes these messages to its HandlerRoutine functions, which are added and removed by calls to the SetConsoleCtrlHandler function. ExitWindowsEx sends these notification messages asynchronously; thus, an application cannot assume that the console notification messages have been handled when a call to ExitWindowsEx returns."

So, I think your services need to install handlers and look out for the CTRL_SHUTDOWN_EVENT and CTRL_LOGOFF_EVENT messages.  I believe these are issued synchronously.
If the user logoff, your service don´t is shutdown, it´s continue running !!!
(I do this and works)
AndrewRAuthor Commented:
I will attempt this latest suggestion, but might take a little while, have to finish something else first. Thanks
Right! Answer me about the news !
AndrewRAuthor Commented:
leandrobecker, I realize if the user is just logging off the service will stay running, but this didn't seem to work correctly (the WM_QUERYENDSESSION message logoff parameter) in the versions of NT i first developed this for. What news do you want an answer to?
Any progress what you reach.

You desire to receive the message
WM_QUERYENDSESSION and this is does´t
happen ?

I don´t know about this, but I made a test with my service. The test is:

When the computer is logged off, I try  
to show a window and the window do not was showed but when I logged on the window appears !!!
leandrobecker, as far as I can see, he does not want to receive the WM_QUERYENDSESSION.  This message is sent by the SCM when the service is to be shutdown, if the service does not respond in time, the SCM will shutdown the service forcefully.  The WM_QUERYENDSESSION message is sent asyncronously and therefore the SCM must be updated as to the status of the service by calling SetServiceStatus.... Look at my proposed answer for more details - I'm sure that this will work.  The SetServiceStatus will have to be called within a timer loop while the message box is popped up in order to keep the service from being shutdown by the SCM.
AndrewRAuthor Commented:
I added the thread that keeps updating the SCM on our status while waiting for the MessageBox( MB_OK ) to be answered.
I also had to add the MB_DEFAULT_DESKTOP_ONLY flag to my MessageBox() call for this to work correctly. Not clear on why this is so, but am very happy to see it work.

Sorry it took so long for me to get back onto this problem.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.