I wrote an example which sends random data from any additional instance back to the first instance.
Random data are the two numbers
555 and
789.
Code: Select all
#NoTrayIcon
#SingleInstance, Off
MutexName := "random"
If DllCall("OpenMutex", Int, 0x100000, Int, 0, Str, MutexName)
Gosub, AlreadyRunning
Menu, Tray, Icon ; show
hMutex := DllCall("CreateMutex", Int, 0, Int, False, Str, MutexName)
OnMessage(0x5678, "Receiver")
OnExit, ReleaseMutex
Return
;-------------------------------------------------------------------------------
ReleaseMutex: ; clean up
;-------------------------------------------------------------------------------
DllCall("ReleaseMutex", Ptr, hMutex)
ExitApp
;-------------------------------------------------------------------------------
Receiver(wParam, lParam) { ; handle incoming messages from additional instances
;-------------------------------------------------------------------------------
MsgBox,, Receiver, wParam: %wParam%`nlParam: %lParam%
}
;-------------------------------------------------------------------------------
AlreadyRunning: ; additional instances run this subroutine
;-------------------------------------------------------------------------------
DetectHiddenWindows, On
WinGet, hwnd, List, %A_ScriptFullPath% ahk_class AutoHotkey
Loop, %hwnd%
If (hwnd%A_Index% != A_ScriptHwnd)
; send random data to first instance
PostMessage, 0x5678, 555, 789,, % "ahk_id " hwnd%A_Index%
ExitApp ; no return from here
In this example I use a MsgBox as an indicator. This is bad for the following reason:
When you start the script a third time while the "MsgBox from the second" is still displayed, the message from the third script is lost.