Sign in to follow this  
Followers 0
Taek

CmdLine To Running Proccess

5 posts in this topic

#1 ·  Posted (edited)

I am attempting to do something that will help further my downloader project for a larger WebSite File Sharing Project.

What i need is is away to check and see if there is a Instance of the application running (ie downloader.exe) if so pass the CmdLine[1] variable to the running proccess. Now in the running proccess is should wait for a CmdLine[1] to hit it's running process and do things from there. I am currently working on an example of what i want it to to, but i'm stuck at trying to check if there is a running process.

Sample Script

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt('MustDeclareVars', 1)

 Example1()    ; example 1

Func Example1()
Local $msg
Local $msgoutput

GUICreate("My GUI") ; will create a dialog box that when displayed is centered
$msgoutput = GUICtrlCreateEdit( "" , 5, 5, 390, 310, 0x0840 + $WS_VSCROLL)
GUISetState(@SW_SHOW) ; will display an empty dialog box
GUICtrlSetData($msgoutput, $CmdLine[0], 1)      ; Run the GUI until the dialog is closed

While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()
EndFunc   ;==>Example1

Another note.

The information passed from CmdLine[1] will be displayed in the $msgoutput portion of the GUI (this is to make sure it actually works.)

Any help would be appreciated and added to the Developer's Credit page on my project website :D

Edited by Taek

Share this post


Link to post
Share on other sites



I wanted to try establishing a loopback connection and use it for interop on the local system. I read about benefits of doing so in an article, but now I can't remember where I saw that article. 8)

Anyway, run this script. Wait for the GUI to appear, then run this script again. You'll see the GUI updates.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>

Local Const $port = 49283
Local $mainsocket, $socket = -1

Local $msg
Local $msgoutput

Local $isUnique

_d("Starting up TCP")
TCPStartup()

$isUnique = _Singleton("SomeDownloader", 1) <> 0
_d("This is a unique process = " & $isUnique)

If Not $isUnique Then
    
    _SendToListener("Some data from command line")
    
    _d("Exiting script")
    Exit
EndIf

_CreateListener()
    
_d("Creating GUI")
GUICreate("My GUI") ; will create a dialog box that when displayed is centered
$msgoutput = GUICtrlCreateEdit( "" , 5, 5, 390, 310, 0x0840 + $WS_VSCROLL)
GUISetState(@SW_SHOW) ; will display an empty dialog box

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    
    _CheckListener()
WEnd

Func _SendToListener($data)
    _d("Sending data to listener. Data=" & $data)
    
    $socket = TCPConnect(@IPAddress1, $port)
    _HandleError(@error, @ScriptLineNumber)
    
    Sleep(500)
    $n = TCPSend($socket, $data)
    _HandleError(@error, @ScriptLineNumber)
    
EndFunc

Func _CreateListener()
    _d("Creating listener")
    $mainsocket = TCPListen(@IPAddress1, $port)
    _HandleError(@error, @ScriptLineNumber)
    
EndFunc

Func _CheckListener()
    If $socket = -1 Then
        $socket = TCPAccept($mainsocket)
    Else
        $data = TCPRecv($socket, 2048)
        If $data <> "" Then
            _ProcessData($data)
            _CloseListener()
        EndIf
    EndIf
EndFunc

Func _CloseListener()
    _d("Closing listener")
    TCPCloseSocket($socket)
    _HandleError(@error, @ScriptLineNumber)
    $socket = -1
EndFunc

Func _ProcessData($data)
    _d("Processing data: " & $data)
    GUICtrlSetData($msgoutput, $data, 1)
EndFunc

Func _d($s) ; Debug func
    ConsoleWrite($s & @CRLF)
EndFunc

Func _HandleError($err, $line)
    If ($err) Then
        _d("Error " & $err & " occured at line " & $line)
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I'm not sure why you're using TCP functions at all. i mean i can understand that if the port is taken than it can't do anything if a second process runs but at the sametime that looks abit more complicated to be able to implement into something that's already running without this tcp deal.

however i'm not saying it doesn't work.....

Edited by Taek

Share this post


Link to post
Share on other sites

In order to do interop between processes, at least one of the two processes will need to be taught how to receive input. To receive input from other processes you only have a few options in AutoIt. In my opinion, the two most important and workable ones are:

GUI interop using SendMessage.

TCP interop.

In my views, using TCP there are a lot of benefits over using GUIs and SendMessage.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0