问题
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 :-)
回答1:
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.
来源:https://stackoverflow.com/questions/694475/how-to-hook-to-events-messages-in-windows-using-python