4
votes

I know that shutdown -a will abort a Windows shutdown, but I need to know if there is anything any where I can check for to see if a shutdown is in progress.

Ideally, I'd like a small program like this:

 import os

 while True:
    shuttingDown = <shutdown variable to check>
    if shuttingDown:
         os.system("shutdown.exe -a")
1
This sort of question suggests to me that you're trying to do something evil. Could you elaborate as to why you want to abort shutdown when the user has clearly indicated they want to shut down? - Greg D
windows 7 RC1 expires today and shuts down every 2 hours without saving. Trying to stop the shutdown. - user283898
@Kenneth: here's a novel idea: buy the software that you're using! - Joachim Sauer
In that case a hack like calling "shutdown.exe -a" every second or so might be enough to get you through the backup. - Joachim Sauer
Mark Ransom: They do, but it's totally unsupported. You do it at your own risk. This blog post shows how to do it: blogs.msdn.com/e7/archive/2009/04/07/… - ReinstateMonica Larry Osterman

1 Answers

1
votes

For preventing a Windows shutdown when it is happening, you can react to the WM_QUERYENDSESSION message (don't know if you can do that easily with Python's win32 API but it's simple in C). This might not prevent applications from closing because Windows sends WM_ENDSESSION to those that answer TRUE to the query message.

I guess you rather want to prevent a timed shutdown using "shutdown.exe". I'm sure that program uses InitiateSystemShutdown to show the shutdown dialog, but there are no resources on intercepting that call (at least I didn't find any or know of a Windows feature that allows such a thing).