Function: Version 7 (31/12/2012) (Save as WM_COPYDATA.au3)
#cs Thanks to the following for helping in the past ... KaFu for the idea from _EnforceSingleInstance(). Yashied for x64 support. #ce #include <WinAPI.au3> #include <WindowsConstants.au3> Global Enum $__hInterCommunicationGUI, $__iInterCommunicationControlID, $__sInterCommunicationIDString, $__sInterCommunicationData, $__iInterCommunicationMax Global $__vInterCommunicationAPI[$__iInterCommunicationMax] ; Internal array for the WM_COPYDATA functions. Func _WM_COPYDATA($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $wParam Local Const $tagCOPYDATASTRUCT = 'ulong_ptr;dword;ptr' Local $tParam = DllStructCreate($tagCOPYDATASTRUCT, $lParam) Local $tData = DllStructCreate('wchar[' & DllStructGetData($tParam, 2) / 2 & ']', DllStructGetData($tParam, 3)) $__vInterCommunicationAPI[$__sInterCommunicationData] = DllStructGetData($tData, 1) GUICtrlSendToDummy($__vInterCommunicationAPI[$__iInterCommunicationControlID]) EndFunc ;==>_WM_COPYDATA Func _WM_COPYDATA_GetData() Local $sReturn = $__vInterCommunicationAPI[$__sInterCommunicationData] $__vInterCommunicationAPI[$__sInterCommunicationData] = '' Return $sReturn EndFunc ;==>_WM_COPYDATA_GetData Func _WM_COPYDATA_GetGUI() Return $__vInterCommunicationAPI[$__hInterCommunicationGUI] EndFunc ;==>_WM_COPYDATA_GetGUI Func _WM_COPYDATA_GetID() Return $__vInterCommunicationAPI[$__sInterCommunicationIDString] EndFunc ;==>_WM_COPYDATA_GetID Func _WM_COPYDATA_Send($sString) If _WM_COPYDATA_GetGUI() = -1 Then Return SetError(1, 0, 0) EndIf If StringStripWS($sString, 8) = '' Then Return SetError(2, 0, 0) EndIf If _WM_COPYDATA_GetGUI() Then Local $tData = DllStructCreate('wchar[' & StringLen($sString) + 1 & ']') DllStructSetData($tData, 1, $sString) Local Const $tagCOPYDATASTRUCT = 'ulong_ptr;dword;ptr' Local $tCOPYDATASTRUCT = DllStructCreate($tagCOPYDATASTRUCT) DllStructSetData($tCOPYDATASTRUCT, 1, 0) DllStructSetData($tCOPYDATASTRUCT, 2, DllStructGetSize($tData)) DllStructSetData($tCOPYDATASTRUCT, 3, DllStructGetPtr($tData)) _SendMessage(_WM_COPYDATA_GetGUI(), $WM_COPYDATA, 0, DllStructGetPtr($tCOPYDATASTRUCT)) Return Number(Not @error) EndIf EndFunc ;==>_WM_COPYDATA_Send Func _WM_COPYDATA_SetGUI($vGUI) $__vInterCommunicationAPI[$__hInterCommunicationGUI] = $vGUI EndFunc ;==>_WM_COPYDATA_SetGUI Func _WM_COPYDATA_SetID($sIDString) $__vInterCommunicationAPI[$__sInterCommunicationIDString] = $sIDString Return $sIDString EndFunc ;==>_WM_COPYDATA_SetID Func _WM_COPYDATA_Shutdown() Local $hHandle = WinGetHandle(AutoItWinGetTitle()) GUIRegisterMsg($WM_COPYDATA, '') GUIDelete(_WM_COPYDATA_GetGUI()) ControlSetText($hHandle, '', ControlGetHandle($hHandle, '', 'Edit1'), '') EndFunc ;==>_WM_COPYDATA_Shutdown Func _WM_COPYDATA_Start($hGUI, $fCheckOnly = Default) Local $hHandle = WinGetHandle(_WM_COPYDATA_GetID()) If @error Then If $fCheckOnly Then Return 0 EndIf AutoItWinSetTitle(_WM_COPYDATA_GetID()) $hHandle = WinGetHandle(_WM_COPYDATA_GetID()) If IsHWnd($hGUI) = 0 Or $hGUI = Default Then $hGUI = GUICreate('', 0, 0, -99, -99, '', $WS_EX_TOOLWINDOW) GUISetState(@SW_SHOW, $hGUI) EndIf ControlSetText($hHandle, '', ControlGetHandle($hHandle, '', 'Edit1'), $hGUI) GUIRegisterMsg($WM_COPYDATA, '_WM_COPYDATA') _WM_COPYDATA_SetGUI(-1) $__vInterCommunicationAPI[$__iInterCommunicationControlID] = GUICtrlCreateDummy() Return $__vInterCommunicationAPI[$__iInterCommunicationControlID] Else $hHandle = HWnd(ControlGetText($hHandle, '', ControlGetHandle($hHandle, '', 'Edit1'))) _WM_COPYDATA_SetGUI($hHandle) Return SetError(1, 0, $hHandle) EndIf EndFunc ;==>_WM_COPYDATA_Start
Example use of Function:
#include <GUIConstantsEx.au3> #include 'WM_COPYDATA.au3' _WM_COPYDATA_SetID('BAC92AA6-FDD8-11E1-91FF-3EE2937D8BEC') Example() Func Example() Local $iControlID = _WM_COPYDATA_Start(Default) ; Start the communication process. If @error Then If Not _WM_COPYDATA_Send($CmdLineRaw) Then ; Send $CmdLineRaw if there is process already running. MsgBox(4096, '2nd Instance: ' & @AutoItPID, 'Seems there was an @error, but more than likely $CmdLineRaw was blank.') EndIf Exit EndIf While 1 Switch GUIGetMsg() Case $iControlID ; If the WM_COPYDATA message is interecepted then show the optimise function. _Optimise() EndSwitch WEnd _WM_COPYDATA_Shutdown() EndFunc ;==>Example Func _Optimise() Return MsgBox(4096, '1st Instance: ' & @AutoItPID, _WM_COPYDATA_GetData()) ; Get the WM_COPYDATA file. EndFunc ;==>_Optimise
Edited by guinness, 31 December 2012 - 11:41 AM.





