Search code examples
windowsshutdown

Why do other applications still shutdown even when I return FALSE to the WM_QUERYENDSESSION message


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?


Solution

  • 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.