0

I have spent the past three hours looking for a solution but have only found solutions that output the contents of CMD once the process has finished, not while it is still running.

My code currently is as follows:

Public Class main_form
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim process_cmd As ProcessStartInfo = New ProcessStartInfo("cmd.exe")
    Dim process_obj As Process
    process_cmd.CreateNoWindow = True
    process_cmd.UseShellExecute = False
    process_cmd.RedirectStandardInput = True
    process_cmd.RedirectStandardOutput = True
    process_obj = Process.Start(process_cmd)
    process_obj.StandardInput.WriteLine("my command")
    process_obj.StandardInput.Close()
    RichTextBox1.Text = process_obj.StandardOutput.ReadToEnd
    process_obj.StandardOutput.Close()
 End Sub
End Class

Now this code works fine and all however it only outputs to the Richtextbox once the process in CMD has finished. I want the Richtextbox to display the contents of the CMD window in real time as well as for the process to accept additional commands (e.g status of the current process) while it is still running, as if the Richtextbox is the CMD window itself in terms of output (essentially mirroring the CMD window).

I want to do this so that i can then manipulate the contents of the output using my application and then input more parameters into the CMD process based on this. (For example running a server and changing certain parameters while it is still running.)

EDIT:

I have been almost able to accomplish my task by using redirect standard output, something which I was unaware existed until another user brought it to my attention.

Below is my new code:

Public Class main_form
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim p As New Process()
    p.StartInfo.FileName = "mycmdexecutable"
    p.StartInfo.Arguments = ""
    p.StartInfo.WorkingDirectory = ""
    p.StartInfo.RedirectStandardError = True
    p.StartInfo.RedirectStandardOutput = True
    p.EnableRaisingEvents = True
    p.StartInfo.UseShellExecute = False
    Application.DoEvents()
    AddHandler p.ErrorDataReceived, AddressOf p_OutputDataReceived
    AddHandler p.OutputDataReceived, AddressOf p_OutputDataReceived
    p.Start()
    p.BeginErrorReadLine()
    p.BeginOutputReadLine()

End Sub

Delegate Sub UpdateTextBoxDelg(text As String)
Public myDelegate As UpdateTextBoxDelg = New UpdateTextBoxDelg(AddressOf UpdateTextBox)
Public Sub UpdateTextBox(text As String)
    RichTextBox1.Text += text & Environment.NewLine
    RichTextBox1.SelectionStart = RichTextBox1.Text.Length
    RichTextBox1.ScrollToCaret()
End Sub

Public Sub p_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
    If Me.InvokeRequired = True Then
        Me.Invoke(myDelegate, e.Data)
    Else
        UpdateTextBox(e.Data)
    End If
End Sub

This code works fine for displaying the CMD output to the richtextbox I have. However two new issues have cropped up.

  • I'm having trouble now inputting data into the process that I am running, I have absolutely no clue as to where to start and the existing examples I have found online are too complex for me to understand.

  • My output into the richtextbox includes some invalid characters and characters which were not present in the CMD window (E.g '[0m'). I initially thought that this was an encoding error with the richtextbox however that does not seem to be the case and I do not know how to progress further.

Once again thank you for your help!

  • What if you redirect standard out to a file and read from the file? – PerpetualStudent Nov 05 '17 at 20:13
  • Thank you for bringing this to my attention. @PerpetualStudent I have incorporated redirect standard output into my code. I would appreciate it if you could have a look at the new information that I have provided. – Parth Kataria Nov 05 '17 at 21:45
  • Not sure if this will work, but what if you have your cmd run a bat file, and then add more commands to the bat file. Sort of the reverse of redirecting standard out, redirecting standard in. Check out this link: https://stackoverflow.com/questions/906586/changing-a-batch-file-when-its-running – PerpetualStudent Nov 05 '17 at 22:01
  • [0m is an ANSI code to clear all ANSI attributes. You can remove that when dysplaying. yourstring.replace("[0m","") http://ascii-table.com/ansi-escape-sequences.php – Chillzy Nov 06 '17 at 04:10

1 Answers1

0

Use ProcessWriter to send your input to your process

Public Class main_form

Private ProcessWriter As StreamWriter 'add this to global declare

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim p As New Process()
    p.StartInfo.FileName = "mycmdexecutable"
    p.StartInfo.Arguments = ""
    p.StartInfo.WorkingDirectory = ""
    p.StartInfo.RedirectStandardError = True
    p.StartInfo.RedirectStandardOutput = True
    p.StartInfo.RedirectStandardInput = True  'added this to redirect your input
    p.EnableRaisingEvents = True
    p.StartInfo.UseShellExecute = False
    Application.DoEvents()
    AddHandler p.ErrorDataReceived, AddressOf p_OutputDataReceived
    AddHandler p.OutputDataReceived, AddressOf p_OutputDataReceived
    p.Start()
    ProcessWriter = p.StandardInput
    p.BeginErrorReadLine()
    p.BeginOutputReadLine()

End Sub
Chillzy
  • 468
  • 3
  • 9