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