Jump to content

Windows 7 sleeping vs hibernate vs reboot


JohanS
 Share

Recommended Posts

I'm interested in a system going to sleep, hibernate or being rebooted/shutdown.

I managed to subscribe to the correct events to notice hibernate and reboot/shutdown but i'm not able to detect sleep.

Can anybody point me in the right direction?

Current code:

#NoTrayicon
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#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



$h = GUICreate("detect WM_POWERBROADCAST")
GUISetState()
GUIRegisterMsg($WM_POWERBROADCAST, "PowerEvents")
GUIRegisterMsg($WM_QUERYENDSESSION, "EndSession")

While GUIGetMsg() <> -3
WEnd

Func EndSession($hWnd, $Msg, $wParam, $lParam)
   ConsoleWrite("Reboot/Shutdown" & @CRLF)
   return TRUE
EndFunc


Func PowerEvents($hWnd, $Msg, $wParam, $lParam)
    Switch $wParam
        Case 0x0004
             ConsoleWrite("Hibernate" & @CRLF)
         Case 0x12
             ConsoleWrite("Resumed" & @CRLF)
     EndSwitch


    Return $GUI_RUNDEFMSG
 EndFunc
Link to comment
Share on other sites

From this link...
Remarks

The system always sends a PBT_APMRESUMEAUTOMATIC message whenever the system resumes. If the system resumes in response to user input such as pressing a key, the system also sends a PBT_APMRESUMESUSPEND message after sending PBT_APMRESUMEAUTOMATIC.

WM_POWERBROADCAST messages do not distinguish between different low-power states. An application can determine only that the system is entering or has resumed from a low-power state; it cannot determine the specific power state. The system records details about power state transitions in the Windows System event log.

To prevent the system from transitioning to a low-power state in Windows Vista, an application must call SetThreadExecutionState to inform the system that it is in use.

 

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Thanks for the quick reply Kylomas.

What i take away from that is that you can't distinguish sleep from hibernate.  Which isn't really an issue for what i'm trying to accomplish.  

The weird thing is i DON'T get an event when the system goes to sleep. (through a time out set in the enery manager or by selecting sleep from the start menu).

When I close the lid of the laptop or i choose hibernate from the start menu I do get the event.

 

Is sleeping not considered a low-power state?  Should I register to an other event?

Edited by JohanS
Link to comment
Share on other sites

I found the problems with my testing.

1) I had set dim screen AND sleep to 1 minute.  Not receiving a message when the screen dimmed I falsely presumed that no event was being sent when the system went to sleep.   
 

I found out that the system never went to sleep after setting the dim screen to never.
 

2) After some testing it seemed that the system went to sleep if my script wasn't running.  If it was running it seemed to block the sleep action

Problem was that I was running the script from a network drive.  This prevents the system from going to sleep.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...