Send keys to CMD vb.net?

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
Hello everyone, I am writing a program and need some help with what should be the easiest part......

Basically, my program opens up CMD as an administrator, but after it does that I want it to run a command, I tried using sendkeys but that didnt work at all. the CMD window does appear to be the active window (it is in front of all the other windows, and if I type on the keyboard the text is displayed there) but the sendkeys doesnt actually send any keys. Can anyone give me some help please?

TL:DR
Want CMD to open as administrator then run command. Open as admin works, but I cant get it to run a command after it opens.

Code:
Dim process As New Process()
        process.StartInfo.FileName = "cmd.exe" 
        process.StartInfo.Verb = "runas"
        process.StartInfo.UseShellExecute = True
        process.Start()
        System.Threading.Thread.Sleep(3000) 
        SendKeys.Send("ipconfig /all")
        SendKeys.Send("{ENTER}")
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Why do you have to inject the command string at the prompt? Can't you just pass it to cmd as an argument at start?
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
You would use a variation of the below. In my sample I'm getting any messages coming from the process and converting that to a string. You would want to reverse some of the logic to post a string to the standard input. So while this isn't the answer, it should lead you down the right path.

Code:
                    p = New Diagnostics.ProcessStartInfo
                    p.WindowStyle = ProcessWindowStyle.Hidden
                    p.Arguments = strl_CMD
                    p.FileName = "sqlcmd"

                    p.RedirectStandardOutput = True
                    p.RedirectStandardError = True
                    p.RedirectStandardInput = True
                    p.UseShellExecute = False
                    p.CreateNoWindow = True

                    newProc = Diagnostics.Process.Start(p)
                    Dim objl_STREAMREADER As System.IO.StreamReader = newProc.StandardOutput
                    newProc.WaitForExit()

                    strl_LINE = objl_STREAMREADER.ReadToEnd.Trim()
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
You would use a variation of the below. In my sample I'm getting any messages coming from the process and converting that to a string. You would want to reverse some of the logic to post a string to the standard input. So while this isn't the answer, it should lead you down the right path.

Code:
                    p = New Diagnostics.ProcessStartInfo
                    p.WindowStyle = ProcessWindowStyle.Hidden
                    p.Arguments = strl_CMD
                    p.FileName = "sqlcmd"

                    p.RedirectStandardOutput = True
                    p.RedirectStandardError = True
                    p.RedirectStandardInput = True
                    p.UseShellExecute = False
                    p.CreateNoWindow = True

                    newProc = Diagnostics.Process.Start(p)
                    Dim objl_STREAMREADER As System.IO.StreamReader = newProc.StandardOutput
                    newProc.WaitForExit()

                    strl_LINE = objl_STREAMREADER.ReadToEnd.Trim()

Awesome, much appreciated. I will give this a crack and see if I can work it into my script.

Thank you!
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
Are you trying to get IP Address info? A better idea might be to use WMI to get this information, that way you don't have to start an external process and parse its output. you get just the data you want.

http://msdn.microsoft.com/en-us/library/aa394217(v=vs.85).aspx

Thanks, but I was just using ipconfig as a test, I have a specific command that I want to run.

So the above code didnt work unfortunately, and I have modified my own code to try and force everything to work, but for some reason it wont send any keys to CMD. If I do:
Code:
shell("cmd.exe")
sendkeys.send("YOYOYO")
It gets sent to CMD just fine, but if I use the real process.start method, nothing happens. New code:
Code:
        Dim process As New Process()
        process.StartInfo.FileName = "cmd.exe "
        process.StartInfo.Verb = "runas"
        process.StartInfo.UseShellExecute = True
        process.Start()
        System.Threading.Thread.Sleep(2000)
        If process.Handle = GetForegroundWindow() Then
            SendKeys.Send("HELLO")
        End If
Does anyone have any ideas why shell works, but process.start wont let me send keys?

Thank you