Visual Basic (Visual Studio 2005) redirects input for processing

I searched around the net in about three hours without moving. I don't know VB very well, but I need to create a wrapper for any executable that logs arguments, input, output, and error information:

  • My wrapper is called: eg. someApp.exe arg1 arg2
  • Log in to someApp.log: arg1 arg2
  • Calls the original executable: _someApp.exe arg1 arg2
  • Should register and forward any console input to the input stream of the _someApp process
  • Should log any output and error stream from the _someApp process

Ok, I'm stuck at point 4 now:

        Dim p As New ProcessStartInfo
        p.FileName = execute
        p.Arguments = Command()
        p.UseShellExecute = False
        p.CreateNoWindow = True
        p.RedirectStandardInput = True
        p.RedirectStandardError = True
        p.RedirectStandardOutput = True

        Dim process As System.Diagnostics.Process
        process = Diagnostics.Process.Start(p)
        process.WaitForExit()

      

After _someApp completes, I can read and write error messages, but I still need to provide my own wrappers to inject this process, and I want to read and fix the stream as it happens.

Thanks for the info / examples

+2


source to share


2 answers


Ok solution here ...

Required variables:

Private process As System.Diagnostics.Process

Private threadOut As Thread

Private streamOut As System.IO.StreamReader

Private threadErr As Thread

Private streamErr As System.IO.StreamReader

Private threadIn As Thread

Private streamIn As System.IO.StreamWriter

      

Required items:

Private Sub ThreadTaskOut()
    Dim line
    While Not process.HasExited
        line = streamOut.ReadToEnd
        If line <> Nothing And line <> "" Then
            log("Out: " & line)
            Console.Out.Write(line)
        End If
    End While
End Sub

Private Sub ThreadTaskErr()
    Dim line
    While Not process.HasExited
        line = streamErr.ReadToEnd
        If line <> Nothing And line <> "" Then
            log("Err: " & line)
            Console.Error.Write(line)
        End If
    End While
End Sub

Private Sub ThreadTaskIn()
    Dim line
    While Not process.HasExited
        line = Console.In.ReadLine
        If line <> Nothing And line <> "" Then
            log("In: " & line)
            streamIn.WriteLine(line)
        End If
    End While
End Sub

      

Inside main:



        ' create process information
        Dim p As New ProcessStartInfo
        p.FileName = execute
        p.Arguments = Command()
        p.UseShellExecute = False
        p.CreateNoWindow = True
        p.RedirectStandardInput = True
        p.RedirectStandardError = True
        p.RedirectStandardOutput = True

        ' log process start
        log("Execute: " & execute & " " & Command())

        ' start process
        process = Diagnostics.Process.Start(p)

        ' start thread for output stream
        streamOut = process.StandardOutput
        threadOut = New Thread(AddressOf ThreadTaskOut)
        threadOut.IsBackground = True
        threadOut.Start()

        ' start thread for error stream
        streamErr = process.StandardError
        threadErr = New Thread(AddressOf ThreadTaskErr)
        threadErr.IsBackground = True
        threadErr.Start()

        ' start thread for input stream
        streamIn = process.StandardInput
        threadIn = New Thread(AddressOf ThreadTaskIn)
        threadIn.IsBackground = True
        threadIn.Start()

        ' wait for the process to finish
        process.WaitForExit()

      

log is another item to write to the file, execution is a variable containing the original post _someApp.exe. I still don't know if the console supports input stream because my shell app doesn't have input as it seems. Maybe someone throws an error ...

For my purposes, hmm, it works like I need it

Greetz, GHAD

Code inside main

+1


source


How about writing an application:

Dim sw as IO.StreamWriter = process.StandardInput
sw.WriteLine("Boo")

      



and to read from standard output:

Dim sr As IO.StreamReader = process.StandardOutput
Do
  aString = sr.ReadLine()
Loop Until (sr.EndOfStream)

      

0


source







All Articles