embedded DOS window
Hi,
I was wondering how can I embed a dos window in vb.net
What I am trying to do is to have a dos window embedded into a form so that I can have someone fill in a textbox and then I can take what is in the textbox and run it as if I had typed it in from the command line.
What I did was I ran it through the shell command and displayed its output on the screen. That works but there is one issue; It returns the whole output when the command has ended but what I need is it to display its output as the command is run, as it does when its ran in the dos prompt.
is this possible?
thanks
The following code runs a DOS window 'neatly' inside a VB form.
(A program (secondary to CMD), does not have to be named. In
other words, you can go straight to the 'C:\' prompt.
' Create a Windows application form of a LARGE size.
' Add a Panel from the toolbox (Panel1).
' Position the panel even with the top left of the form.
' Cut and paste this code into your form1.vb.
Imports
System.Runtime.InteropServicesPublic
Class Form1Private Declare Function SetParent Lib "user32" _(
ByVal hWndChild As System.IntPtr, ByVal hWndNewParent As System.IntPtr) As IntegerPrivate Declare Function MoveWindow Lib "user32" _(
ByVal hwnd As Integer, ByVal x As Integer, ByVal y As Integer, _ByVal nWidth As Integer, ByVal nHeight As Integer, _ByVal bRepaint As Integer) As IntegerPrivate Declare Function GetWindowRect Lib "user32" Alias _"GetWindowRect" (ByVal hwnd As System.IntPtr, ByRef lpRect As RECT) As Integer' Main Window Handle of started processPublic mwh As IntegerPublic DOS As Boolean = True ' Set to false to run a Windows programPrivate Structure RECTPublic Left As IntegerPublic Top As IntegerPublic Right As IntegerPublic Bottom As IntegerEnd StructurePrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e _As System.EventArgs) Handles MyBase.Load' Name the DOS program that you want to run here;' or change the public variable 'DOS' to false and name' a Windows program insteadDim MyProgName As String = "edit.com" ' This can be left as ""'' or try uncommenting the next two lines'MyProgName = "calc.exe" 'DOS = FalseDim p As New ProcessIf DOS Thenp.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
p.StartInfo.FileName =
"cmd.exe"' It is probably best to change the working directory' to the directory of any DOS program you may want to' start.p.StartInfo.WorkingDirectory = (
"c:\windows\system32")p.StartInfo.Arguments =
"/k " & MyProgNamep.Start()
'- Start the process' Use sleep instead of waitforinputidle for CMD.EXE' The following sleep time may have to be increased to get this
' to work correctly all the time. If set too low, the CMD/DOS program
' launches but does not get 'captured' into the VB program
System.Threading.Thread.Sleep(265)
Application.DoEvents()
Elsep.StartInfo.WindowStyle = ProcessWindowStyle.Normal
p.StartInfo.FileName = MyProgName
p.Start()
'- Start the processp.WaitForInputIdle()
' Wait for the message loop to be created End If'- Use the SetParentAPI to trap the processes window' inside of panel1SetParent(p.MainWindowHandle,
Me.Panel1.Handle)mwh = p.MainWindowHandle
Dim rectwin As New RECT' Find the size of our CMD box windowGetWindowRect(mwh, rectwin)
Dim wi As Integer = rectwin.Right - rectwin.LeftDim hi As Integer = rectwin.Bottom - rectwin.Top' The '-30' in the staements below takes the cmd window' top menu bar up to hide itMoveWindow(mwh, 0, -30, wi, hi, 1)
' Resize the containing panelPanel1.Width = wi
Panel1.Height = hi - 30
End SubEnd
ClassDepending upon what DOS commands you need to run you may also find redirecting standard input and output to a textbox useful.
The following shows the bare bones. It just needs a form with a textbox on it.
Public Class Form1
Dim P As New Process
Dim SW As System.IO.StreamWriter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler P.OutputDataReceived, AddressOf DisplayOutput
P.StartInfo.CreateNoWindow() = True
P.StartInfo.UseShellExecute = False
P.StartInfo.RedirectStandardInput = True
P.StartInfo.RedirectStandardOutput = True
P.StartInfo.FileName = "cmd.exe"
P.Start()
P.SynchronizingObject = Textbox1
P.BeginOutputReadLine()
SW = P.StandardInput
SW.WriteLine()
End Sub
Private Sub DisplayOutput(ByVal sendingProcess As Object, ByVal output As DataReceivedEventArgs)
Textbox1.AppendText(output.Data() & vbCrLf)
End Sub
Private Sub Textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress
Static Line As String
If e.KeyChar = Chr(Keys.Return) Then
SW.WriteLine(Line & vbCrLf)
Line = ""
Else
Line = Line & e.KeyChar
End If
End Sub
End Class