Results 1 to 40 of 61

Thread: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]

Hybrid View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]

    After typing a few commands, your "edited" example doesn't work anymore... it hangs up and no results are shown in the box...

  2. #2
    Junior Member w8taminute's Avatar
    Join Date
    Aug 2006
    Posts
    24

    Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]

    How would I get the command prompt to open in my parent form?

  3. #3
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]

    I changed this a bit, but the main thing wrong was the "results of the command window".


    Code:
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim CMDThread As New Threading.Thread(AddressOf CMDAutomateThread)
            CMDThread.Start()
        End Sub
        Private Sub CMDAutomateThread()
            TextBox1.Text = CMDAutomate("date", "12/31/2099") 'Set textbox to string return
        End Sub
    
        Private Function CMDAutomate(ByVal cmdString As String, ByVal cmdString2 As String) As String
            Dim myprocess As New Process
            Dim StartInfo As New System.Diagnostics.ProcessStartInfo
            Dim s As String = ""
            StartInfo.FileName = "cmd" 'starts cmd window        
            StartInfo.RedirectStandardInput = True
            StartInfo.RedirectStandardOutput = True
            StartInfo.UseShellExecute = False 'required to redirect    
            ' StartInfo.CreateNoWindow = True 'creates no cmd window         
            myprocess.StartInfo = StartInfo
            myprocess.Start()
            Dim SR As System.IO.StreamReader = myprocess.StandardOutput
            Dim SW As System.IO.StreamWriter = myprocess.StandardInput
            SW.WriteLine(cmdString) 'the commands you wish to run.....      
            SW.WriteLine(cmdString2)
            s = SR.ReadToEnd 'returns results of the command window  before exit
            SW.WriteLine("exit") 'exits command prompt window      
            SW.Close()
            SR.Close()
            Return s
        End Function
    Thanks for the example!

  4. #4
    Member
    Join Date
    Jul 2007
    Location
    Silly Clone Valley CA
    Posts
    47

    Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]

    Quote Originally Posted by TTn
    I changed this a bit, but the main thing wrong was the "results of the command window".


    Code:
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim CMDThread As New Threading.Thread(AddressOf CMDAutomateThread)
            CMDThread.Start()
        End Sub
        Private Sub CMDAutomateThread()
            TextBox1.Text = CMDAutomate("date", "12/31/2099") 'Set textbox to string return
        End Sub
    
        Private Function CMDAutomate(ByVal cmdString As String, ByVal cmdString2 As String) As String
            Dim myprocess As New Process
            Dim StartInfo As New System.Diagnostics.ProcessStartInfo
            Dim s As String = ""
            StartInfo.FileName = "cmd" 'starts cmd window        
            StartInfo.RedirectStandardInput = True
            StartInfo.RedirectStandardOutput = True
            StartInfo.UseShellExecute = False 'required to redirect    
            ' StartInfo.CreateNoWindow = True 'creates no cmd window         
            myprocess.StartInfo = StartInfo
            myprocess.Start()
            Dim SR As System.IO.StreamReader = myprocess.StandardOutput
            Dim SW As System.IO.StreamWriter = myprocess.StandardInput
            SW.WriteLine(cmdString) 'the commands you wish to run.....      
            SW.WriteLine(cmdString2)
            s = SR.ReadToEnd 'returns results of the command window  before exit
            SW.WriteLine("exit") 'exits command prompt window      
            SW.Close()
            SR.Close()
            Return s
        End Function
    Thanks for the example!
    DO NOT TRY THIS CODE! It will change your system date. Yeah, maybe I'm an idiot for not knowing that "date 12/31/2099" is the command to set your system to the year 2099, but still, just warning people out there.

  5. #5
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]

    SlumberMachine,

    Yeah maybe you are.
    Usually everyone would look at the code to see what it means, and so yeah it's a no-brainer. There are comments throughout it.
    I don't appreciate you posting things like:
    "DONT TRY TTN'S MALICIOUS CODE".

    By all means, everyone can try my/gigemboy's code, with the command you want, in the window.
    Duh.

  6. #6
    Member
    Join Date
    Jul 2007
    Location
    Silly Clone Valley CA
    Posts
    47

    Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]

    Quote Originally Posted by TTn
    SlumberMachine,

    Yeah maybe you are.
    Usually everyone would look at the code to see what it means, and so yeah it's a no-brainer. There are comments throughout it.
    I don't appreciate you posting things like:
    "DONT TRY TTN'S MALICIOUS CODE".

    By all means, everyone can try my/gigemboy's code, with the command you want, in the window.
    Duh.
    Look, I didn't mean to hurt your feelings, but what you posted was not only wrong, but malicious, and you know it. And I'm just trying to do what is right and warn people, like myself that are trying to learn. You didn't comment that line of the code. If you didn't intend for it to be malicious then why not do a "dir c:" instead? I've never heard or ever had to use a date command in dos and just figured it would return something interesting like the number of days until that date or something. Don't get defensive, just do what is right for others and admit the mistake and accept the punishment (which is someone else warning others not to use a bit of code).

    No need to take it all personal or anything, and if you want to think that I'm an ignorant idiot, that doesn't know crap about dos commands or vb that is fine with me. It sounds like you have an awesome wealth of knowledge in this area and I'm sure you are way better then me. but.. the fact is:

    you posted dangerous code, which did screw up someones system, and others should be warned to not make the same mistake when trying it out.

  7. #7
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]

    You could have, and should have simply stated that the command, will adjust your clock.
    If anyone intends to use the command window, then they'd be putting in their own command. Your point is completely mute, because everyone knows that.

    I did comment that piece in fact.
    "'the commands you wish to run..... "

    That's not malicious, because you could enter any command you like.
    I can't think of one thing that can be maliciously affected, by changing the time anyway. It didn't happen, you can just change the time back. Woopie. No systems were screwed up, as you implied, and lied about.

    I hate liars, that try to make a point by testifying false data.
    I really wish the moderators would ban counter-productive members like you.

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