问题描述
当 windows 在 python 中注销时,我如何检测或得到通知?
How can I detect, or be notified, when windows is logging out in python?
Martin v. Löwis 的回答很好,适用于完全注销,但不适用于快速用户切换"事件,例如按 win+L,这正是我真正需要的.
我没有使用作为服务运行的 gui
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.
im not using a gui this is running as a service
推荐答案
您可以使用终端服务 API 检测快速用户切换事件,您可以使用 win32ts 模块从 Python 访问它href="http://sourceforge.net/projects/pywin32/" rel="noreferrer">pywin32.在 GUI 应用程序中,调用 WTSRegisterSessionNotification 以接收通知消息,WTSUnRegisterSessionNotification 停止接收通知,并处理 WM_WTSSESSION_CHANGE
窗口过程中的消息.
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.
如果您使用 Python 编写 Windows 服务,请使用 RegisterServiceCtrlHandlerEx
函数检测快速用户切换事件.这在 pywin32 库中作为 servicemanager
模块中的 RegisterServiceCtrlHandler
函数可用.在您的处理程序函数中,查找SERVICE_CONTROL_SESSIONCHANGE
通知.另请参阅 WM_WTSSESSION_CHANGE
文档,了解有关具体的通知代码.
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.
此主题中有更多详细信息,来自python-win32 邮件列表,可能有用.
There's some more detail in this thread from the python-win32 mailing list, which may be useful.
我希望这会有所帮助!
这篇关于在 Python 中检测 Windows 注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!