Windows exit detection in Python

How can I detect or be notified when windows exit the system in python?

Edit: Reply Martin v. Löwis is good and works for a complete logout, but it does not work for "quick user switching", for example, pressing win + L, for which I really need it.

Editing: im not using gui, this works as a service

+5
source share
2 answers

You can detect fast user switching events using the Terminal Services API, which you can access with Python using a module win32tsfrom pywin32 . In the GUI application, call WTSRegisterSessionNotification to receive notifications, WTSUnRegisterSessionNotification to stop receiving notifications and process the message WM_WTSSESSION_CHANGEin your window procedure.

If you are writing a Windows service in Python, use the function RegisterServiceCtrlHandlerExto detect fast user switch events. This is available in the pywin32 library as a function RegisterServiceCtrlHandlerin the module servicemanager. In for details on specific notification codes. WM_WTSSESSION_CHANGE

python-win32, .

, !

+6

win32api.SetConsoleCtrlHandler CTRL_LOGOFF_EVENT. GUI WM_QUERYENDSESSION. ( ) .

+3

All Articles