I made this UDF for making my scripts being able to communicate with each other.
It uses a file to store data and check data for the command transfer.
This script will execute your function when your program got a command.
;Functions: ; _CommunicateStartup ; _CommunicateSend ; _CommunicateGetCommands ; _CommunicateBroadcast ; _CommunicateRenewFile ; _CommunicateGetUsers ; _CommunicateEnd
UDF:
#include-once ;Be sure to not use these variables in your script: global $sInit, $sData1, $sData2, $szgCommandings[3], $xProgramFile = @WindowsDir&'\CommunicationPrograms.cmf', $xyzAdlibFunc='', $rpdHandle[3], $lrszSpeed='' global $yzlMaxLines = 200 ;Max lines of the communication files. If it contains more lines than this var the file will be renewed/cleared with _CommunicateRenewFile ;^ Set to -1 if you do not want this to be done automaticly ;Functions: ; _CommunicateStartup ; _CommunicateSend ; _CommunicateGetCommands ; _CommunicateBroadcast ; _CommunicateRenewFile ; _CommunicateGetUsers ; _CommunicateEnd ;============================================================================= ; Description: Starts up the communication ; Version: 1.0.0 ; Syntax: _CommunicateStartup($sApp, $sFunction, $hFile=1, $xSpeed=200) ; ; Parameter(s): $sApp = The name of your program/app, you'll this need when using _CommunicateSend ; $sFunction = The name of the function that will be called when a command has been send to you, The function should look like this: ; _Function($command). The $command is an array. $command[1] contains the command, $command[2] contains the name of the ; name of the program that send it to you. ; $hFile = The file that you want to use for the communication. Default is: @WindowsDir&'\CommunicationFile.cmf' ; $xSpeed = The speed that you want to use for getting commands. Default is: 200, (recommended) ; Requirement(s): - ; Return Value(s): On Success - Returns a handle you'll need for other functions. ; On Failure - Returns an empty string and sets @Error on errors ; @Error=1 $sApp, $sFunction, $hFile or/and $xSpeed is/are containing nothing. ; @Error=2 Could not create communication file ; @Error=3 Could not create program file ; @Error=4 Could not write $sApp to program file ; Author(s): Ludocus <ludovic1993@hotmail.com> ; Note(s): This works like an OnEvent function using AdlibRegister and executing your function when a command has been send to you. ; Do not use this function more than once in your script. ;=============================================================================== Func _CommunicateStartup($sApp, $sFunction, $hFile=1, $xSpeed=200) if $sApp = '' or $sFunction = '' or $hFile = '' or $xSpeed = '' Then return SetError(1) if $hFile = 1 then $hFile = @WindowsDir&'\CommunicationFile.cmf' $lrszSpeed = $xSpeed if not FileExists($hFile) then If not FileWrite($hFile, '-start communicate file-') then return SetError(2) EndIf if not FileExists($xProgramFile) then If not FileWrite($xProgramFile, '-programs running-') then return SetError(3) EndIf $xyzAdlibFunc=$sFunction if not FileWrite($xProgramFile, @CRLF&$sApp) then return SetError(4) AdlibRegister('_CommunicateAdlib', $lrszSpeed/2) $rpdHandle[1] = $hFile $rpdHandle[2] = $sApp return $rpdHandle EndFunc ;============================================================================= ; Description: Sends a command to an app ; Version: 1.0.0 ; Syntax: _CommunicateSend($sHandle, $sToApp, $xCommand) ; ; Parameter(s): $sHandle = The handle returned from _CommunicateStartup ; $sToApp = The name of the app/program you want to send a command ; $xCommand = The command you want to send ; Requirement(s): - ; Return Value(s): On Success - Returns 1 ; On Failure - Returns 0 or sets error to: ; @Error=1 $sHandle isn't an array ; Author(s): Ludocus <ludovic1993@hotmail.com> ; Note(s): ;=============================================================================== Func _CommunicateSend($sHandle, $sToApp, $xCommand) if not IsArray($sHandle) then return SetError(1) $hFile = $sHandle[1] $sApp = $sHandle[2] $sRet = FileWrite($hFile, @CRLF&$sToApp&'->'&$xCommand&'<-'&$sApp) return $sRet EndFunc ;============================================================================= ; Description: Returns all raw commands that have been send since last file creation/renewing ; Version: 1.0.0 ; Syntax: _CommunicateGetCommands($sHandle) ; ; Parameter(s): $sHandle = The handle returned from _CommunicateStartup ; Requirement(s): - ; Return Value(s): On Success - Returns all raw commands ; On Failure - Returns 0 or sets error to: ; @Error=1 $sHandle isn't an array ; Author(s): Ludocus <ludovic1993@hotmail.com> ; Note(s): Returns raw commands, for example: toApp->Command<-App ;=============================================================================== Func _CommunicateGetCommands($sHandle) if not IsArray($sHandle) then return SetError(1) $hFile = $sHandle[1] $sApp = $sHandle[2] $sData = FileRead($hFile) Return $sData EndFunc ;============================================================================= ; Description: Broadcasts a command to all programs ; Version: 1.0.0 ; Syntax: _CommunicateBroadcast($sHandle, $xCommand) ; ; Parameter(s): $sHandle = The handle returned from _CommunicateStartup ; $xCommand = The command you want to send ; Requirement(s): - ; Return Value(s): On Success - Returns 1 ; On Failure - Returns 0 or sets error to: ; @Error=1 $sHandle isn't an array ; Author(s): Ludocus <ludovic1993@hotmail.com> ; Note(s): ;=============================================================================== Func _CommunicateBroadcast($sHandle, $xCommand) Return _CommunicateSend($sHandle, '=(++[-ALL-]++)=', $xCommand) EndFunc ;============================================================================= ; Description: Renews the communication file (clears it) ; Version: 1.0.0 ; Syntax: _CommunicateRenewFile($sHandle) ; ; Parameter(s): $sHandle = The handle returned from _CommunicateStartup ; Requirement(s): - ; Return Value(s): On Success - Returns 1 ; On Failure - Returns 0 = Was not able to FileDelete or FileWrite $hFile ; - Returns -1 = Was not able to FileDelete and FileWrite $hFile ; Author(s): Ludocus <ludovic1993@hotmail.com> ; Note(s): Use this function when you think the communication file is getting too big. ; This function will be called automaticly when the amount of lines of the file is higher than $yzlMaxLines ;=============================================================================== Func _CommunicateRenewFile($sHandle) $hFile = $sHandle[1] $sRet = _CommunicateBroadcast($sHandle, '--RENEWING FILE--') $sRet -= FileDelete($hFile) $sRet -= FileWrite($hFile, '-start communicate file-') return $sRet EndFunc ;============================================================================= ; Description: Gets the users using the communication right now ; Version: 1.0.0 ; Syntax: _CommunicateGetUsers($sHandle) ; ; Parameter(s): $sHandle = The handle returned from _CommunicateStartup ; Requirement(s): - ; Return Value(s): On Success - Returns 1 ; On Failure - Returns 0 ; Author(s): Ludocus <ludovic1993@hotmail.com> ; Note(s): Could be wrong when a program did not call _CommunicateEnd at exit ;=============================================================================== Func _CommunicateGetUsers($sHandle) return FileRead($xProgramFile) EndFunc ;============================================================================= ; Description: Ends the communication ; Version: 1.0.0 ; Syntax: _CommunicateEnd($sHandle) ; ; Parameter(s): $sHandle = The handle returned from _CommunicateStartup ; Requirement(s): - ; Return Value(s): On Success - Returns 1 ; On Failure - Returns 0 ; Author(s): Ludocus <ludovic1993@hotmail.com> ; Note(s): Call this function on exit. ; When this function is not called on exit the _CommunicateGetUsers will get the wrong users. ;=============================================================================== Func _CommunicateEnd($sHandle) AdlibUnRegister($xyzAdlibFunc) $hFile = $sHandle[1] $sApp = $sHandle[2] $ySplit = StringSplit(FileRead($xProgramFile), @CRLF, 1) $sFileData = '' For $i = 1 to $ySplit[0] if $ySplit[$i] <> $sApp Then if $sFileData = '' Then $sFileData = $ySplit[$i] Else $sFileData &= @CRLF&$ySplit[$i] EndIf EndIf Next FileDelete($xProgramFile) Return FileWrite($xProgramFile, $sFileData) EndFunc ;Do not use these functions: ;----------------------------------- Func _CommunicateListener($sHandle, $sSpeed=200) if not IsArray($sHandle) then return SetError(1) if $xyzAdlibFunc='' then return SetError(2) if $sSpeed <= 10 then return SetError(3) $lCommand = 0 $hFile = $sHandle[1] $sApp = $sHandle[2] if $sInit = '' Then $sInit = TimerInit() $sData1 = FileRead($hFile) Else $sDiff = TimerDiff($sInit) if $sDiff >= $sSpeed Then $sData2 = FileRead($hFile) if $sData1 <> $sData2 Then $zSplit = StringSplit($sData2, @CRLF, 1) $zSplit2 = StringSplit($zSplit[$zSplit[0]], '->', 1) if $zSplit2[1] = $sApp or $zSplit2[1] = '=(++[-ALL-]++)=' then $zSplit3 = StringSplit($zSplit2[2], '<-', 1) if $zSplit3[2] <> '--RENEWING FILE--' Then $szgCommandings[1] = $zSplit3[1] $szgCommandings[2] = $zSplit3[2] Execute($xyzAdlibFunc&'($szgCommandings)') Else $sData2 = '-start communicate file-' EndIf EndIf EndIf $sData1 = $sData2 EndIf EndIf return $lCommand EndFunc Func _CommunicateAdlib() if $yzlMaxLines <> -1 and __FileCountLines($rpdHandle[1]) > $yzlMaxLines then _CommunicateRenewFile($rpdHandle) if $rpdHandle <> '' and $lrszSpeed <> '' Then _CommunicateListener($rpdHandle, $lrszSpeed) EndIf EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: __FileCountLines ; Description ...: Returns the number of lines in the specified file. ; Syntax.........: __FileCountLines($sFilePath) ; Parameters ....: $sFilePath - Path and filename of the file to be read ; Return values .: Success - Returns number of lines in the file. ; Failure - Returns a 0 ; @Error - 0 = No error. ; |1 = File cannot be opened or found. ; Author ........: Tylo <tylo at start dot no> ; Modified.......: Xenobiologist, Gary ; Remarks .......: It does not count a final @LF as a line. ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func __FileCountLines($sFilePath) Local $hFile = FileOpen($sFilePath, 0) If $hFile = -1 Then Return SetError(1, 0, 0) Local $sFileContent = StringStripWS(FileRead($hFile), 2) FileClose($hFile) Local $aTmp If StringInStr($sFileContent, @LF) Then $aTmp = StringSplit(StringStripCR($sFileContent), @LF) ElseIf StringInStr($sFileContent, @CR) Then $aTmp = StringSplit($sFileContent, @CR) Else If StringLen($sFileContent) Then Return 1 Else Return SetError(2, 0, 0) EndIf EndIf Return $aTmp[0] EndFunc ;==>_FileCountLines ;-----------------------------------
Example:
#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.6.1 Author: Ludocus Script Function: Explaining the Communicate UDF #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here #include <Communicate.au3> #include <GUIConstants.au3> #include <WindowsConstants.au3> $1 = '' $_ = _Stripe() $Handle = _CommunicateStartup('Example', '_GotCommand') ;Example = name of app, _GotCommand = Function that'll be called when we got a command! $Form1 = GUICreate("Example", 560, 310, 193, 125) $Label1 = GUICtrlCreateLabel("Program:", 16, 24, 46, 17) $Input1 = GUICtrlCreateInput("Leave blank for broadcast", 16, 48, 121, 21) $Label2 = GUICtrlCreateLabel("Command:", 16, 96, 54, 17) $Input2 = GUICtrlCreateInput("", 16, 120, 121, 21) $Button1 = GUICtrlCreateButton("Send", 32, 160, 75, 25, 0) $Edit1 = GUICtrlCreateEdit("Communication:"&@CRLF&$_, 200, 0, 361, 313) GUISetState(@SW_SHOW) $x = TimerInit() $y = 0 While 1 if TimerDiff($x) > $y + 200 Then _Refresh() $y = TimerDiff($x) EndIf $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 if GUICtrlRead($Input1) = '' Then _CommunicateBroadcast($Handle, GUICtrlRead($Input2)) Else _CommunicateSend($Handle, GUICtrlRead($Input1), GUICtrlRead($Input2)) EndIf GUICtrlSetData($Input1, '') GUICtrlSetData($Input2, '') EndSwitch WEnd Func _Refresh() if $1 = '' Then $1 = _CommunicateGetCommands($Handle) Else $2 = _CommunicateGetCommands($Handle) if $1 <> $2 then $splz = StringSplit($2, @CRLF, 1) GUICtrlSetData($Edit1, GUICtrlRead($Edit1)&@CRLF&$splz[$splz[0]]&@CRLF&$_) EndIf $1 = $2 EndIf EndFunc Func _GotCommand($command) ;This function will be called when you got a command! ;$command[1] = Command ;$command[2] = Program that send the command GUICtrlSetData($Edit1, GUICtrlRead($Edit1)&@CRLF&'You got a command from: '&$command[2]&@CRLF&'The command is: '&$command[1]&@CRLF&$_) EndFunc Func _Exit() Exit EndFunc Func _Stripe($x=112) $p = '' For $i = 1 to $x $p &= '-' Next return $p EndFunc
I hope you like it and find it useful.
Ludocus
Edited by ludocus, 20 September 2010 - 03:16 PM.










