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

[598 byte] By [KalhoNaaho] at [2007-12-28]
# 1

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.InteropServices

Public Class Form1

Private Declare Function SetParent Lib "user32" _

(ByVal hWndChild As System.IntPtr, ByVal hWndNewParent As System.IntPtr) As Integer

Private 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 Integer

Private Declare Function GetWindowRect Lib "user32" Alias _

"GetWindowRect" (ByVal hwnd As System.IntPtr, ByRef lpRect As RECT) As Integer

' Main Window Handle of started process

Public mwh As Integer

Public DOS As Boolean = True ' Set to false to run a Windows program

Private Structure RECT

Public Left As Integer

Public Top As Integer

Public Right As Integer

Public Bottom As Integer

End Structure

Private 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 instead

Dim MyProgName As String = "edit.com" ' This can be left as ""

'' or try uncommenting the next two lines

'MyProgName = "calc.exe"

'DOS = False

Dim p As New Process

If DOS Then

p.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 " & MyProgName

p.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()

Else

p.StartInfo.WindowStyle = ProcessWindowStyle.Normal

p.StartInfo.FileName = MyProgName

p.Start() '- Start the process

p.WaitForInputIdle() ' Wait for the message loop to be created

End If

'- Use the SetParentAPI to trap the processes window

' inside of panel1

SetParent(p.MainWindowHandle, Me.Panel1.Handle)

mwh = p.MainWindowHandle

Dim rectwin As New RECT

' Find the size of our CMD box window

GetWindowRect(mwh, rectwin)

Dim wi As Integer = rectwin.Right - rectwin.Left

Dim hi As Integer = rectwin.Bottom - rectwin.Top

' The '-30' in the staements below takes the cmd window

' top menu bar up to hide it

MoveWindow(mwh, 0, -30, wi, hi, 1)

' Resize the containing panel

Panel1.Width = wi

Panel1.Height = hi - 30

End Sub

End Class

TallDude at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Depending 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

Dave299 at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

both ways work, thanks a lot guys

really appriciate it !!!

thx

KalhoNaaho at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic General...