DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: OnMessage(0x218,"FunctionName") question  (Read 19,797 times)

SvenBent

  • Participant
  • Joined in 2016
  • *
  • Posts: 3
OnMessage(0x218,"FunctionName") question
« on: March 16, 2016, 04:23 PM »
Sorry if I'm posting in the wrong place. but I have a question with AHK and Skrommels seems to be fond of that.

I'm trying to get the "going to standby" message from windows in my AHK scripts by using the OnMessage() command.
but it seems like my function ever gets called

Code: ( autohotkey)
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetBatchLines -1


OnMessage(0x218, "WM_POWERBROADCAST")
Return

WM_POWERBROADCAST(wParam, lParam)
	{
	msgbox test
	}

Is there some preparations I need to do to be able to get that message ?

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • Coding Snacks
Re: OnMessage(0x218,"FunctionName") question
« Reply #1 on: March 16, 2016, 07:46 PM »
If you use the expression form of the function as in the example you can test "Name" for blank on failure.

Code: (autohotkey)
Name := OnMessage(MsgNumber, "FunctionName")
If (! Name)
{
  MsgBox Message Registration Failed
  ExitApp
}
; do stuff if it worked

Edit:  Also even though the docs say you do not need to have all the params in the function definition I have found stuff works better if I use all four.

If your registration code does return the expected function name then it may be something with Windows where the change of state is not broadcasting a message for some reason.  But esp. when first trying to get code to work it is good to check the return codes for errors or wrap the code in try catch so that errors are reported.

Edit2:  Skrommel has not been around in a while so I took the liberty of sticking in my $.02.  :)

Edit3:  Also try setting DetectHiddenWindows, On before calling OnMessage since you are not using a Gui.

Edit4:  AHK uses an implied window handle called LastFound.  See the examples for OnMessage and DetectHiddenWindows.  A standard trick is to call WinExist() with a blank param to set LastFound to the handle of the current AHK program.  That's the most likely cause.

A better resource is: https://autohotkey.com/boards/index.php
see the "ask for help" board.  You will get a better explanation than the one I gave I am sure.  That's where I go when I get stuck on something AHK.  :)

« Last Edit: March 16, 2016, 10:21 PM by MilesAhead »

SvenBent

  • Participant
  • Joined in 2016
  • *
  • Posts: 3
Re: OnMessage(0x218,"FunctionName") question
« Reply #2 on: March 18, 2016, 06:16 PM »
Thank you for your feedback. any advise are gratly welcome as im desperate lol

I tried AHK forum "Ask for help" but nothing i spend a few days on the IRC channels to no prevail. I've been googling around for at least a week now, and still anything I try, of other peoples script just fails on both my computers ( work win7 64  & home Win10)  i did however get 0x11  to work so it would react on logoff but that si at east i know something is working right.

i tried you script
Code
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetBatchLines -1


Name := OnMessage(MsgNumber, "FunctionName")
If (! Name)
{
  MsgBox Message Registration Failed
  ExitApp
}
response: Message Registration Failed

i added "DllCall("RegisterSuspendResumeNotification", "ptr", A_ScriptHwnd, "uint", 0)" in front of the on message
response: Message Registration Failed

I added a small gui in fron of the above
response: Message Registration Failed

Then i added detecthidden windows
response: Message Registration Failed

Final code was this
Code
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetBatchLines -1


DetectHiddenWindows, on

Gui, New
Gui, Add, Text,, This is text:
Gui, Show

DllCall("RegisterSuspendResumeNotification", "ptr", A_ScriptHwnd, "uint", 0)
Name := OnMessage(MsgNumber, "FunctionName")
If (! Name)
{
  MsgBox Message Registration Failed
  ExitApp
}

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • Coding Snacks
Re: OnMessage(0x218,"FunctionName") question
« Reply #3 on: March 18, 2016, 07:03 PM »
The call with "FunctionName" is not to be taken literally.  That is how it is written in the help.

I got Message Registration Succeeded with this.

Code: (autohotkey)
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
DetectHiddenWindows, on
LastFound := WinExist()

DllCall("RegisterSuspendResumeNotification", "ptr", A_ScriptHwnd, "uint", 0)
Name := OnMessage(0x218, "myfunc")
If (! Name)
{
  MsgBox Message Registration Failed
  ExitApp
}
MsgBox Message Registration Succeeded
ExitApp

myfunc(wParam, lParam, msg, hwnd)
{
    MsgBox myfunc was called
}
« Last Edit: March 18, 2016, 07:14 PM by MilesAhead »

SvenBent

  • Participant
  • Joined in 2016
  • *
  • Posts: 3
Re: OnMessage(0x218,"FunctionName") question
« Reply #4 on: March 18, 2016, 07:21 PM »
Adding in more and more stuff Igot it to work then i started to removde stuff and got it al lthe way down to this



Code
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetBatchLines 1


Gui +LastFound
OnMessage(0x218, "WM_POWERBROADCAST")

return



WM_POWERBROADCAST(wparam, lparam)
	{
	msgbox %wparam% %lparam%
	}


which works.  so just the Gui +Lastfound made it all work perfectly

I also tried your new script and i got message registration succeed



WOOOT time to put it in aaction and eliminate some looping check on A_timeIdle



Thank you for getting me on the right track

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • Coding Snacks
Re: OnMessage(0x218,"FunctionName") question
« Reply #5 on: March 19, 2016, 02:41 AM »
Thank you for getting me on the right track

You are most welcome.  I am glad it worked out.  I should have gotten onto the LastFound thing right away.  Sometimes I have to sneak up on stuff.  :)

Original address: https://www.donationcoder.com/forum/index.php?topic=42423.0