views:

884

answers:

2

I want to be able to detect and abort OS shutdown from my application, preferably by using the Windows API. I know that it is possible to do this manually using the command shutdown -a In the worst case, I could ShellExecute this, but I was wondering if there was a better way.

Also, how do I find out programatically that the OS is about to shut down, via the Win32 API?

+6  A: 

From MSDN:

The WM_QUERYENDSESSION message is sent when the user chooses to end the session or when an application calls one of the system shutdown functions. If any application returns zero, the session is not ended. The system stops sending WM_QUERYENDSESSION messages as soon as one application returns zero.

So, my application's WindowProc now processes the WM_QUERYENDSESSION message and returns 0.

Didn't expect it to be this simple; as a bonus, it also works on Windows 2000.

Piskvor
Nice, this doesn't have to be community wiki by the way. :)
BobbyShaftoe
Well, it is correct *enough* for me. There may be some quirks or advanced operations for this message, and CW allows more people to edit.
Piskvor
+1  A: 

In regards to 'simply' returning 0 to block a shutdown, it isn't as simple as that if you want to do it in the proper way. Especially on Vista. For example please also read http://msdn.microsoft.com/en-us/library/ms700677(VS.85).aspx

CookieRevised
The article seems to say "it's much more complicated on Vista: Applications that must block shutdown should use the new shutdown reason API." Never in the entire article does it mention a "more proper" way to handle this on XP. Did I miss something?
Piskvor