interacting with a process

Hello, I am attempting to interact with a process that I launch. I need to monitor its output and enter key strokes in response to the output from the process. I cannot wait for the process to exit before seeing and responding to its output. Here is a simple example of what I am talking about as what you would see if using a cmd window:

C:\STMFlashLoader.exe -c --pn 17 --br 115200
Opening Port              [OK]
 Press any key to continue...

I have code working that allows me to monitor the output, but am unable to "send keys" with stream or sendkeys. I would like a structure such that I can monitor the output and when I see certain strings, respond by sending an appropriate string back to the process, and continue to monitor and again, send an appropriate string until the process exits or I terminate the process.

Here is the code I have working at this point

Dim start_info As New ProcessStartInfo("C:\Program Files\STMicroelectronics\Software\Flash Loader Demonstrator\STMFlashLoader.exe")
start_info.Arguments = "-c --pn 17 --br 115200"
start_info.UseShellExecute = False
start_info.CreateNoWindow = True
start_info.RedirectStandardOutput = True
start_info.RedirectStandardError = True

 ' Make the process and set its start information.
  Dim proc As New Process()
  proc.StartInfo = start_info

    ' Start the process.

  ' Attach to stdout and stderr.
  Dim std_out As StreamReader = proc.StandardOutput()
   Dim std_err As StreamReader = proc.StandardError()

        ' Display the results.
  txtStdout.Text = std_out.ReadToEnd()
   txtStderr.Text = std_err.ReadToEnd()

    ' Clean up.

Much thanks,

Who is Participating?
mastooConnect With a Mentor Commented:
You'll need asynchronous redirection.  If you look at your VS help, Process.BeginOutputReadLine has an example.  It is complicated, but I think it is the only way to do what you describe.  I'm assuming here that you don't have the option of modifying the console app that you are invoking.
Mohamed AbowardaSoftware EngineerCommented:
To get the keystrokes, get the window handle and whenever the window is active use GetAsyncKeyState API:
i think this depends on the application, what if the process is windowless?!
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

bartdspAuthor Commented:

Note sure I understand your question. The exe(STMFlashLoader.exe) that I "call", does have a window(DOS) when called from the command line, but if you are asking  if it needs to be visible, the answer is no. Let me know if that answers your question.


so, how are you to record actions on it if it is not visible?
bartdspAuthor Commented:
As you can see from my code example I I can monitor the output via re-directed stdout. In this case I have it sent to a text box. This could just as easily be directed to string (buffer) where I could monitor the output instead of a textbox.

Make sense?
I think we're saying it is a console application so windows messaging doesn't help.  Interprocess Communication can range from writing to a file (and the other app polls the file), to sockets or named pipes, or you could make a remoting server component.  I'd be inclined to just poll a file unless you need a better response, then named pipes are old fasioned but easy to do.  You can easily use a named mutex with a file to avoid the two programs running into each other on file access.
bartdspAuthor Commented:
Besides needing to grab the data (file, pipes, sockets, etc) there is the need to send  characters to the console application.
Can't say I'm an expert in this area but I think the only way to do that is your app needs to redirect stdin also.  Your app can then feed characters to stdin of the console app.  So now the question becomes, how does your app know what to feed it?  It either gets this from elsewhere (a file, pipes, sockets, etc.) or it could easily be a gui that is displaying stdout results and accepting or deciding on things to send to stdin.
bartdspAuthor Commented:
I was attempting to re-direct stdin but was having problems with implementation. I would imagine the code would spin in a loop while the app is still responding, looking for (for example): "press any key to continue" If that string is found, the app would send a space key, then continue to look for similar messages.

The 2 basic criteria: Receive data while the console app is running(not after it has terminated) and be able to send characters to the console app.

I apologise, my comment was for Medo3337.

Here is how I'd do it in AutoIT which is based on the same principal, basically capture the output in a loop and as you suggested send keys then wipe the output so that it doesn't continue to hit the send key.

For more instructions just use elseif StringInStr($CHK_FLASHLOADER, .... etc.. and add other send keys.

Hope that made sense.

#include <Constants.au3>

$RUN_FLASHLOADER = Run('"' & @ProgramFilesDir & '\STMicroelectronics\Software\Flash Loader Demonstrator\STMFlashLoader.exe" -c --pn 17 --br 115200', '', @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

	While 1
		If @error Then ExitLoop
		If StringInStr($CHK_FLASHLOADER, 'Press any key to continue...') Then

	While 1
		If @error Then ExitLoop

Open in new window

bartdspAuthor Commented:

I tried the following and am still unable to monitor the process until after the exe that I am calling has exited. Note that I have a timer firing every 200 ms in an attempt to gather the data and display. I am not yet attempting to send keys. If I send the exe name and parameters in a dos window I get streaming data in a dos window for 20 seconds telling me about the process. If sending via my code I get the data only after 20 seconds has elapsed.


Sub ProcessTest()

            ' Initialize the process and its StartInfo properties.
            Dim myProcess As New Process()
            Dim numInputLines As Integer = 0

            Bigmessage = ""

            myProcess.StartInfo.FileName = "C:\Program Files\STMicroelectronics\Software\Flash Loader Demonstrator\STMFlashLoader.exe"
            myProcess.StartInfo.Arguments = "-c --pn 17 --br 115200 -i STM32_Med-density_128K -u --fn c:\qaz.hex"

            ' Set UseShellExecute to false for redirection.
            myProcess.StartInfo.UseShellExecute = False

            ' Redirect the standard output of the sort command.  
            ' Read the stream asynchronously using an event handler.
            myProcess.StartInfo.RedirectStandardOutput = True
            sortOutput = New StringBuilder()

            ' Set our event handler to asynchronously read the sort output.
            AddHandler myProcess.OutputDataReceived, AddressOf MyOutputHandler

            ' Redirect standard input as well.  This stream is used synchronously.
            myProcess.StartInfo.RedirectStandardInput = True

            ' Start the process.

            ' Use a stream writer to synchronously write .
            Dim myStreamWriter As StreamWriter = myProcess.StandardInput

            ' Start the asynchronous read of the sort output stream.

            ' End the input stream to the sort command.

            ' Wait



            MsgBox("Process test: " & Err.Description)
        End Try

    End Sub

    Sub MyOutputHandler(ByVal sendingProcess As Object, ByVal outLine As DataReceivedEventArgs)

            Dim msg As String


            If outLine.Data.ToString = Nothing Then Exit Sub
            msg = outLine.Data.ToString

            If Not msg = "" Then

                Bigmessage = Bigmessage & msg
                numOutputLines = numOutputLines + 1

            End If


            MsgBox("Handler: " & Err.Description)

        End Try

    End Sub
mastooConnect With a Mentor Commented:
This runs from a gui with a timer that is checking Bigmessage?

Two things I note: I assume MyOutputHandler gets its callback from a background thread, so DoEvents and MsgBox shouldn't be used there.  And is the other app looking for input on stdin?  I'm not sure how it will react but you close the redirected input stream immediately.  This is why I said it would be complicated.  I'm thinking you need to keep the input stream open while watching the output from stdout, and respond on stdin based on what stdout does.
bartdspAuthor Commented:
All, I ended up getting my hands on the source for the console app and modified so that I did not need to send it any key info. Then I used a process and a handler:

            ' define a process
            Dim p As New Process

            p.StartInfo.FileName = filename
            p.StartInfo.Arguments = parametrers

            ' setup
            p.StartInfo.UseShellExecute = False
            p.StartInfo.RedirectStandardOutput = True
            p.StartInfo.CreateNoWindow = True

            ' add a handler so we can easily display the data
            AddHandler p.OutputDataReceived, AddressOf Showme


            ' spin here till done or cancel
            Do While (p.HasExited = False) And (CANCEL = False)
                ' monitor progress

            If CANCEL = True Then
                Return False
            End If

            Return true

    End Function
All Courses

From novice to tech pro — start learning today.