如何使用python在windows中钩住事件/消息 简言之:

如何使用python在windows中钩住事件/消息 简言之:,python,windows,wmi,power-management,Python,Windows,Wmi,Power Management,我想截取笔记本电脑上的挂起/待机消息,但我的程序没有收到所有相关消息 背景: windows xp/2k上的ms excel中存在一个错误,如果在网络/usb驱动器上打开文件,则会阻止系统挂起 我正试图以编程方式解决它(我的工具箱包括python、vb6或命令行工具) 我对windows检测一无所知:-) 我有一个sysinternals实用程序,它无论如何都会挂起系统。我想把它挂到关闭盖子事件上 总之: 笔记本电脑盖关闭(富士通u810)启动待机程序[如何操作?] 然后系统向每个人发送WM\u

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

背景: windows xp/2k上的ms excel中存在一个错误,如果在网络/usb驱动器上打开文件,则会阻止系统挂起

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

我对windows检测一无所知:-)

我有一个sysinternals实用程序,它无论如何都会挂起系统。我想把它挂到关闭盖子事件上

总之: 笔记本电脑盖关闭(富士通u810)启动待机程序[如何操作?]

然后系统向每个人发送WM\u POWERBROADCAST:PBT\u APMQUERYSUSPEND(我可以使用
SPYXX.EXE
跟踪他们)

每个程序都回答“真”,直到excel回答“假”,整个过程停止

我的问题是: 1) 我的python程序既不捕获pbm_apmquerysuspend,也不捕获PBT_APMQUERYSTANDBYFAILED,也不捕获PBT_APMQUERYSUSPENDFAILED: `

` 如果最终出现待机状态,则只接收PBT_APMSUSPEND

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

2) 是否有其他方法拦截备用进程

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


谢谢大家:-)

我找到了一个难看的解决办法: 我编写了一个脚本,检测Excel的错误消息框,关闭它,并运行一个sysinternals实用程序,强制计算机待机

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 选择(“WinWaitDelay”,400) ; -- 精确的文本匹配,以节省大量的杯周期! 选项(“WinTitleMatchMode”,3) 选项(“WinDetectHiddenText”,1) 选项(“鼠标命令模式”,0) ; 选择(“WinSearchChildren”,1) dim$title=“Microsoft Excel” dim$text=“Windows无法进入待机状态,因为正在从网络访问Microsoft Office文档或应用程序组件。在将计算机置于待机状态之前,必须关闭打开的文档或退出应用程序。” 虽然是真的 ; 等待excel的错误消息 WinWait($title,$text) 运行(“psshutdown.exe-c-d-accepteula-m mooshmoosh-t5”) ; 没有“睡眠”按钮,恼人的msgbox无法关闭 睡眠(1000) ; 关闭恼人的模态msgbox! 温克洛斯(头衔) ;1分钟延迟,节省cpu(?) 睡眠(1*60*1000) 温德 (这是一个优化的版本-第一次试验是CPU密集型的)。
现在它位于系统托盘中,可以正常工作

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

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