Visual Basic Capture output of cmd

More than one problem. First off, as @shf301 already told you, you forgot to read stderr. He in turn forgot to add an extra line:

    Process.Start()
    AddHandler Process.OutputDataReceived, _
       Sub(processSender As Object, lineOut As DataReceivedEventArgs)
           output += lineOut.Data + vbCrLf
       End Sub
    Process.BeginOutputReadLine()
    AddHandler Process.ErrorDataReceived, _
       Sub(processSender As Object, lineOut As DataReceivedEventArgs)
           output += lineOut.Data + vbCrLf
       End Sub
    Process.BeginErrorReadLine()

There’s another very cumbersome problem, your event handlers run late. They fire after the process has already exited. A side effect of these handlers running on a thread-pool thread. You’ll need to wait for an arbitrary (and unguessable) amount of time before you use the output variable:

    Do
        Application.DoEvents()
    Loop Until Process.HasExited
    System.Threading.Thread.Sleep(1000)

This is too ugly. Do this the way that any IDE or editor does it. Redirect the output to a temporary file and read the file afterwards:

    Dim tempfile As String = System.IO.Path.GetTempFileName
    Using Process As New Process
        Process.StartInfo = New ProcessStartInfo("cmd.exe")
        Process.StartInfo.Arguments = "/c make 1> """ + tempfile + """ 2>&1"
        Process.StartInfo.WorkingDirectory = "C:\projectTest"
        Process.StartInfo.UseShellExecute = False
        Process.StartInfo.CreateNoWindow = True
        Process.Start()
        Process.WaitForExit()
        output = System.IO.File.ReadAllText(tempfile)
        System.IO.File.Delete(tempfile)
    End Using

Some annotation with the mystic command line:

  • /c tells cmd.exe to execute just the single command and then exit
  • 1> redirects the output to the temporary file
  • 2>&1 tells cmd.exe to redirect stderr to stdout
  • the triple double quotes ensures that spaces in the tempfile name don’t cause trouble.

That same 2>&1 would also have fixed your original problem 😉

Leave a Comment