如何使用 python 挂钩到 Windows 中的事件/消息 [英] how to hook to events / messages in windows using python

查看:52
本文介绍了如何使用 python 挂钩到 Windows 中的事件/消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之:

我想在我的笔记本电脑上拦截挂起/待机消息,但我的程序没有收到所有相关消息.

背景:

Windows xp/2k 上的 ms-excel 存在一个错误,如果在网络/USB 驱动器上打开文件,它会阻止系统挂起.

我正在尝试以编程方式解决它(我的工具箱包括 python、vb6 或命令行工具).

我对 Windows 仪器一无所知 :-)

我有一个 sysinternals 实用程序,无论如何都可以挂起系统.我想把它挂到 close-lid 事件上!

长:

关闭笔记本盖子(富士通u810)启动待机程序[如何?]

系统然后向所有人发送 WM_POWERBROADCAST:PBT_APMQUERYSUSPEND(我可以使用 SPYXX.EXE 追踪他们)

每个程序都回答True",直到excel回答false",整个​​过程停止.

我的问题:

1) 我的 python 程序既没有捕获 pbm_apmquerysuspend,也没有捕获 PBT_APMQUERYSTANDBYFAILED,也没有捕获 PBT_APMQUERYSUSPENDFAILED:` ...

query = "SELECT * FROM Win32_PowerManagementEvent"power_watcher = wmi.ExecNotificationQuery(查询)power_event = power_watcher.NextEvent()

`如果最终发生待机,它只接收 PBT_APMSUSPEND.

为什么不 - 我如何拦截它?

2) 有没有其他方法可以拦截待机进程?

在完美世界中,我会设置盖子关闭事件来运行我选择的命令.在一个完美的世界中,盖子关闭是一个记录在案的事件.

谢谢大家:-)

解决方案

我发现了一个丑陋的解决方法:我写了一个 AutoIt 脚本,它检测 Excel 的错误 MessageBox,关闭它,并运行一个 sysinternals 的实用程序这会强制计算机待机.

<前>Opt("WinWaitDelay",400);- 精确的文本匹配,以节省大量的杯子循环!Opt("WinTitleMatchMode",3)Opt("WinDetectHiddenText",1)选项(鼠标坐标模式",0);Opt("WinSearchChildren",1)暗 $title = "Microsoft Excel"" dim $text = "Windows 无法进入待机状态,因为正在从网络访问 Microsoft Office 文档或应用程序组件.您必须关闭打开的文档或退出应用程序,然后才能将计算机置于待机状态.虽然是真的;等待excel的错误消息WinWait($title, $text)运行(psshutdown.exe -c -d -accepteula -m mooshmoosh -t 5");如果没有睡眠",烦人的 msgbox 就不会关闭睡眠(1000);关闭烦人的模态msgbox!WinClose($title);1 分钟延迟,节省 cpu (?)睡眠(1*60*1000)结束

(这是一个优化版本 - 第一次试验是 CPU 密集型的).
现在它位于系统托盘中并且可以正常工作.

丢失消息的问题仍然悬而未决.虽然我意识到它首先与 python 无关.

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 :-)

解决方案

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.

Opt("WinWaitDelay",400)
; -- exact text match, to save LOTS of cup cycles!
Opt("WinTitleMatchMode",3)
Opt("WinDetectHiddenText",1)
Opt("MouseCoordMode",0)
; Opt("WinSearchChildren",1)
dim $title = "Microsoft Excel"
dim $text = "Windows cannot go on standby because Microsoft Office documents or application components are being accessed from the network. You must close the open documents or exit the applications before you can put the computer on standby."
While True
     ; wait for excel's error msg
     WinWait($title, $text)
     Run("psshutdown.exe -c -d -accepteula -m mooshmoosh -t 5")
     ; the annoying msgbox doesn't close without the 'sleep'
     Sleep(1000)
     ; close the annoying modal msgbox!
     WinClose($title)
     ;1 minute delay, save cpu (?)
     Sleep(1*60*1000)
WEnd

(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.

这篇关于如何使用 python 挂钩到 Windows 中的事件/消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆