1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

DOS Commands In Visual Basic

Discussion in 'Programming & Webmastering' started by PVTCaboose1337, Jan 21, 2010.

  1. PVTCaboose1337

    PVTCaboose1337 Graphical Hacker

    Joined:
    Feb 1, 2006
    Messages:
    9,512 (2.99/day)
    Thanks Received:
    1,143
    Location:
    San Antonio, Texas
    How would I go about executing DOS commands in Visual Basic? My plan is to have an input field in VB, and then when the user pushes a button it will input this input into the DOS command and execute it.
     
  2. PVTCaboose1337

    PVTCaboose1337 Graphical Hacker

    Joined:
    Feb 1, 2006
    Messages:
    9,512 (2.99/day)
    Thanks Received:
    1,143
    Location:
    San Antonio, Texas
    Ok, update:

    I have found that

    Code:
      Dim sCommand As String
            sCommand = "DOSCOMMAND HERE"
            Shell("cmd.exe /c" & sCommand)
    will execute a dos command, but lets say I want to echo from a text box inside VB? How would I go about that?

    Code:
      Dim sCommand As String
            sCommand = "echo SOMETHINGFROMVB"
            Shell("cmd.exe /c" & sCommand)
     
  3. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.93/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    I think that sCommand could be a filename with all the commands you want to execute in it.
    Our do you want to be able to talk to the cmd process from individual lines of VB code?
     
  4. PVTCaboose1337

    PVTCaboose1337 Graphical Hacker

    Joined:
    Feb 1, 2006
    Messages:
    9,512 (2.99/day)
    Thanks Received:
    1,143
    Location:
    San Antonio, Texas
    I figured it out just now. Was very simple. Stuff box is a textbox btw.

    Code:
    Dim stuff As String
    
            stuff = stuffbox.Text
    
            Dim sCommand As String
            sCommand = "echo" & " " & stuff
            Shell("cmd.exe /c" & sCommand)
     
    Kreij says thanks.
  5. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.93/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    Thanks for letting us know how you got it working ! :toast:
    I use this section as a programming reference quite often.
     
  6. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,781 (6.26/day)
    Thanks Received:
    3,668
    Location:
    IA, USA
    You should get in the habit of using System.Diagnostics.Process.Start() rather than Shell. Using that method, you can redirect StandardOutput, and StandardError. In your app, create a while loop to write the contents of each of those streams to a window in the application:

    Code:
    While p.StandardOutput.Peek() <> -1
      txt.Text +=p.StandardOutput.ReadLine()
    End While
    Repeat for StandardError.
     
    Crunching for Team TPU
  7. PVTCaboose1337

    PVTCaboose1337 Graphical Hacker

    Joined:
    Feb 1, 2006
    Messages:
    9,512 (2.99/day)
    Thanks Received:
    1,143
    Location:
    San Antonio, Texas
    Ok guys, here is where the work went to. A fun program to screw around with on the school network. Nobody knew what I meant when I said "use the command prompt to send messages," so I made my own program so people can talk over the network. Will improve later, want to add some more options, but for a quick time, this is good enough!

    I took advantage of this original batch file written in about 30 seconds:

    Code:
    @echo off
    color 1f
    :begin
    cls
    set /p n=User: 
    set /p m=Message: 
    msg %n% %m%
    pause
    Goto begin
    
    It only worked with Windows 7 / Vista, so in the VB code I added a condition to change between different versions of Windows. Anyway...

    Enjoy!

    Edit: For all those against opening strange .exe files they find on tech forums, this is for you:

    [​IMG]

    http://www.virustotal.com/analisis/...ace159ac1db582d43e66605655427286e1-1264127325
     

    Attached Files:

    Last edited: Jan 22, 2010
  8. A Cheese Danish

    A Cheese Danish

    Joined:
    Nov 18, 2006
    Messages:
    2,963 (1.02/day)
    Thanks Received:
    413
    Location:
    your local vending machine
    That's a fun little program you got there.
    This reminds me, I need to keep teaching myself how to code more in depth with VB.
     
    PVTCaboose1337 says thanks.
  9. PVTCaboose1337

    PVTCaboose1337 Graphical Hacker

    Joined:
    Feb 1, 2006
    Messages:
    9,512 (2.99/day)
    Thanks Received:
    1,143
    Location:
    San Antonio, Texas
    It has been about 4 year since I touched VB! I forgot how much fun it was to write little programs like that!

    ALSO: For those who care, the full code:

    Code:
    Public Class Form1
    
        Private Sub send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles send.Click
            'Define and store user and message into strings
            Dim user As String
            Dim msg As String
    
            user = userbox.Text
            msg = msgbox.Text
    
            'Send all suicide mode!
            If checkall.Checked Then
                user = "*"
                userbox.ReadOnly = True
    
            End If
    
            'Main Code
    
            If radionew.Checked Then
    
                'Windows Vista / 7 Code
                'Calls the user and msg strings and inputs them into the DOS code to execute
    
                Dim sCommand As String
                sCommand = "msg" & " " & user & " " & msg
                Shell("cmd.exe /c" & sCommand)
    
    
            ElseIf radioold.Checked Then
    
                'Windows 2000 / XP Code
                'Calls the user and msg strings and inputs them into the DOS code to execute
    
                Dim sCommand As String
                sCommand = "net send" & " " & user & " " & msg
                Shell("cmd.exe /c" & sCommand)
    
            End If
    
        End Sub
    
        Private Sub clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clear.Click
            'Clears the message box
            msgbox.Text = ""
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    End Class
    
    Simple no?

    If anyone is using Windows XP or 2000, please test the code for me! I have only 7 and Vista! Just make sure to check the correct box!

    Download is here: http://forums.techpowerup.com/attachment.php?attachmentid=32339&d=1264124659
     
    Last edited: Jan 22, 2010
  10. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,781 (6.26/day)
    Thanks Received:
    3,668
    Location:
    IA, USA
    You can use Environment.OSVersion to figure out what version of Windows the application is being run on is.

    Code:
    If Environment.OSVersion.MajorVersion >= 6
     ' Vista/7/Server 2008
    Else
     ' XP/Server 2003/2000/NT

    It does not work on Server 2003 most likely because "net send" is a Windows 2000 and older command. "msg" works fine:

    Code:
    msg Administrator test
    I'll check XP but I'm pretty sure it will be the same result.


    Edit: XP is the same as Server 2003 (use "msg").


    If you change it over to use Process.Start, the command window, and most likely response, will appear in your application (or not at all) rather than separate windows.
     
    Last edited: Jan 22, 2010
    PVTCaboose1337 says thanks.
    Crunching for Team TPU
  11. PVTCaboose1337

    PVTCaboose1337 Graphical Hacker

    Joined:
    Feb 1, 2006
    Messages:
    9,512 (2.99/day)
    Thanks Received:
    1,143
    Location:
    San Antonio, Texas
    So I need to change it so XP uses the msg command as well? Last time I checked, the net send command also worked on XP.

    Also, the auto environment detector, GENIUS! I will use that!

    EDIT: Hmm... trying to figure out how to "capture" the return text, is it possible to grab it and have that other window never open?
     
    Last edited: Jan 22, 2010
  12. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,781 (6.26/day)
    Thanks Received:
    3,668
    Location:
    IA, USA
    "net send Administrator test" does not work on Server 2003. It says "the message alias could not be found on the network."


    Come to think of it, the response Window will pop up even if you redirect output because the application recieveing the message most likely isn't the computer running the app. You can get rid of console windows though:

    Code:
    Imports System.Diagnostics
    
    ...
    
    Dim psi As New ProcessStartInfo()
    With psi
      .Arguments = user & " " & msg
      .CreateNoWindow = True
      .FileName = Environment.GetEnvironmentVariable("SYSTEMROOT") & "\system32\msg.exe"
      .UseShellExecute = False
      .ErrorDialog = False
      .RedirectStandardOutput = True
      .RedirectStandardError = True
      .WorkingDirectory = Environment.GetEnvironmentVariable("SYSTEMROOT") & "\system32"
    End With
    
    Dim p As New Process()
    With p
      .StartInfo = psi;
      .Start();
      .WaitForExit();
    End With
    After that last End With is where you would put the While loop to handle the streams. I really see no need to in this case, however.


    I don't have Visual Studio installed at this time so the code above is untested (converted to VB from C#).
     
    PVTCaboose1337 says thanks.
    Crunching for Team TPU
  13. PVTCaboose1337

    PVTCaboose1337 Graphical Hacker

    Joined:
    Feb 1, 2006
    Messages:
    9,512 (2.99/day)
    Thanks Received:
    1,143
    Location:
    San Antonio, Texas
    New version... Really minor changes, removed an unneeded line of code, updated the icon, picture, etc. Added 90% transparency, and most of all, fixed the XP error. Next up is to make a "test" button that sends the local user a test message to make sure everything is correctly configured.

    Edit: Please give suggestions for features if you think this small app needs improvement.
     

    Attached Files:

  14. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,781 (6.26/day)
    Thanks Received:
    3,668
    Location:
    IA, USA
    By the way, all the version numbers are on here:
    http://en.wikipedia.org/wiki/History_of_Microsoft_Windows#Product_progression

    I don't know if Windows 2000 has msg.exe or not. Doing some Googling, it appears that Windows 2000 has msg.exe but some distros of Windows Vista does not (Home Basic and Home Premium).

    In any case, you should add a check for the file before attempting to execute it:
    Code:
    If System.IO.File.Exists(Environment.GetEnvironmenVariable("SYSTEMROOT") & "\system32\msg.exe")) Then
      ' run it
    Else
      MessageBox.Show("msg.exe" could not be found") ' or attempt net send but I still don't see how to run it
    End If

    Edit:
    Another suggestion is to catch the KeyPress event for Message and invoke the Send button. Something like:
    Code:
    Private Sub txtMessage_KeyPress(sender As Object, e As KeyPressEventArgs)
      If e.KeyChar = Convert.ToChar(13) Then
        send_Click(Me, New EventArgs())
        e.Handled = True
      End If
    End Sub
     
    Last edited: Jan 22, 2010
    Crunching for Team TPU
  15. PVTCaboose1337

    PVTCaboose1337 Graphical Hacker

    Joined:
    Feb 1, 2006
    Messages:
    9,512 (2.99/day)
    Thanks Received:
    1,143
    Location:
    San Antonio, Texas
    I am having issues with

    Code:
    Environment.GetEnvironmentalVariable
    Seems not to work...
     
  16. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,781 (6.26/day)
    Thanks Received:
    3,668
    Location:
    IA, USA
    Environment.GetEnvironmentVariable()


    It works on Server 2003 but I had Windows 7/Vista checked.
     
    Crunching for Team TPU
  17. PVTCaboose1337

    PVTCaboose1337 Graphical Hacker

    Joined:
    Feb 1, 2006
    Messages:
    9,512 (2.99/day)
    Thanks Received:
    1,143
    Location:
    San Antonio, Texas
    [​IMG]
     
  18. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,781 (6.26/day)
    Thanks Received:
    3,668
    Location:
    IA, USA
    It is Environment, not Environmental (my brain fart, I got right a post or two before that :laugh:). I fixed the code you quoted of mine.
     
    PVTCaboose1337 says thanks.
    Crunching for Team TPU
  19. PVTCaboose1337

    PVTCaboose1337 Graphical Hacker

    Joined:
    Feb 1, 2006
    Messages:
    9,512 (2.99/day)
    Thanks Received:
    1,143
    Location:
    San Antonio, Texas
    Great! You forgot the "then" as well... Works now, also, I need someone to test it with the new code in it who DOES NOT HAVE MSG.exe!!! Also, for the test button... I was planning on having it send a message back to the user... I tried saying the user was "127.0.0.1" but no luck! Any ideas?

    Edit: Ok we kinda are dumb ... what happens if the user is using Windows 2000 which does not have MSG.exe, but still uses the net send command? I will fix... those radio buttons are lifesavers!

    Edit2: Problem solved:

    [​IMG]
     

    Attached Files:

    Last edited: Jan 22, 2010
  20. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,781 (6.26/day)
    Thanks Received:
    3,668
    Location:
    IA, USA
    You shouldn't need the radio buttons...
    Code:
    If Environment.OSVersion.Platform <> PlatformID.Win32NT Then
      ' Windows 9x or other, not suported
    Else
      If System.IO.File.Exists(Environment.GetEnvironmentalVariable("SYSTEMROOT") & "\system32\msg.exe") Then
        ' Use msg
      Else
        If System.IO.File.Exists(Environment.GetEnvironmentalVariable("SYSTEMROOT") & "\system32\net.exe") And Environment.OSVersion.MajorVersion < 6 Then
          ' Use net send
        Else
          ' Not supported
        End If
      End If
    End If
    You should really make the path to msg and the path to net readonly variables:
    Code:
    Private ReadOnly PATH_MSG As String = Environment.GetEnvironmentalVariable("SYSTEMROOT") & "\system32\msg.exe") 
    Private ReadOnly PATH_NET As String = Environment.GetEnvironmentalVariable("SYSTEMROOT") & "\system32\net.exe")
    Instead of typing that whole lot, you'd only have to type PATH_MSG or PATH_NET.


    Edit: I updated both code areas in this post...
     
    Last edited: Jan 22, 2010
    Crunching for Team TPU
  21. PVTCaboose1337

    PVTCaboose1337 Graphical Hacker

    Joined:
    Feb 1, 2006
    Messages:
    9,512 (2.99/day)
    Thanks Received:
    1,143
    Location:
    San Antonio, Texas
    Ok great! I fixed most of the code to automate choosing the Windows version.
     

    Attached Files:

  22. PVTCaboose1337

    PVTCaboose1337 Graphical Hacker

    Joined:
    Feb 1, 2006
    Messages:
    9,512 (2.99/day)
    Thanks Received:
    1,143
    Location:
    San Antonio, Texas
    Having trouble calling a media file from resources. How would I accomplish this?

    Code:
    AxWindowsMediaPlayer1.URL = "My.Resources.music.mid"
    The above code does not work.
     
  23. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,781 (6.26/day)
    Thanks Received:
    3,668
    Location:
    IA, USA
    Crunching for Team TPU
  24. PVTCaboose1337

    PVTCaboose1337 Graphical Hacker

    Joined:
    Feb 1, 2006
    Messages:
    9,512 (2.99/day)
    Thanks Received:
    1,143
    Location:
    San Antonio, Texas
    I actually got it to play, but the music / midi file has to be in the same folder. Simply need to know how to call from resources.

    Code:
    AxWindowsMediaPlayer1.URL = "music.mid"
     
  25. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,781 (6.26/day)
    Thanks Received:
    3,668
    Location:
    IA, USA
    Oh, embeded in your executable? Usually it is:

    AssemblyName.Properties.Reserouces.name_of_resource
     
    Crunching for Team TPU

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page