Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to abort shutdown in Windows (XP|Vista) programatically?

I want to be able to 1. detect (and, if needed 2. abort) OS shutdown from my application, preferably by using the Windows API.

I know that it is possible to abort shutdown manually using the command shutdown -a In the worst case, I could ShellExecute this, but I was wondering if there was a better way to prevent the shutdown programatically.

Maybe it would be enough to be notified programatically that the OS is about to shut down - how to do this?

like image 393
Piskvor left the building Avatar asked Feb 26 '09 10:02

Piskvor left the building


2 Answers

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.

like image 79
2 revs Avatar answered Nov 17 '22 13:11

2 revs


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

like image 41
CookieRevised Avatar answered Nov 17 '22 15:11

CookieRevised