views:

83

answers:

2

I have small app with a Window that is listening for the WM_QUERYENDSESSION message and returning FALSE - which should tell Windows that I don't want it to shutdown (see this MSDN link).

What I have found is that even though I am asking Windows to not shutdown, and Windows is itself not shutting down, it is still sending the WM_ENDSESSION message and closing other applications that are running alongside my own.

Does anyone know why this is happening and what can be done so that my application also prevents other applications from shutting down?

+1  A: 

Please look into this msdn article: http://msdn.microsoft.com/en-us/library/aa376890(VS.85).aspx

Basically, it really depends on what each application do with the WM_QUERYENDSESSION and the WM_ENDSESSION messages. As stated in the above article, WM_ENDSESSION messages are sent no matter what with the results of the WM_QUERYENDSESSION messages. Many applications decide to shutdown no matter the result.

There is also many application that start their shutdown process right after receiving the WM_QUERYENDSESSION to give them more time to shutdown before the dreaded "this application is not responding..." dialog

Hope this clarifies things a bit

MLefrancois
A: 

I have found that the order in which I start my application and other applications on the system affects which ones are shutdown and which are not.

A bit more research uncovered a system call to SetProcessShutdownParameters(). Using this call I can place my application at the front of the list of processes to be asked about shutting down Windows and so prevent the other applications from ever getting the WM_ENDSESSION message.

So to summarize:

When a shutdown event occurs (shutdown, restart or log off), Windows sends out a WM_QUERYENDSESSION message to each application in turn.

If an application doesn't object to the shutdown (they return TRUE), they are then sent a WM_ENDSESSION message.

As soon as one application returns FALSE to the WM_QUERYENDSESSION the shutdown is aborted, and no further messages are sent out.

Tim