I would recommend that you work with an object from a class, and using def 
__del__(self):
I think when Windows shutdown, it send a stop/quit signal to all processes. 
When your Python is stopped by the OS, it can stop gracefully, and write the 
info in the file to backup, before it dies. This could work whatever the 
interruption reason, whatever the OS. As this is called everytime the object is 
destructed/deleted.

About the events to listen to, when the system shutdowns and sends a signal, 
you can read this 
(https://www.apriorit.com/dev-blog/413-win-api-shutdown-events).
There's a note about the fact that they may not work correctly. This happens 
when the OS calls internal console cleanup routines before executing the 
process signal handler.

One alternative also can be to create a fake window and catch 
WM_QUERYENDSESSION and/or WM_POWERBROADCAST events, so you turn your 
application into a GUI app for Windows point of view, and you rely on possibly 
more reliable events.
Regards,
_____________________________________
Antoine FERRON
Président — BitLogiK

bitlogik.fr (https://bitlogik.fr) — PGP Key ID#22F95B31 
(https://keys.openpgp.org/search?q=antoine.ferron%40bitlogik.fr)
On Mar 9 2022, at 11:20 am, H.Fujii <hisab...@gmail.com> wrote:
> I'm currently developing a console program in Python 3.9.
> I want to detect the shutdown of Windows and save the data to a file safely.
> I created a sample program by referring to the following URL.
> https://docs.microsoft.com/en-us/windows/console/registering-a-control-handler-function
>
> However, the sample program does not detect shutdown
> (CTRL_SHUTDOWN_EVENT) well.
> I have confirmed that CTRL_C_EVENT and CTRL_CLOSE_EVENT can be detected
> successfully.
> Please let me know how to detect a successful shutdown.
>
> Sincerely,
>
> Expected behavior and actual behavior.
> detecting the shutdown of Windows
>
> Steps to reproduce the problem.
> python mian.py
>
> Version of Python and pywin32
> Python 3.8.11
> pywin32 version 228
>
>
> # Python Code : main.py
> import time
> import win32api
> import win32con
>
> def ctrl_handler(evt):
> with open('event_info.txt', 'a') as f:
> if evt == win32con.CTRL_C_EVENT:
> f.write("CTRL_C_EVENT\n")
> return True
> elif evt == win32con.CTRL_CLOSE_EVENT:
> f.write("CTRL_CLOSE_EVENT\n")
> return True
> elif evt == win32con.CTRL_BREAK_EVENT:
> f.write("CTRL_BREAK_EVENT\n")
> return False
> elif evt == win32con.CTRL_LOGOFF_EVENT:
> f.write("CTRL_LOGOFF_EVENT\n")
> return False
> elif evt == win32con.CTRL_SHUTDOWN_EVENT:
> f.write("CTRL_SHUTDOWN_EVENT\n")
> return False
> else:
> return False
>
> def main():
> print('Starting Program!')
> win32api.SetConsoleCtrlHandler(ctrl_handler, True)
> while True:
> time.sleep(1)
> print('End of Program!')
>
> if __name__ == "__main__":
> main()
> _______________________________________________
> python-win32 mailing list
> python-win32@python.org
> https://mail.python.org/mailman/listinfo/python-win32
>

_______________________________________________
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32

Reply via email to