topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Saturday December 14, 2024, 9:32 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

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

SvenBent

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 3
    • View Profile
    • Donate to Member
OnMessage(0x218,"FunctionName") question
« on: March 16, 2016, 11:23 AM »
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 [Select]
  1. #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
  2. SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
  3. SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
  4.  
  5.  
  6. OnMessage(0x218, "WM_POWERBROADCAST")
  7. Return
  8.  
  9. WM_POWERBROADCAST(wParam, lParam)
  10.         {
  11.         msgbox test
  12.         }

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

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: OnMessage(0x218,"FunctionName") question
« Reply #1 on: March 16, 2016, 02: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 [Select]
  1. Name := OnMessage(MsgNumber, "FunctionName")
  2. If (! Name)
  3. {
  4.   MsgBox Message Registration Failed
  5. }
  6. ; 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, 05:21 PM by MilesAhead »

SvenBent

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 3
    • View Profile
    • Donate to Member
Re: OnMessage(0x218,"FunctionName") question
« Reply #2 on: March 18, 2016, 01: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
#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
#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
    • View Profile
    • Donate to Member
Re: OnMessage(0x218,"FunctionName") question
« Reply #3 on: March 18, 2016, 02: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 [Select]
  1. #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
  2. ; #Warn  ; Enable warnings to assist with detecting common errors.
  3. SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
  4. SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
  5. LastFound := WinExist()
  6.  
  7. DllCall("RegisterSuspendResumeNotification", "ptr", A_ScriptHwnd, "uint", 0)
  8. Name := OnMessage(0x218, "myfunc")
  9. If (! Name)
  10. {
  11.   MsgBox Message Registration Failed
  12. }
  13. MsgBox Message Registration Succeeded
  14.  
  15. myfunc(wParam, lParam, msg, hwnd)
  16. {
  17.     MsgBox myfunc was called
  18. }
« Last Edit: March 18, 2016, 02:14 PM by MilesAhead »

SvenBent

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 3
    • View Profile
    • Donate to Member
Re: OnMessage(0x218,"FunctionName") question
« Reply #4 on: March 18, 2016, 02: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



#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
    • View Profile
    • Donate to Member
Re: OnMessage(0x218,"FunctionName") question
« Reply #5 on: March 18, 2016, 09:41 PM »
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.  :)