Name: go_places_puterwize Date: March 9, 2008 at 14:41:25 Pacific Subject: Console input/output OS: Windows Vista Premium CPU/Ram: AMD64 1.8Ghz Dual Core Model/Manufacturer: HP Compaq Presario F700
Comment:
Hi everyone, I'm trying to write text and read from a console window after its done writing, but I may have it in the wrong order.. what I want to do is send what the textbox has to the console window, then append the text back to the textbox after the console app is done. I don't know I may have it wrong but I'm using VB 2008, If possible I want to completely remove bringing up the window and just read and write to it from the textbox itself. here's what I have: Dim processID As Integer processID = Shell("cmd.exe", AppWinStyle.MinimizedFocus) Dim consoleApp As New Process
TextBox2.AppendText("ping " + ip_address.Text) If custom_size.Checked Then
TextBox2.AppendText(" -l " + buffer_size.Text + " ") End If If resolve_option.Checked Then
TextBox2.AppendText(" -a ") End If If continuous.Checked Then
TextBox2.AppendText(" -t ") End If
With consoleApp .Start("cmd.exe") .StartInfo.RedirectStandardOutput = True End With
I was hoping someone more knowledgeable than me would answer, but until that happens, here's a couple of points.
First, you don't need to create a cmd.exe process which in turn creates a ping.exe process. It's more efficient to call ping.exe directly, leaving out cmd.exe. Just supply everything on the one command line, then you don't need to send it any more commands.
Second, as ping.exe is a console application, you can't escape from the fact that it's going to create a console window. There may be a non-console equivalent of ping.exe available to download from somewhere, which will do what you want. Or you can learn some network programming and do what ping does in your own VB application.
Yeah, I suppose it can be done. I found an answer to your question on MSDN. The catch is that their answer is C code, not VB. I'd do the conversion myself, but I don't have VB.NET here, and my head hurts. A lot.
As for the console window, it shouldn't be too hard to hide it; Shell() will do it for you at process creation.
Alright, my head doesn't hurt anymore, so for the sake of completeness, here's a quick example of output redirection. It doesn't hide the console window, but I can't do all of your work for you. EDIT: Apperently, I was just missing a setting. It should hide the window now.
Public Class Form1 FriEnd WithEvents Txt As System.Windows.Forms.TextBox FriEnd WithEvents Btn As System.Windows.Forms.Button FriEnd WithEvents Proc As System.Diagnostics.Process 'Yes, we're playing with Threads Delegate Sub WriteOutput(ByVal text As String)
Public Sub New() 'You can just ignore everything in this sub 'Just basic, "TextBox here, Button there," stuff. Me.Txt = New System.Windows.Forms.TextBox Me.Btn = New System.Windows.Forms.Button InitializeComponent() Me.Size = New Size(500, 500) Me.Controls.Add(Btn) Me.Controls.Add(Txt) With Me.Txt .AcceptsReturn = True .AcceptsTab = True .Location = New System.Drawing.Point(9, 12) .Multiline = True .Name = "Txt" .Size = New System.Drawing.Size(471, 420) .TabIndex = 0 End With With Me.Btn .Location = New System.Drawing.Point(9, 438) .Name = "Btn" .Size = New System.Drawing.Size(75, 23) .TabIndex = 1 .Text = "Ping" .UseVisualStyleBackColor = True End With End Sub
Private Sub Btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn.Click If Not Proc Is Nothing Then _ Proc.Kill() 'Okay, let's do this: 'Step 1: Create the process Proc = New Process With Proc.StartInfo .FileName = "ping" .Arguments = "www.google.com" 'Step 2: Prep the process for output redirection .UseShellExecute = False .RedirectStandardOutput = True .CreateNoWindow = True End With 'Step 2.5: Because I'd like to know when the child process finishes Proc.EnableRaisingEvents = True 'Step 3: Start the process Proc.Start() Txt.Text = "[SoP]" + vbNewLine 'Step 4: Redirect the output Proc.BeginOutputReadLine() End Sub
Private Sub HandleOutput(ByVal sendingProcess As Object, _ ByVal outLine As DataReceivedEventArgs) Handles Proc.OutputDataReceived If Not [String].IsNullOrEmpty(outLine.Data) Then _ ToTxt(outLine.Data + vbNewLine) End Sub
Private Sub Proc_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles Proc.Exited 'A delay to allow all of the output to flush... System.Threading.Thread.Sleep(1000) ToTxt("[EoP]") End Sub
Private Sub ToTxt(ByVal text As String) If Txt.InvokeRequired Then 'If we're here, we're on another thread. We could probably get away 'without this in VS2K3, but whatever. 'FYI: WriteOutput() was declared as a Delegate Sub towards the top 'ToTxt is obviously this Sub 'And finally, we send an array of Objects... Me.Invoke(New WriteOutput(AddressOf ToTxt), New Object() {text}) Else Txt.Text = Txt.Text + text End If End Sub End Class
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE