[2005] CMD Console Emulator: Almost there
Hi,
With much help from the forum I am almost there with my project to emulate a cmd console using a form with a textbox (txtResults) to display the output of the (hidden) console, and another textbox (txtCommand) for me to type in commands, and send to the console with a click of a button (Button1).
The following code now almost emulates the cmd console exactly, but just one little bug still to iron out:
Look at the following pairs of screenshots from a cmd console and my emulator, and you will see the difference.
1. cmd console at start
2. emulator at start
3. cmd console after the CHCP (display code page) command
4. emulator after sending the CHCP command
As you can see, the emulator does not display the MS header until after sending a command.
a) How can I modify the code so my emulator textbox shows precisely what I see in the cmd console?
b) Is my Do While loop OK (I only want my emulator to stop when I close my application, and to be running otherwise)?
Many thanks for all the help so far, I have learnt a lot along the way
Rock
Code:
1 Public Class Form1
2
3 Private Results As String
4 Private Delegate Sub delUpdate()
5 Private Finished As New delUpdate(AddressOf UpdateText)
6
7 Private Sub UpdateText()
8 txtResults.AppendText(Results & vbNewLine)
9
10 If txtResults.Lines.Length >= 300 Then 'This sets a buffer for the textbox of 200-300 lines
11 Dim s(99) As String
12 Array.Copy(txtResults.Lines, txtResults.Lines.Length - 99, s, 0, 99)
13 txtResults.Lines = s
14 End If
15
16 txtResults.SelectionStart = txtResults.Text.Length ' this line and the next
17 txtResults.ScrollToCaret() 'sets the scrollbar to the bottom
18
19 End Sub
20 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
21 Dim CMDThread As New Threading.Thread(AddressOf User)
22 CMDThread.Start()
23 End Sub
24 Private Sub User()
25 Dim UserServer As New Process
26 Dim p As New System.Diagnostics.ProcessStartInfo
27 'p.FileName = "C:\Documents and Settings\Colin\Desktop\bin 7302\OpenSim.Grid.UserServer.exe" 'starts cmd window
28 'p.WorkingDirectory = "C:\Documents and Settings\Colin\Desktop\bin 7302"
29 p.FileName = "cmd" 'starts the cmd console
30 p.WorkingDirectory = "c:\"
31 p.RedirectStandardInput = True
32 p.RedirectStandardOutput = True
33 p.UseShellExecute = False 'required to redirect
34 p.CreateNoWindow = True 'creates no cmd window
35 UserServer.StartInfo = p
36 UserServer.Start()
37
38 Dim SR As System.IO.StreamReader = UserServer.StandardOutput
39 Dim SW As System.IO.StreamWriter = UserServer.StandardInput
40 SW.WriteLine(txtCommand.Text) 'the command you wish to run.....
41
42 Do While UserServer.HasExited = False 'Careful, could this cause an infinite loop
43 Results = UserServer.StandardOutput.ReadLine
44 Invoke(Finished) 'invokes Finished delegate, which updates textbox with the results
45 Loop
46
47 SW.Close()
48 SR.Close()
49
50 End Sub
51
52 End Class
53
Re: [2005] CMD Console Emulator: Almost there
Bumped, as I am getting a little bit desperate for a solution to this one.