如何使用 python 挂钩 Windows 中的事件/消息
简而言之:
我想在我的笔记本电脑上拦截挂起/待机消息,但我的程序没有收到所有相关消息。
背景:
windows xp/2k 上的 ms-excel 中存在一个错误,如果在网络/USB 驱动器上打开文件,该错误会阻止系统挂起。
我正在尝试以编程方式解决它(我的工具箱包括 python、vb6 或命令行工具)。
我对 Windows 仪器一无所知:-)
我有一个 sysinternals 实用程序,无论如何都会挂起系统。 我想将它与合盖事件挂钩!
长:
笔记本盖关闭(富士通u810)启动待机程序[如何?]
系统然后向每个人发送WM_POWERBROADCAST:PBT_APMQUERYSUSPEND(我可以使用SPYXX.EXE
跟踪它们)
每个程序都回答“True” ,直到excel回答“假”,整个过程停止。
我的问题:
1)我的python程序既没有捕获pbm_apmquerysuspend,也没有捕获PBT_APMQUERYSTANDBYFAILED,也没有捕获PBT_APMQUERYSUSPENDFAILED: `` ...
query = "SELECT * FROM Win32_PowerManagementEvent"
power_watcher = wmi.ExecNotificationQuery ( query )
power_event = power_watcher.NextEvent ()
' 如果最终发生待机,它仅接收 PBT_APMSUSPEND。
为什么不呢 - 我该如何拦截它?
2)还有其他方法拦截待机进程吗?
在完美的世界中,我会设置盖子关闭事件来运行我选择的命令。 在完美的世界中,盖子关闭是一个有记录的事件。
谢谢你们 :-)
in short:
i want to intercept suspend/standby messages on my laptop, but my program doesn't receives all relevant messages.
background:
there's a bug in ms-excel on windows xp/2k, which prevents system suspend if a file is opened on a network/usb drive.
i'm trying to work-around it programmatically (my toolbox include python, vb6, or command line tools).
i know nothing about windows instrumentation :-)
i have a sysinternals utility that suspends the system anyhow. i want to hook it to the close-lid event!
in long:
The notebook lid close (fujitsu u810) initiate the standby procedure [how?]
The system then send everybody WM_POWERBROADCAST: PBT_APMQUERYSUSPEND (i can trace them using SPYXX.EXE
)
Every program answers "True", until excel answers "false", and the whole process stops.
My questions:
1) my python program doesn't catch neither pbm_apmquerysuspend, nor PBT_APMQUERYSTANDBYFAILED, nor PBT_APMQUERYSUSPENDFAILED:
` ...
query = "SELECT * FROM Win32_PowerManagementEvent"
power_watcher = wmi.ExecNotificationQuery ( query )
power_event = power_watcher.NextEvent ()
`
it receives only PBT_APMSUSPEND, if standby finally occurs.
Why not - and how do i intercept it?
2) Is there another way to intercept the standby process?
in a prefect world, i would set the lid-close event to run a command i choose.
in a perfect world, lid-closure is a documented event.
thank you all :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了一个丑陋的解决方法:
我编写了一个 AutoIt 脚本,该脚本检测 Excel 的错误 MessageBox、关闭它并运行 sysinternals 实用程序这会强制计算机进入待机状态。
(这是一个优化版本 - 第一次试验是 CPU 密集型的)。
现在它位于系统托盘中并且可以正常工作。
丢失消息的问题仍然悬而未决。 虽然我意识到它首先与 python 无关。
I've found an ugly workaround:
I wrote an AutoIt script which detects the Excel's error MessageBox, closes it, and runs a sysinternals' utility which forces the computer to standby.
(this is an optimized version - the first trials were CPU intensive).
now it sits in the system-tray and just works.
the lost messages question is still open. though i realized it has nothing to do with python in the first place.