Jump to content

Recommended Posts

Posted

I'm trying to detect when the laptop is returning from standby. I found several examples from years ago (such as https://www.autoitscript.com/forum/topic/53311-standbye-detection), but apparently as of Windows 10 v2004 an application must now register to receive WM_POWERBROADCAST messages when running on a laptop with "modern standby" (see https://docs.microsoft.com/en-us/answers/questions/48395/cannot-receive-wm-powerbroadcast-message-on-win10.html for a discussion of this). I found an example of how to do this with Autohotkeys using the line

DllCall("RegisterSuspendResumeNotification", "ptr", A_ScriptHwnd, "uint", 0)

(see https://www.autohotkey.com/boards/viewtopic.php?t=36714 for the full post). As near as I can tell the equivalent for AutoIT would be 

DllCall("c:\windows\System32\User32.dll", "ptr", "RegisterSuspendResumeNotification", "UINT", 0)

This isn't working for me, however, upon executing I get exit code ">Exit code: 3221225477". Here's my full code:

#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 $WM_POWERBROADCAST     = 536

Global $PBT_APMRESUMESUSPEND  =  0x0007
Global $PBT_APMRESUMESTANDBY  =  0x0008
Global $PBT_APMRESUMEAUTOMATIC  =  0x0012

$hGUI       = GUICreate("Test", 1000, 400,1,1)
DllCall("User32.dll", "ptr", "RegisterSuspendResumeNotification", "UINT", 0)
GUIRegisterMsg($WM_POWERBROADCAST, "Standby")
;GUISetState()

ConsoleWrite(_NowTime() & ": Running" & @LF)

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_APMRESUMESUSPEND
            ConsoleWrite(" You just woke up from Suspend." & @LF)
            MsgBox(0,"Hello Back", " You just woke up from Suspend")
        Case $wParam = $PBT_APMRESUMESTANDBY
            ConsoleWrite(" You just woke up from Standby." & @LF)
            MsgBox(0,"Hello Back", " You just woke up from Standby")
        Case $wParam = $PBT_APMRESUMEAUTOMATIC
            ConsoleWrite(" You just woke up from ????." & @LF)
            MsgBox(0,"Hello Back", " You just woke up from ??????")
        Case Else
    EndSelect
EndFunc

Any suggestions? Thanks!

Posted

RegisterSuspendResumeNotification returns a value of type HPOWERNOTIFY which is a PVOID pointer, which in AutoIt translates to a standard "ptr". Therefore, RegisterSuspendResumeNotification must be implemented this way in AutoIt:

DllCall( "User32.dll", "ptr", "RegisterSuspendResumeNotification", "handle", $hRecipient, "dword", $iFlags )

Valid flag values are:

Global Const $DEVICE_NOTIFY_WINDOW_HANDLE = 0
Global Const $DEVICE_NOTIFY_CALLBACK = 2

As you're using the function in your code, the DllCall should look like this:

$hPowerNotify = DllCall( "User32.dll", "ptr", "RegisterSuspendResumeNotification", "handle", $hGUI, "dword", $DEVICE_NOTIFY_WINDOW_HANDLE )

You can use $hPowerNotify to unregister the notifications.

Untested since I'm on Windows 7 at the moment.

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
  • Recently Browsing   0 members

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