Results 1 to 9 of 9

Thread: STDIN Command Prompt Closes

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    29

    Question 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:
    1. Sub IpSwitch(ByVal IP As String, ByVal SM As String, ByVal DG As String)
    2.         Dim CMD As New Process
    3.         With CMD.StartInfo
    4.             .FileName = "cmd.exe"
    5.             .UseShellExecute = False
    6.             .Verb = "runas"
    7.             .RedirectStandardInput = True 'Set to false if not using stdin, and using sending keystrokes instead
    8.         End With
    9.         CMD.Start()
    10.  
    11.         'Dim stdin As IO.StreamWriter
    12.         'stdin = CMD.StandardInput
    13.         'stdin.WriteLine("netsh interface ip set address ""Wireless Network Connection"" static " + IP + " " + SM + " " + DG + " 1")
    14.         'stdin.WriteLine("exit")
    15.  
    16.         'Dim SetPoint As Date
    17.         'SetPoint = Now.AddMilliseconds(500)
    18.         'While Date.Compare(Now, SetPoint) < 0
    19.         '    Application.DoEvents()
    20.         'End While
    21.         'SendKeys.SendWait("netsh interface ip set address ""Wireless Network Connection"" static " + IP + " " + SM + " " + DG + " 1")
    22.         'SendKeys.SendWait(Chr(Keys.Enter))
    23.         'SendKeys.SendWait("exit")
    24.         'SendKeys.SendWait(Chr(Keys.Enter))
    25.         'SetPoint = Now.AddMilliseconds(500)
    26.         'While Date.Compare(Now, SetPoint) < 0
    27.         '    Application.DoEvents()
    28.         'End While
    29.         'Me.Focus()
    30.     End Sub

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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:
    1. Public Class Form1
    2.     Dim cmd As System.Diagnostics.Process
    3.  
    4.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    5.         cmd = New Process
    6.         With CMD.StartInfo
    7.             .FileName = "cmd.exe"
    8.             .UseShellExecute = False
    9.             .Verb = "runas"
    10.             .RedirectStandardInput = True 'Set to false if not using stdin, and using sending keystrokes instead
    11.             .RedirectStandardOutput = True
    12.             .CreateNoWindow = True
    13.  
    14.         End With
    15.         cmd.EnableRaisingEvents = True
    16.         AddHandler cmd.Exited, AddressOf cmd_exited
    17.  
    18.         cmd.Start()
    19.  
    20.         cmd.StandardInput.WriteLine("exit") ' Send your command here (exit will exit the process).
    21.     End Sub
    22.  
    23.     Private Sub cmd_exited(ByVal sender As Object, ByVal e As EventArgs)
    24.         MsgBox("OK")
    25.     End Sub
    26.  
    27. End Class

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    29

    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.
    Last edited by Micro Farad; Nov 25th, 2010 at 06:59 PM.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    29

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

  5. #5
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    29

    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")

  7. #7
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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?

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    29

    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?

  9. #9
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width