954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

CMD in VB

So, I am running into an issue. I am wanting to display the contents of the open CMD window in my Rich Text Box. Any help? Thanks.

NetJunkie
Junior Poster
158 posts since Aug 2011
Reputation Points: 41
Solved Threads: 28
 

I hope you get things started with this little snippet

Dim cmdProcess As Process

cmdProcess = New Process()
' Dump all info from ipconfig command
cmdProcess.StartInfo.Arguments = "/ALL"
' Run command: ipconfig
cmdProcess.StartInfo.FileName = "ipconfig"
' Redirect stdout
cmdProcess.StartInfo.RedirectStandardOutput = True
' Set to false, otherwise you can't redirect stdout
cmdProcess.StartInfo.UseShellExecute = False
' Start process
If cmdProcess.Start() Then
    ' Read stdout and show the content in a rtf-box
    RichTextBox1.Text = cmdProcess.StandardOutput.ReadToEnd
Else
    ' Failed to execute command
End If

HTH

Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 

I am not sure if I was clear enough. I am wanting to display the contents of an already open CMD window. I was thinking I could read the header of the CMD window and display the contents of it but I am not sure how to go about this.

NetJunkie
Junior Poster
158 posts since Aug 2011
Reputation Points: 41
Solved Threads: 28
 
I am wanting to display the contents of an already open CMD window


Ok, so that _was_ critical :) My example shows a neat & clean way to redirect stdout if and only if you can open the command prompt yourself.was thinking I could read the header of the CMD window and display the contents of it
Read the header means finding the right window, am I right?

You can find an open command prompt by looping all open windows and searching the one that contains "Command Prompt". You can even do "Print Screen" to that window. There's code in this forum for these or you'll find them with some googling.

But obtaining the content (char buffer for that cmd) is a bit different thing. You should hook the stdout or grab cmd's buffer. You could try to find some (low level) Windows API for that but I don't know any direct solution.

Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 

Could you possibly give me an example snippet to see how to do that?

NetJunkie
Junior Poster
158 posts since Aug 2011
Reputation Points: 41
Solved Threads: 28
 

I have been Googling and all I can find is reading the contents of a CMD window that the button creates not reading a CMD window that is already opened.

NetJunkie
Junior Poster
158 posts since Aug 2011
Reputation Points: 41
Solved Threads: 28
 

Here's how you find the running process

' An array for all the processes running
Dim Running() As System.Diagnostics.Process
Dim searhProcessName As String = "CMD" ' Use uppercase, comparison is case-insensitive
Dim i As Integer

Try
    ' Get process list
    Running = System.Diagnostics.Process.GetProcesses()
    ' You could also use a faster method: Running = System.Diagnostics.Process.GetProcessesByName("Cmd")
    ' if you know the exact name of the process
    For i = 0 To Running.GetUpperBound(0)
        ' ToUpper.Contains() makes search case-insesitive and finds process also with partial name
        If Running(i).ProcessName.ToUpper.Contains("CMD") Then
            MessageBox.Show(Running(i).ProcessName)
            ' save this information and jump out
            'Exit For
        End If
    Next i
Catch ex As Exception
    ' Handle error(s)
End Try

If you can do with a screen shot, see this Screen Shot Application in CodeProject.com
I haven't used that code myself but you should be able to strip necessary parts of the source code.

Now you would have code for 1) finding the command prompt and 2) getting the content as an image. Do you absolutely need the content as a string? And if so, what's the reason you can't start the command prompt yourself?

HTH

Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 

Just in case you need the content as a string I'll give something to search for. At this point you do have access to process (cmd.exe) and you can get the handle of the process. What you can't do directly is to read stdout. StandardOutput is "hooked" in processes StartInfo and you can't change that either. The only solution would be to find a proper Windows API call which can hook stdout to your application while the other process (cmd.exe) is running. I don't know if there's such API call, but on the other hand, I haven't looked for one :)

HTH

Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: