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.

Recommended Answers

All 7 Replies

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

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.

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.

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

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.