How to use SendMessage & OnMessage in V2?

Get help with using AutoHotkey and its commands and hotkeys
magusneo
Posts: 36
Joined: 30 Sep 2013, 06:34

How to use SendMessage & OnMessage in V2?

01 Oct 2013, 19:42

I trid follow scripts,ErrorLevel always be set "Error" after SendMessage.These scripts comes from V1's examples.It works in V1.Why not in V2?

send.ahk

Code: Select all

TargetScriptTitle:="rev.ahk ahk_class AutoHotkey"

#space::  ; Win+Space hotkey. Press it to show an InputBox for entry of a message string.
InputBox, StringToSend, Send text via WM_COPYDATA, Enter some text to Send:
if ErrorLevel  ; User pressed the Cancel button.
    return
result := Send_WM_COPYDATA(StringToSend, TargetScriptTitle)
if result = FAIL
    MsgBox SendMessage failed. Does the following WinTitle exist?:`n%TargetScriptTitle%
else if result = 0
    MsgBox Message sent but the target window responded with 0, which may mean it ignored it.
return

Send_WM_COPYDATA(ByRef StringToSend, ByRef TargetScriptTitle)  ; ByRef saves a little memory in this case.
; This function sends the specified string to the specified window and returns the reply.
; The reply is 1 if the target window processed the message, or 0 if it ignored it.
{
    VarSetCapacity(CopyDataStruct, 3*A_PtrSize, 0)  ; Set up the structure's memory area.
    ; First set the structure's cbData member to the size of the string, including its zero terminator:
    SizeInBytes := (StrLen(StringToSend) + 1) * (A_IsUnicode ? 2 : 1)
    NumPut(SizeInBytes, CopyDataStruct, A_PtrSize)  ; OS requires that this be done.
    NumPut(&StringToSend, CopyDataStruct, 2*A_PtrSize)  ; Set lpData to point to the string itself.
    Prev_DetectHiddenWindows := A_DetectHiddenWindows
    Prev_TitleMatchMode := A_TitleMatchMode
    DetectHiddenWindows On
    SetTitleMatchMode 2
    SendMessage, 0x4a, 0, &CopyDataStruct,, %TargetScriptTitle%  ; 0x4a is WM_COPYDATA. Must use Send not Post.
	OutputDebug,% ErrorLevel
    DetectHiddenWindows %Prev_DetectHiddenWindows%  ; Restore original setting for the caller.
    SetTitleMatchMode %Prev_TitleMatchMode%         ; Same.
    return ErrorLevel  ; Return SendMessage's reply back to our caller.
}
rev.ahk

Code: Select all

OnMessage(0x4a, "Receive_WM_COPYDATA")  ; 0x4a is WM_COPYDATA
return

Receive_WM_COPYDATA(wParam, lParam)
{
    StringAddress := NumGet(lParam + 2*A_PtrSize)  ; Retrieves the CopyDataStruct's lpData member.
    CopyOfData := StrGet(StringAddress)  ; Copy the string out of the structure.
    ; Show it with ToolTip vs. MsgBox so we can return in a timely fashion:
    ToolTip %A_ScriptName%`nReceived the following string:`n%CopyOfData%
    return true  ; Returning 1 (true) is the traditional way to acknowledge this message.
}

lexikos
Posts: 6855
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: How to use SendMessage & OnMessage in V2?

01 Oct 2013, 21:16

Command syntax in v2 requires % to indicate an expression for any arg, not just some args like in v1. &CopyDataStruct is an expression.

So for example, use any of:

Code: Select all

SendMessage, 0x4a, 0, % &CopyDataStruct,, %TargetScriptTitle%
SendMessage, 0x4a, 0, %&CopyDataStruct%,, %TargetScriptTitle%
SendMessage(0x4a, 0, &CopyDataStruct,, TargetScriptTitle)
magusneo
Posts: 36
Joined: 30 Sep 2013, 06:34

Re: How to use SendMessage & OnMessage in V2?

01 Oct 2013, 22:38

Thanks.I prefer this style:

Code: Select all

SendMessage(0x4a, 0, &CopyDataStruct,, TargetScriptTitle)


It seems V2 also have issues http://www.autohotkey.com/board/topic/9 ... itor-fail/ like V1.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bad husband, boiler, botman, malcev, tee_rock and 132 guests