When you send a WM_SETTEXT message as in the quoted example, the system knows that it must copy the
string between processes. Similarly, if you use WM_COPYDATA, the system knows that it must copy the data referenced by lParam (a struct containing a number and a pointer to data of specified length) between processes.
If you use some other arbitrary message which doesn't have predefined string parameters, the target window will just get whatever
number you specified. While that may be the address of a string within the
source process, interpreting it as an address within the
target process is not safe and will not work.
Even if you are using a system message which copies the parameter between processes for you, OnMessage still passes the parameter value to you as a number. If that number is the address of a string, you can use StrGet to retrieve its value. Chr with the deref (* prefix) operator will also work if used correctly, but note that
*address reads a
byte, which is never a complete character unless you are using an ANSI version of AutoHotkey.
WM_COPYDATA is designed specifically for sending data between processes, but it is a little more complex than needed for just one string.
This would work:
Code: Select all
Gui, Show, w200 h200, test
OnMessage(0xC, "Msg") ; WM_SETTEXT := 0xC
Msg(wParam, lParam) {
MsgBox % StrGet(lParam)
return 0 ; This prevents the GUI title from changing.
}
Code: Select all
MyVar := "Hello"
SendMessage, 0xC, 0, &MyVar,, test
If you prefer not to "overload" the WM_SETTEXT message, since it already has a purpose (change the window title), you can use some other
system message which has a string parameter. One such message is EM_REPLACESEL, which normally has meaning only to the Edit control. You can just replace
0xC with
0xC2 in the example above (and
return 0 can be removed or left as is).
There is no need to show a visible window for this if the sending script can identify the receiver by script name. For example:
Code: Select all
; receiver.ahk
OnMessage(0xC2, "Msg")
Msg(wParam, lParam) {
MsgBox % StrGet(lParam)
}
Code: Select all
MyVar := "Hello"
DetectHiddenWindows, On
SetTitleMatchMode, 2
SendMessage, 0xC2, 0, &MyVar,, receiver.ahk ahk_class AutoHotkey