Detect windows logout in Python

后端 未结 2 934
梦毁少年i
梦毁少年i 2021-02-06 12:41

How can I detect, or be notified, when windows is logging out in python?

Edit: Martin v. Löwis\' answer is good, and works for a full logout but it does not work for a \

相关标签:
2条回答
  • 2021-02-06 13:02

    In a console application, you can use win32api.SetConsoleCtrlHandler and look for CTRL_LOGOFF_EVENT. In a GUI application, you need a window open and wait for the WM_QUERYENDSESSION message. How precisely that works (and if it works at all) depends on your GUI library.

    0 讨论(0)
  • 2021-02-06 13:08

    You can detect fast user switching events using the Terminal Services API, which you can access from Python using the win32ts module from pywin32. In a GUI application, call WTSRegisterSessionNotification to receive notification messages, WTSUnRegisterSessionNotification to stop receiving notifications, and handle the WM_WTSSESSION_CHANGE message in your window procedure.

    If you're writing a Windows service in Python, use the RegisterServiceCtrlHandlerEx function to detect fast user switching events. This is available in the pywin32 library as the RegisterServiceCtrlHandler function in the servicemanager module. In your handler function, look for the SERVICE_CONTROL_SESSIONCHANGE notification. See also the WM_WTSSESSION_CHANGE documentation for details of the specific notification codes.

    There's some more detail in this thread from the python-win32 mailing list, which may be useful.

    I hope this helps!

    0 讨论(0)
提交回复
热议问题