1

I am basically trying to somehow run a java console-based program inside vb.net and display the output in let's say a normal textbox and also have another textbox where i could type in input to the program. Is it possible to do this?

Visual Magic
  • 479
  • 6
  • 13

2 Answers2

1

You would basically need to create a new process with parameters, which will be the input from your textbox. Then, you could capture the program's output, store it in a variable and show it in a dialog.

Here's a small snippet:

Dim command As String = "C:\My Dir\MyFile.exe"
Dim args As String = "MyParam1 MyParam2"

Dim proc = New Process() With { _
  Key .StartInfo = New ProcessStartInfo() With { _
    Key .FileName = "program.exe", _
    Key .Arguments = args, _
    Key .UseShellExecute = False, _
    Key .RedirectStandardOutput = True, _
    Key .CreateNoWindow = True _
  } _
}

proc.Start()

While Not proc.StandardOutput.EndOfStream
  Dim line As String = proc.StandardOutput.ReadLine()
  ' do something with the line
End While
Filippos Karapetis
  • 4,367
  • 21
  • 39
  • Thank you! Is there also a way to GIVE input to the console? – Visual Magic Jan 26 '14 at 17:53
  • You're welcome :) well, you could use command-line arguments to start your program (as mentioned above). Then, you could send keystrokes to the spawned process. Check this question for a way to do that: http://stackoverflow.com/questions/2686865/how-can-i-send-keypresses-to-a-running-process-object – Filippos Karapetis Jan 26 '14 at 17:55
0

Here's some boilerplate code for that (make sure there is txtInput and txtOutput on the form):

Private m_process As Process
Private m_encoding_for_program As System.Text.Encoding

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    m_encoding_for_program = System.Text.Encoding.GetEncoding(866) 'Adjust as needed

    Dim psi As New ProcessStartInfo
    psi.CreateNoWindow = True
    psi.UseShellExecute = False
    psi.FileName = "cmd.exe"
    psi.RedirectStandardInput = True
    psi.RedirectStandardError = True
    psi.StandardErrorEncoding = m_encoding_for_program
    psi.RedirectStandardOutput = True
    psi.StandardOutputEncoding = m_encoding_for_program

    m_process = Process.Start(psi)
    AddHandler m_process.OutputDataReceived, AddressOf OutputDataReceived
    AddHandler m_process.ErrorDataReceived, AddressOf OutputDataReceived 'Make different handler if needed.
    m_process.BeginOutputReadLine()
    m_process.BeginErrorReadLine()

End Sub

Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    m_process.CancelOutputRead()
    m_process.CancelErrorRead()
    m_process.Kill()
End Sub

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtInput.KeyUp
    If e.KeyCode = Keys.Enter Then
        Dim b = m_encoding_for_program.GetBytes(txtInput.Text)
        m_process.StandardInput.BaseStream.Write(b, 0, b.Length)
        m_process.StandardInput.WriteLine()
    End If
End Sub

Private Sub OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
    Me.Invoke(CType(AddressOf Me.ThreadProcSetter, Action(Of Object)), e.Data)
End Sub

Private Sub ThreadProcSetter(ByVal text As Object)
    txtOutput.AppendText(DirectCast(text, String) & ControlChars.NewLine)
End Sub

Enter commands in txtInput and press enter to see results. Make sure txtOutput is multiline.
Add error checking as needed.

GSerg
  • 76,472
  • 17
  • 159
  • 346