简体   繁体   English

在Python中检测Windows注销

[英]Detect windows logout in Python

How can I detect, or be notified, when windows is logging out in python? 当我在python中注销Windows时,如何检测或通知?

Edit: Martin v. Löwis' answer is good, and works for a full logout but it does not work for a 'fast user switching' event like pressing win+L which is what I really need it for. 编辑:Martinv.Löwis的答案很好,并且可以完全注销,但它不能用于“快速用户切换”事件,例如按下win + L这是我真正需要的。

Edit: im not using a gui this is running as a service 编辑:我不使用gui这是作为服务运行

You can detect fast user switching events using the Terminal Services API, which you can access from Python using the win32ts module from pywin32 . 您可以使用终端服务API检测快速用户切换事件,您可以使用pywin32中win32ts模块从Python访问该API。 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. 在GUI应用程序中,调用WTSRegisterSessionNotification以接收通知消息, WTSUnRegisterSessionNotification以停止接收通知,并在窗口过程中处理WM_WTSSESSION_CHANGE消息。

If you're writing a Windows service in Python, use the RegisterServiceCtrlHandlerEx function to detect fast user switching events. 如果您使用Python编写Windows服务,请使用RegisterServiceCtrlHandlerEx函数来检测快速用户切换事件。 This is available in the pywin32 library as the RegisterServiceCtrlHandler function in the servicemanager module. 这在pywin32库中可用作servicemanager模块中的RegisterServiceCtrlHandler函数。 In your handler function , look for the SERVICE_CONTROL_SESSIONCHANGE notification. 处理程序函数中 ,查找SERVICE_CONTROL_SESSIONCHANGE通知。 See also the WM_WTSSESSION_CHANGE documentation for details of the specific notification codes. 有关特定通知代码的详细信息,另请参阅WM_WTSSESSION_CHANGE文档。

There's some more detail in this thread from the python-win32 mailing list, which may be useful. 来自python-win32邮件列表的这个帖子中有一些更详细的信息,这可能很有用。

I hope this helps! 我希望这有帮助!

In a console application, you can use win32api.SetConsoleCtrlHandler and look for CTRL_LOGOFF_EVENT. 在控制台应用程序中,您可以使用win32api.SetConsoleCtrlHandler并查找CTRL_LOGOFF_EVENT。 In a GUI application, you need a window open and wait for the WM_QUERYENDSESSION message. 在GUI应用程序中,您需要打开一个窗口并等待WM_QUERYENDSESSION消息。 How precisely that works (and if it works at all) depends on your GUI library. 它的工作原理(如果它可以工作)取决于您的GUI库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM