And while playing around... here is a version which passes commandline parameters to first instance via WM_COPYDATA before exiting...
Compile and start multiple times with different commandline parameters to see whats happening...
#NoTrayIcon
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$hwnd_AutoIt = _EnforceSingleInstance
('e15ff08b-84ac-472a-89bf-5f92db683165') ; any 'unique' string; created with <a href='http://www.guidgen.com/Index.aspx' class='bbc_url' title='External link' rel='norewrite nofollow external'>http://www.guidgen.com/Index.aspx</a>
Opt("TrayIconHide", 1)
$hGUI = GUICreate("My GUI " & $hwnd_AutoIt,300,300,Default,Default) ; will create a dialog box that when displayed is centered
$label = GUICtrlCreateLabel($CmdLineRaw,10,10,300,100)
GUISetState(@SW_SHOW) ; will display an empty dialog box
ControlSetText($hwnd_AutoIt,'',ControlGetHandle($hwnd_AutoIt,'','Edit1'),$hGUI) ; to pass hWnd of main GUI to AutoIt default GUI
GUIRegisterMsg($WM_COPYDATA, "WM_COPYDATA")
While 1
sleep(10)
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()
Func _EnforceSingleInstance
($GUID_Program = "")
If $GUID_Program = "" Then Return
$hwnd = WinGetHandle($GUID_Program)
If IsHWnd($hwnd) Then
$hwnd_Target = ControlGetText($hwnd,'',ControlGetHandle($hwnd,'','Edit1'))
WM
_COPYDATA
_SendData
(HWnd($hwnd_Target), $CmdLineRaw)
Exit
EndIf
AutoItWinSetTitle($GUID_Program)
Return WinGetHandle($GUID_Program)
EndFunc ;==>_EnforceSingleInstance
Func WM
_COPYDATA
($hWnd, $MsgID, $wParam, $lParam)
; <a href='http://www.autoitscript.com/forum/index.php?showtopic=105861&view=findpost&p=747887' class='bbc_url' title='' rel='norewrite'>http://www.autoitscript.com/forum/index.php?showtopic=105861&view=findpost&p=747887</a>
; Melba23, based on code from Yashied
Local $tCOPYDATA = DllStructCreate("ulong_ptr;dword;ptr", $lParam)
Local $tMsg = DllStructCreate("char[" & DllStructGetData($tCOPYDATA, 2) & "]", DllStructGetData($tCOPYDATA, 3))
$msg = DllStructGetData($tMsg, 1)
if $msg = " " then
GUICtrlSetData($label, "")
else
GUICtrlSetData($label, DllStructGetData($tMsg, 1))
endif
Return 0
EndFunc ;==>WM_COPYDATA
Func WM
_COPYDATA
_SendData
($hWnd, $sData)
If Not IsHWnd($hWnd) Then Return 0
if $sData = "" then $sData = " "
Local $tCOPYDATA, $tMsg
$tMsg = DllStructCreate("char[" & StringLen($sData) + 1 & "]")
DllStructSetData($tMsg, 1, $sData)
$tCOPYDATA = DllStructCreate("ulong_ptr;dword;ptr")
DllStructSetData($tCOPYDATA, 2, StringLen($sData) + 1)
DllStructSetData($tCOPYDATA, 3, DllStructGetPtr($tMsg))
$Ret = DllCall("user32.dll", "lparam", "SendMessage", "hwnd", $hWnd, "int", $WM_COPYDATA, "wparam", 0, "lparam", DllStructGetPtr($tCOPYDATA))
If (@error) Or ($Ret[0] = -1) Then Return 0
Return 1
EndFunc ;==>WM_COPYDATA_SendData