I modified the final OnMessage() example (using sender.ahk and receiver.ahk) on the sender side by changing the 1st seven lines and the SendMessage Line from this:
TargetScriptTitle = Receiver.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) ****** Same ***** 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. { ****** Same ***** SendMessage, 0x4a, 0, &CopyDataStruct,, %TargetScriptTitle% ; 0x4a is WM_COPYDATA. Must use Send not Post. DetectHiddenWindows %Prev_DetectHiddenWindows% ; Restore original setting for the caller. SetTitleMatchMode %Prev_TitleMatchMode% ; Same. return ErrorLevel ; Return SendMessage's reply back to our caller. }
to this:
global PID:= #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 target:="Receiver.exe" Process, Exist, %target% if ErrorLevel=0 { Run, %target%,,, PID tooltip, launched PID %PID% } else PID:=ErrorLevel result := Send_WM_COPYDATA(StringToSend, PID) 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. Send_WM_COPYDATA(ByRef StringToSend, PID) ; 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,, ahk_pid %PID% ; 0x4a is WM_COPYDATA. Must use Send not Post. DetectHiddenWindows %Prev_DetectHiddenWindows% ; Restore original setting for the caller. SetTitleMatchMode %Prev_TitleMatchMode% ; Same. return ErrorLevel ; Return SendMessage's reply back to our caller. }
This is because I want to run the receiver as an EXE.
So, in my version, the data is received only the first time. Subsequent sends are apparanetly not receivedas there is no return code. Why is this and what is the correct way to use SendMessage to pass a data string to another EXE process?
In case it's relevent my OS is win7 Ultimate, 64-bit. Also, the resource monitor displays the receiver PID as active - both for the 1st text data that succeeds and the subsequent text data that all fail.