I tried to use following app as a bridge between Firefox and another AHK script (named FirefoxLogin.exe). So that if the app received message from Firefox (via stdin), it may send the string to FirefoxLogin.exe (via WM_COPYDATA); if FirefoxLogin.exe send a message to app (via WM_COPYDATA), it may send message to Firefox (via stdout). However, it only works in one direction (Firefox -> app -> FirefoxLogin) but not the opposite. Both "Windows GUI" and "Windows Console" work in the same pattern. Through some debugging, the SendMessage from FirefoxLogin to app always fails so the app is unable to receive message from FirefoxLogin. I wonder if Firefox interferes with the app's messaging loop
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Include <JXON>
#include <WM_COPYDATA>
#SingleInstance,force
#Persistent
DetectHiddenWindows,On
SetBatchLines,-1
SetControlDelay,-1
#NoTrayIcon
global stdin := FileOpen("*", "r")
global stdout := FileOpen("*", "w")
stdin.Encoding := stdout.Encoding := "UTF-8"
,OnMessage(0x4A, "On_WM_COPYDATA")
SetTimer,main,100
return
main:
if ((receivedMessage := getMessage())="")
return
Send_WM_COPYDATA(WinExist("ahk_exe FirefoxLogin.exe"), 3, receivedMessage)
return
On_WM_COPYDATA(wParam, lParam, msg, hwnd) {
; dwData = Command
; cbData = String lenght
; lpData = String
Critical
dwData := NumGet(lParam+0, 0)
,cbData := NumGet(lParam + A_PtrSize)
,lpData := NumGet(lParam + 2*A_PtrSize)
,str := StrGet(lpData)
If (dwData = 0)
sendMessage(str)
}
; Read a message from stdin and decode it.
getMessage() {
Critical
if (!(messageLength := stdin.ReadUInt()))
return
msg:=""
,stdin.RawRead(&msg, messageLength)
return message:=StrGet(&msg, messageLength, "UTF-8")
}
; Send a message to stdout
sendMessage(message) {
Critical
length:=StrPut(message, "UTF-8")
,msg:=""
,StrPut(message, &msg, "UTF-8")
,stdout.WriteUInt(length)
,stdout.RawWrite(&msg, length)
,stdout.Read(0) ; Flush the write buffer.
}