STDIN Command Prompt Closes
I'm trying to write a program to change my static IP. I am successful in opening a command prompt, sending keystrokes to it, and closing it again. That seems messy, so I want to use stdin. If I try using stdin, the command prompt just opens and closes really quickly... Help me!
The first block of commented out code is what fails.
The second block of commented out code is successful.
Code Code:
Sub IpSwitch(ByVal IP As String, ByVal SM As String, ByVal DG As String)
Dim CMD As New Process
With CMD.StartInfo
.FileName = "cmd.exe"
.UseShellExecute = False
.Verb = "runas"
.RedirectStandardInput = True 'Set to false if not using stdin, and using sending keystrokes instead
End With
CMD.Start()
'Dim stdin As IO.StreamWriter
'stdin = CMD.StandardInput
'stdin.WriteLine("netsh interface ip set address ""Wireless Network Connection"" static " + IP + " " + SM + " " + DG + " 1")
'stdin.WriteLine("exit")
'Dim SetPoint As Date
'SetPoint = Now.AddMilliseconds(500)
'While Date.Compare(Now, SetPoint) < 0
' Application.DoEvents()
'End While
'SendKeys.SendWait("netsh interface ip set address ""Wireless Network Connection"" static " + IP + " " + SM + " " + DG + " 1")
'SendKeys.SendWait(Chr(Keys.Enter))
'SendKeys.SendWait("exit")
'SendKeys.SendWait(Chr(Keys.Enter))
'SetPoint = Now.AddMilliseconds(500)
'While Date.Compare(Now, SetPoint) < 0
' Application.DoEvents()
'End While
'Me.Focus()
End Sub
Re: STDIN Command Prompt Closes
Cmd has .StandardInput streamwriter that is used to receive commands. You should write into that stream in order to 'type' something instead of sending keystrokes.
vb Code:
Public Class Form1
Dim cmd As System.Diagnostics.Process
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
cmd = New Process
With CMD.StartInfo
.FileName = "cmd.exe"
.UseShellExecute = False
.Verb = "runas"
.RedirectStandardInput = True 'Set to false if not using stdin, and using sending keystrokes instead
.RedirectStandardOutput = True
.CreateNoWindow = True
End With
cmd.EnableRaisingEvents = True
AddHandler cmd.Exited, AddressOf cmd_exited
cmd.Start()
cmd.StandardInput.WriteLine("exit") ' Send your command here (exit will exit the process).
End Sub
Private Sub cmd_exited(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("OK")
End Sub
End Class
Re: STDIN Command Prompt Closes
You didn't read my post. That code is nearly identical to the code in the first commented out block of code I posted. It does not work. The command prompt isn't exiting because you tell it to. It's exiting because... I don't exactly know why. But that unknown is my problem.
I have another solution, which is to run a temporary batch file invisibly with admin privileges. But I'd still like to know how to do it with stdin.
Re: STDIN Command Prompt Closes
Further research shows me that this may not be my fault. I think the stupid command prompt is just closing when it reaches the end of the stdin stream. Since you have to start it before you can retrieve the stdin stream and send to it, it's just opening and closing. Is there any way I can get these streams and THEN start the command prompt? It seems unlikely...
Re: STDIN Command Prompt Closes
These codes are not identical. Sending keys and writing to the input stream is not the same thing.
Before posting I checked my code and it worked fine. I didn't even look your code further than I saw SendKeys for the first time. SendKeys imitates keystroke and not writes data to the redirected input stream. That's the difference.
And no, you can't aquire the input stream before the object is created.
Re: STDIN Command Prompt Closes
I posted THIS after the send keys, as the broken alternative I need help with! So I was right, you didn't read my post. Of course your code works, you're trying to close the prompt which is exactly what my problem is!
'Dim stdin As IO.StreamWriter
'stdin = CMD.StandardInput
'stdin.WriteLine("netsh interface ip set address ""Wireless Network Connection"" static " + IP + " " + SM + " " + DG + " 1")
'stdin.WriteLine("exit")
Re: STDIN Command Prompt Closes
I re-read your initial post again. I still don't quite understand what exactly is your problem?
If you're implying that there must be a delay between the first command (netsh ...) and the second one (exit) you should have said that (e.g. How to make a delay?) A delay can be implemented like this:
Code:
System.Threading.Thread.Sleep(2000) ' For 2 seconds
If I understood you wrong then please, can you describe in plain short simple words, what's the problem?
Re: STDIN Command Prompt Closes
The command prompt exits without running the requested command (netsh)... I know that this command works... I am guessing that the command prompt is reaching the end of the stream, since I haven't had a chance to write to it, and then closing. How do I prevent this from happening?
Re: STDIN Command Prompt Closes
Grab the output stream and see the cmd response to your commands. You might want not to transmit 'exit' command while you're debugging.
Also, modify your code:
Code:
Dim command As String = "netsh interface ip set address ""Wireless Network Connection"" static " & IP & " " & SM + " " & DG & " 1")
MsgBox(command)
StdIn.WriteLine(command)
This achieves two things:
If you have Option Strict Off then "2" + "2" might result in "4" and not "22" as you might have expected, that's why I changed + to &
MsgBox will allow you to check if the command is correct before sending it to the input stream.