Sign in to follow this  
Followers 0
GordonCZ

Detect power events

10 posts in this topic

Hi. I need detect situation when PC go to standby mode, b/c I need kill one process before it. I found something about it on MS pages: http://msdn.microsoft.com/en-us/library/aa394362%28VS.85%29.aspx

I´d like to detect value 4 and then kill a process. Could U help me please?

Share this post


Link to post
Share on other sites



Hi. I need detect situation when PC go to standby mode, b/c I need kill one process before it. I found something about it on MS pages: http://msdn.microsoft.com/en-us/library/aa394362%28VS.85%29.aspx

I´d like to detect value 4 and then kill a process. Could U help me please?

This isan example of how to detect power events

;by martin

#include <GUIConstantsEx.au3>

Global Const $WM_POWERBROADCAST =   0x218
Global Const $PBT_APMRESUMEAUTOMATIC =  0x12
Global Const $PBT_APMQUERYSUSPEND   =   0x0000
Global Const $PBT_APMQUERYSTANDBY   =   0x0001
Global Const $PBT_APMQUERYSUSPENDFAILED =   0x0002
Global Const $PBT_APMQUERYSTANDBYFAILED =   0x0003
Global Const $PBT_APMSUSPEND    =   0x0004
Global Const $PBT_APMSTANDBY    =   0x0005
Global Const $PBT_APMRESUMECRITICAL =   0x0006
Global Const $PBT_APMRESUMESUSPEND  =   0x0007
Global Const $PBT_APMRESUMESTANDBY  =   0x0008
Global Const $PBTF_APMRESUMEFROMFAILURE =   0x00000001
Global Const $PBT_APMBATTERYLOW     =   0x0009
Global Const $PBT_APMPOWERSTATUSCHANGE =    0x000A
Global Const $PBT_APMOEMEVENT   =   0x000B



GUICreate("detect WM_POWERBROADCAST")
GUISetState()
GUIRegisterMsg($WM_POWERBROADCAST, "PowerEvents")
$sleeping = False
$lidClosed = False
While GUIGetMsg() <> -3
WEnd


Func PowerEvents($hWnd, $Msg, $wParam, $lParam)
    Switch $wParam 
        Case $PBT_APMQUERYSTANDBY 
             $lidClosed = True;?
             $sleeping = True;?
         Case $PBT_APMRESUMEAUTOMATIC 
             $sleeping = False
     EndSwitch
     
    
    Return $GUI_RUNDEFMSG
EndFunc

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Oh, that was really quick. Thanks, it works. I just had to change "Case $PBT_APMQUERYSTANDBY" to "Case 4" (i dont really know why, but the original didnt work properly).

But I dont like the GUI window. Could it work without showing the window? It isnt nice and also someone might close it. THX

Share this post


Link to post
Share on other sites

Oh, that was really quick. Thanks, it works. I just had to change "Case $PBT_APMQUERYSTANDBY" to "Case 4" (i dont really know why, but the original didnt work properly).

But I dont like the GUI window. Could it work without showing the window? It isnt nice and also someone might close it. THX

You need a window to receive messages, but it doesn't have to be visible. Just comment out the line GuiSetState().

"Isn't nice" ! coo what a cheek- I was very pleased with that nice little window; interesting grey backround and pretty blue border with an attractive red .... :mellow:


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

You need a window to receive messages, but it doesn't have to be visible. Just comment out the line GuiSetState().

"Isn't nice" ! coo what a cheek- I was very pleased with that nice little window; interesting grey backround and pretty blue border with an attractive red .... :mellow:

Thanks, U are amazing. No one has halped me so quick like you. It finally works correctly. Thanks to you, my problem was (I hope so) solved.

Share this post


Link to post
Share on other sites

GUIRegisterMsg($WM_POWERBROADCAST, "PowerEvents")

Func PowerEvents($hWnd, $Msg, $wParam, $lParam)

EndFunc

http://msdn.microsoft.com/en-us/library/aa373247%28VS.85%29.aspx

$lParam will be a pointer to a struct, containing data about the specific change.

the struct is described here:

http://msdn.microsoft.com/en-us/library/aa372723%28v=VS.85%29.aspx

and the data here:

http://msdn.microsoft.com/en-us/library/aa373195%28v=VS.85%29.aspx

but i have no idea,how to write this scruct in autoit:

typedef struct {

GUID PowerSetting;

DWORD DataLength;

UCHAR Data[1];

} POWERBROADCAST_SETTING, *PPOWERBROADCAST_SETTING;

how dealing with the GUID ?


AutoIt-Syntaxsheme for Proton & Phase5 * Firefox Addons by me (resizable Textarea 0.1d) (docked JS-Console 0.1.1)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

thats my code:

Global Const $WM_POWERBROADCAST = 0x218
Global Const $PBT_APMPOWERSTATUSCHANGE = 0xA ; Power status has changed.
Global Const $PBT_APMRESUMEAUTOMATIC = 0x12 ; Operation is resuming automatically from a low-power state. 
                                            ; This message is sent every time the system resumes.
Global Const $PBT_APMRESUMESUSPEND = 0x7 ;Operation is resuming from a low-power state. This message is sent after
                                         ;PBT_APMRESUMEAUTOMATIC if the resume is triggered by user input, 
                                         ; such as pressing a key.
Global Const $PBT_APMSUSPEND = 0x4 ; System is suspending operation.
Global Const $PBT_POWERSETTINGCHANGE = 0x8013 ; A power setting change event has been received.
; Windows Server 2003, Windows XP, and Windows 2000:  The following event identifiers are also supported.
Global Const $PBT_APMBATTERYLOW = 0x9 ; Battery power is low. In Windows Server 2008 and Windows Vista, use PBT_APMPOWERSTATUSCHANGE instead.
Global Const $PBT_APMOEMEVENT = 0xB ; OEM-defined event occurred. In Windows Server 2008 and Windows Vista, this event is not available because these operating systems support only ACPI; APM BIOS events are not supported.
Global Const $PBT_APMQUERYSUSPEND = 0x0 ; Request for permission to suspend. In Windows Server 2008 and Windows Vista, use the SetThreadExecutionState function instead.
Global Const $PBT_APMQUERYSUSPENDFAILED = 0x2 ; Suspension request denied. In Windows Server 2008 and Windows Vista, use SetThreadExecutionState instead.
Global Const $PBT_APMRESUMECRITICAL = 0x6 ; Operation resuming after critical suspension. In Windows Server 2008 and Windows Vista, use PBT_APMRESUMEAUTOMATIC instead. 
Global Const $BROADCAST_QUERY_DENY = 0x424D5144

GUICreate("Event Receiver")
GUIRegisterMsg($WM_POWERBROADCAST,"MY_WM_POWERBROADCAST")
While 1
    Sleep(100)
WEnd
Func MY_WM_POWERBROADCAST($hWnd, $uMsg, $wParam, $lParam)
    ; Beschreibgungen auf http://msdn.microsoft.com/en-us/library/aa373247(VS.85).aspx
    Switch $wParam
        Case $PBT_APMPOWERSTATUSCHANGE
            #cs
            typedef struct {
              GUID  PowerSetting;
              DWORD DataLength;
              UCHAR Data[1];
            } POWERBROADCAST_SETTING, *PPOWERBROADCAST_SETTING;
            #ce
            $GUID = DllStructCreate("dword;word;word;byte")

            ;Local $tNMBHOTITEM = DllStructCreate($GUID&";DWORD;UCHAR",$lParam)
            Local $tNMBHOTITEM = DllStructCreate(DllStructGetPtr($GUID)&";DWORD;CHAR",$lParam)
            Local $nNotifyCode = DllStructGetData($tNMBHOTITEM, 1)
            Local $nID = DllStructGetData($tNMBHOTITEM, 2)
            Local $hCtrl = DllStructGetData($tNMBHOTITEM, 3)
            Local $dwFlags = DllStructGetData($tNMBHOTITEM, 4)
            Tooltip($nNotifyCode&" "&$nID&" "&$hCtrl);&" "&$dwFlags
        Case $PBT_APMQUERYSUSPEND
            Return $BROADCAST_QUERY_DENY

    EndSwitch
EndFunc

how to deal with the GUID ?

EDIT: my fault. $PBT_APMPOWERSTATUSCHANGE does not return statusdetails in $lParam.

$PBT_POWERSETTINGCHANGE does, but thats not usable to detect pulling the ac-power out.

Edited by Raik

AutoIt-Syntaxsheme for Proton & Phase5 * Firefox Addons by me (resizable Textarea 0.1d) (docked JS-Console 0.1.1)

Share this post


Link to post
Share on other sites

Hello Guys

I wonder if anybody could help me to figure this out.

I would like to receive the Power Event for when my monitor goes into standby.

It should be GUID_MONITOR_POWER_ON and Raik posted the way to get it.

http://msdn.microsoft.com/en-us/library/aa373247%28VS.85%29.aspx

$lParam will be a pointer to a struct, containing data about the specific change.

the struct is described here:

http://msdn.microsoft.com/en-us/library/aa372723%28v=VS.85%29.aspx

and the data here:

http://msdn.microsoft.com/en-us/library/aa373195%28v=VS.85%29.aspx

Unfortunately I just can't figure out how to use his code to get what I need.

Please help. Thanks!

Share this post


Link to post
Share on other sites

You need a window to receive messages, but it doesn't have to be visible. Just comment out the line GuiSetState().

Not true, you can build an event sink that will internally fire linked functions. Here is a psuedo-example (I'm not doing the WMI impersonate levels, you will need error checking, and obviously other functions blah blah):

; WMI object creation for CIMV2 namespace
$gobjWMI_CIMV2 = ObjGet("winmgmts:\\.\root\cimv2")

; WMI event notification sink for power management
$gobjWMI_PowerEVT_Sink = ObjCreate("WbemScripting.SWbemSink")
ObjEvent($gobjWMI_PowerEVT_Sink , "PowerEVT_SINK_")


; Build notifier object for power management events
$gobjWMI_CIMV2.ExecNotificationQueryAsync($gobjWMI_PowerEVT_Sink, "Select EventType from Win32_PowerManagementEvent")


;; Sleep forevAr - make sure to build a hotkey somewhere to allow you to terminate this process...
While 1
    Sleep(100000)
WEnd



;; Power Management event sink function
Func PowerEVT_SINK_OnObjectReady($wmiObject, $wmiAsyncContext) 
    $intEvent = $wmiObject.EventType

    Switch $intEvent
        Case 4
            ; Machine is entering suspend")
        Case 7
            ; OS reports resume from suspend is complete")
        Case 10
            ; AC <-> DC switch
        Case 11
            ; OEM Power event...
        Case 18
            ; Hardware triggered resume from suspend...
        Case Etc...
            ; You can case these out from the constant defines mentioned earlier in this thread
    EndSwitch
EndFunc

This code is ugly, I kinda slapped it together for this thread, but you get the idea. No windows necessary, not even a hidden one. I use this for automatic detection of when the WHS connector on my various machines start up in the wee hours of the morning to do their backup. The box wakes up, triggers my app, it goes watching for the backup service to start. If it does within five minutes, I track the process until it completes, and then force the box to go back to whatever sleep state it was in (hibernate / suspend).

Otherwise it drives the wife nuts as she has automatic sleep turned off on her netbook, which means it stays powered on and her OCD side can't take that :)

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

I think I speak for a great many AutoIt users out there - I think we should try to build a UDF for this which provides the following:

  • Current Power State by Function Call
  • Power State Event Triggers by Function Call (need equiv of FIFO for return - including the function should start the event poller described in the post above)
  • Ability to set and read power management settings for current mode, plus other modes by function call.

I'll start working on this based on the code I see in this thread - I badly need this function in the coming weeks. I have the ability to test on XP - Server 2008 R2 at work.

-Tim

Edited by tim292stro

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0