如何使用 python 挂钩 Windows 中的事件/消息

发布于 2024-07-15 09:41:10 字数 982 浏览 7 评论 0原文

简而言之:

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

背景:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟凡古楼 2024-07-22 09:41:10

我发现了一个丑陋的解决方法:
我编写了一个 AutoIt 脚本,该脚本检测 Excel 的错误 MessageBox、关闭它并运行 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

(这是一个优化版本 - 第一次试验是 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文