Jump to content

Get notification when going into hibernate/sleep mode


Recommended Posts

I have a script that monitors network up/down time and when my laptop is set to hibernate or sleep, then later started up again, my script counts all of the hibernate/sleep time as down time.

If I could get notification when going into hibernate/sleep mode and when it resumes normal mode, I could stop looking until normal mode is resumed.

Is there an API that does this, or perhaps an event I could register a handler for?

Andy

Link to comment
Share on other sites

  • Developers

Run this script in SciTE and go into sleep and return from sleep... you will see notifications in the console.

#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <date.au3>
;~ #define PBT_APMQUERYSUSPEND           0x0000
;~ #define PBT_APMQUERYSTANDBY           0x0001
;~ #define PBT_APMQUERYSUSPENDFAILED     0x0002
;~ #define PBT_APMQUERYSTANDBYFAILED     0x0003
;~ #define PBT_APMSUSPEND                0x0004
;~ #define PBT_APMSTANDBY                0x0005
;~ #define PBT_APMRESUMECRITICAL         0x0006
;~ #define PBT_APMRESUMESUSPEND      0x0007
;~ #define PBT_APMRESUMESTANDBY      0x0008
;~ #define PBTF_APMRESUMEFROMFAILURE     0x00000001
;~ #define PBT_APMBATTERYLOW             0x0009
;~ #define PBT_APMPOWERSTATUSCHANGE  0x000A
;~ #define PBT_APMOEMEVENT               0x000B
;~ #define PBT_APMRESUMEAUTOMATIC        0x0012
Global $PBT_APMSUSPEND = 0x0004
Global $PBT_APMRESUMESUSPEND = 0x0007
Global $PBT_APMSTANDBY = 0x0005
Global $PBT_APMRESUMESTANDBY = 0x0008

$hGUI    = GUICreate("Test", 100, 100,1,1)
GUIRegisterMsg($WM_POWERBROADCAST, "Standby")
GUIRegisterMsg($WM_QUERYENDSESSION, "Shutdown")
;~ GUISetState()
While 1
Sleep(10)
;~ $GUIMsg = GUIGetMsg()
;~   Switch $GUIMsg
;~       Case $GUI_EVENT_CLOSE
;~           ExitLoop
;~   EndSwitch
WEnd
;
Exit
;
Func Standby($hWnd, $Msg, $wParam, $lParam)
ConsoleWrite(_NowTime() & ": " & $wParam & @LF)
Select
Case $wParam = $PBT_APMSUSPEND
ConsoleWrite(" You going into Suspend." & @LF)
Case $wParam = $PBT_APMRESUMESUSPEND
ConsoleWrite(" You just woke up from Suspend." & @LF)
Case $wParam = $PBT_APMRESUMESTANDBY
ConsoleWrite(" You are going into Standby." & @LF)
Case $wParam = $PBT_APMRESUMESTANDBY
ConsoleWrite(" You just woke up from Standby." & @LF)
;MsgBox(0,"Hello Back", " You just woke up from Standby")
Case Else
EndSelect
EndFunc

Jos :)

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Excellent code Jos. Will be adding that to my snippet collection.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • 2 weeks later...
  • 3 weeks later...

Thanks for the code, but it has some strange behaviour:

First, here is my code (I'm trying to make an UDF)

; Veille
Global $PBT_APMSUSPEND = 0x0004
Global $PBT_APMRESUMEAUTOMATIC = 0x0012 ; Toujours envoyé à la sortie de veille
Global $PBT_APMRESUMESUSPEND = 0x0007 ; Envoyé si c'est l'utilisateur qui a provoqué la sortie de veille
; Veille prolongée
Global $PBT_APMSTANDBY = 0x0005
Global $PBT_APMRESUMESTANDBY = 0x0008
; Etat batterie et secteur
Global $PBT_APMPOWERSTATUSCHANGE = 0x000A ; Utiliser GetSystemPowerStatus pour plus de détails sur l'event
; ---
Global $__gPowerStatus_sCallBack = ""
Global $__gPowerStatus_hGUI = 0
Global $__gPowerStatus_iIsUserGUI = 0
_PowerStatus_Register("test")
While 1
Sleep(500)
WEnd
Func _PowerStatus_Register($sFunc = "", $hGUI = Default)
If $sFunc Then
  $__gPowerStatus_sCallBack = $sFunc
  If $hGUI = Default Then
   $__gPowerStatus_hGUI = GUICreate('__PowerStatus_WM_POWERBROADCAST_Window')
   $__gPowerStatus_iIsUserGUI = 0
  Else
   $__gPowerStatus_hGUI = $hGUI
   $__gPowerStatus_iIsUserGUI = 1
  EndIf
  GUIRegisterMsg(0x218, "__PowerStatus_WM_POWERBROADCAST") ; WM_POWERBROADCAST
Else
  $__gPowerStatus_sCallBack = ""
  If Not $__gPowerStatus_iIsUserGUI Then
   GUIDelete($__gPowerStatus_hGUI)
  EndIf
  $__gPowerStatus_hGUI = 0
  $__gPowerStatus_iIsUserGUI = 0
EndIf
Return 1
EndFunc
; ---
Func __PowerStatus_WM_POWERBROADCAST($hWnd, $Msg, $wParam, $lParam)
If $hWnd <> $__gPowerStatus_hGUI Then Return
If $Msg <> 0x218 Then Return ; Not WM_POWERBROADCAST
If Not $__gPowerStatus_sCallBack Then Return
; ---
Switch $wParam
  Case $PBT_APMSUSPEND
   ConsoleWrite("Mise en veille" & @CRLF) ; En veille (sleep on)
  Case $PBT_APMRESUMEAUTOMATIC
   ConsoleWrite("Sortie de veille" & @CRLF) ; (sleep off)
  ; ---
  Case $PBT_APMRESUMESTANDBY
   ConsoleWrite("Mise en veille prolongée" & @CRLF) ; Veille prolongée (standby on)
  Case $PBT_APMRESUMESTANDBY
   ConsoleWrite("Sortie de veille prolongée" & @CRLF) ; (standby off)
  ; ---
  Case $PBT_APMPOWERSTATUSCHANGE
   ConsoleWrite("Changement dans l'alimentation:" & @CRLF) ; Power status change
   ; ---
   Local $tagSYSTEM_POWER_STATUS = _
    "byte ACLineStatus;" & _
    "byte BatteryFlag;" & _
    "byte BatteryLifePercent;" & _
    "byte Reserved1;" & _
    "dword BatteryLifeTime;" & _
    "dword BatteryFullLifeTime"
   Local $tSystemPowerStatus = DllStructCreate($tagSYSTEM_POWER_STATUS)
   DllCall("Kernel32.dll", "bool", "GetSystemPowerStatus", "struct*", $tSystemPowerStatus)
   If Not @error Then
    ConsoleWrite(" ACLineStatus = " & DllStructGetData($tSystemPowerStatus, 1) & @CRLF)
    ConsoleWrite(" BatteryFlag = " & DllStructGetData($tSystemPowerStatus, 2) & @CRLF)
    ConsoleWrite(" BatteryLifePercent = " & DllStructGetData($tSystemPowerStatus, 3) & @CRLF)
    ConsoleWrite(" BatteryLifeTime = " & DllStructGetData($tSystemPowerStatus, 5) & @CRLF)
    ConsoleWrite(" BatteryFullLifeTime = " & DllStructGetData($tSystemPowerStatus, 6) & @CRLF)
   EndIf
EndSwitch
EndFunc

Everything works well, but when I go on StandBy, It's the sleeping messages that are fired ($PBT_APMSUSPEND and $PBT_APMRESUMEAUTOMATIC) instead of the StandBy message ($PBT_APMRESUMESTANDBY)

Anyone? Thanks

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

×
×
  • Create New...