![]() |
|
|
#1 |
|
Graphical Hacker
Join Date: Feb 2006
Location: San Antonio, Texas
Posts: 7,478 (2.81/day)
Thanks: 798
Thanked 1,174 Times in 834 Posts
|
DOS Commands In Visual Basic
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.
__________________
CPU-Z validation sig pics temporarily blocked |
|
|
|
|
|
#2 |
|
Graphical Hacker
Join Date: Feb 2006
Location: San Antonio, Texas
Posts: 7,478 (2.81/day)
Thanks: 798
Thanked 1,174 Times in 834 Posts
|
Ok, update:
I have found that Code:
Dim sCommand As String
sCommand = "DOSCOMMAND HERE"
Shell("cmd.exe /c" & sCommand)
Code:
Dim sCommand As String
sCommand = "echo SOMETHINGFROMVB"
Shell("cmd.exe /c" & sCommand)
__________________
CPU-Z validation sig pics temporarily blocked |
|
|
|
|
|
#3 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,108 (5.28/day)
Thanks: 591
Thanked 5,488 Times in 2,932 Posts
|
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?
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
|
#4 |
|
Graphical Hacker
Join Date: Feb 2006
Location: San Antonio, Texas
Posts: 7,478 (2.81/day)
Thanks: 798
Thanked 1,174 Times in 834 Posts
|
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)
__________________
CPU-Z validation sig pics temporarily blocked |
|
|
|
| The Following User Says Thank You to PVTCaboose1337 For This Useful Post: |
|
|
#5 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,108 (5.28/day)
Thanks: 591
Thanked 5,488 Times in 2,932 Posts
|
Thanks for letting us know how you got it working !
![]() I use this section as a programming reference quite often.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
|
#6 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,564 (6.29/day)
Thanks: 1,751
Thanked 2,594 Times in 1,959 Posts
|
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
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
|
#7 |
|
Graphical Hacker
Join Date: Feb 2006
Location: San Antonio, Texas
Posts: 7,478 (2.81/day)
Thanks: 798
Thanked 1,174 Times in 834 Posts
|
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 Enjoy! Edit: For all those against opening strange .exe files they find on tech forums, this is for you: ![]() http://www.virustotal.com/analisis/1...6e1-1264127325
__________________
CPU-Z validation sig pics temporarily blocked Last edited by PVTCaboose1337; Jan 22, 2010 at 01:30 AM. |
|
|
|
|
|
#8 |
![]() Join Date: Nov 2006
Location: At your local vending machine
Posts: 1,914 (0.81/day)
Thanks: 121
Thanked 419 Times in 368 Posts
|
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.
__________________
My FS/FT Thread
HEATWARE Xfire Steam ID Certs: MCTS, MCITP, HP APS Server: AMD Opteron 170 @2.95GHz | 4GB DDR 3-3-2-7 | 1x Samsung SPT 500GB, 2x WD Black 2TB, 2x 1.5TB Western Digital MyBook |
|
|
|
| The Following User Says Thank You to A Cheese Danish For This Useful Post: |
|
|
#9 |
|
Graphical Hacker
Join Date: Feb 2006
Location: San Antonio, Texas
Posts: 7,478 (2.81/day)
Thanks: 798
Thanked 1,174 Times in 834 Posts
|
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
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/attach...9&d=1264124659
__________________
CPU-Z validation sig pics temporarily blocked Last edited by PVTCaboose1337; Jan 22, 2010 at 01:37 AM. |
|
|
|
|
|
#10 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,564 (6.29/day)
Thanks: 1,751
Thanked 2,594 Times in 1,959 Posts
|
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 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.
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } Last edited by FordGT90Concept; Jan 22, 2010 at 02:10 AM. |
|
|
|
| The Following User Says Thank You to FordGT90Concept For This Useful Post: |
|
|
#11 | |
|
Graphical Hacker
Join Date: Feb 2006
Location: San Antonio, Texas
Posts: 7,478 (2.81/day)
Thanks: 798
Thanked 1,174 Times in 834 Posts
|
Quote:
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?
__________________
CPU-Z validation sig pics temporarily blocked Last edited by PVTCaboose1337; Jan 22, 2010 at 02:30 AM. |
|
|
|
|
|
|
#12 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,564 (6.29/day)
Thanks: 1,751
Thanked 2,594 Times in 1,959 Posts
|
"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
I don't have Visual Studio installed at this time so the code above is untested (converted to VB from C#).
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
| The Following User Says Thank You to FordGT90Concept For This Useful Post: |
|
|
#13 |
|
Graphical Hacker
Join Date: Feb 2006
Location: San Antonio, Texas
Posts: 7,478 (2.81/day)
Thanks: 798
Thanked 1,174 Times in 834 Posts
|
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.
__________________
CPU-Z validation sig pics temporarily blocked |
|
|
|
|
|
#14 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,564 (6.29/day)
Thanks: 1,751
Thanked 2,594 Times in 1,959 Posts
|
By the way, all the version numbers are on here:
http://en.wikipedia.org/wiki/History...ct_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
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } Last edited by FordGT90Concept; Jan 22, 2010 at 03:41 AM. |
|
|
|
|
|
#15 | |
|
Graphical Hacker
Join Date: Feb 2006
Location: San Antonio, Texas
Posts: 7,478 (2.81/day)
Thanks: 798
Thanked 1,174 Times in 834 Posts
|
Quote:
Code:
Environment.GetEnvironmentalVariable
__________________
CPU-Z validation sig pics temporarily blocked |
|
|
|
|
|
|
#16 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,564 (6.29/day)
Thanks: 1,751
Thanked 2,594 Times in 1,959 Posts
|
Environment.GetEnvironmentVariable()
It works on Server 2003 but I had Windows 7/Vista checked.
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
|
#17 |
|
Graphical Hacker
Join Date: Feb 2006
Location: San Antonio, Texas
Posts: 7,478 (2.81/day)
Thanks: 798
Thanked 1,174 Times in 834 Posts
|
__________________
CPU-Z validation sig pics temporarily blocked |
|
|
|
|
|
#18 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,564 (6.29/day)
Thanks: 1,751
Thanked 2,594 Times in 1,959 Posts
|
It is Environment, not Environmental (my brain fart, I got right a post or two before that
). I fixed the code you quoted of mine.
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
| The Following User Says Thank You to FordGT90Concept For This Useful Post: |
|
|
#19 |
|
Graphical Hacker
Join Date: Feb 2006
Location: San Antonio, Texas
Posts: 7,478 (2.81/day)
Thanks: 798
Thanked 1,174 Times in 834 Posts
|
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:
__________________
CPU-Z validation sig pics temporarily blocked Last edited by PVTCaboose1337; Jan 22, 2010 at 04:07 AM. |
|
|
|
|
|
#20 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,564 (6.29/day)
Thanks: 1,751
Thanked 2,594 Times in 1,959 Posts
|
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
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")
Edit: I updated both code areas in this post...
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } Last edited by FordGT90Concept; Jan 22, 2010 at 05:06 AM. |
|
|
|
|
|
#21 | |
|
Graphical Hacker
Join Date: Feb 2006
Location: San Antonio, Texas
Posts: 7,478 (2.81/day)
Thanks: 798
Thanked 1,174 Times in 834 Posts
|
Quote:
__________________
CPU-Z validation sig pics temporarily blocked |
|
|
|
|
|
|
#22 |
|
Graphical Hacker
Join Date: Feb 2006
Location: San Antonio, Texas
Posts: 7,478 (2.81/day)
Thanks: 798
Thanked 1,174 Times in 834 Posts
|
Having trouble calling a media file from resources. How would I accomplish this?
Code:
AxWindowsMediaPlayer1.URL = "My.Resources.music.mid"
__________________
CPU-Z validation sig pics temporarily blocked |
|
|
|
|
|
#23 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,564 (6.29/day)
Thanks: 1,751
Thanked 2,594 Times in 1,959 Posts
|
MIDI is going to be trouble. Can you convert it to a .wav? In wave format, you can use System.Media.SoundPlayer.
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
|
#24 | |
|
Graphical Hacker
Join Date: Feb 2006
Location: San Antonio, Texas
Posts: 7,478 (2.81/day)
Thanks: 798
Thanked 1,174 Times in 834 Posts
|
Quote:
Code:
AxWindowsMediaPlayer1.URL = "music.mid"
__________________
CPU-Z validation sig pics temporarily blocked |
|
|
|
|
|
|
#25 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,564 (6.29/day)
Thanks: 1,751
Thanked 2,594 Times in 1,959 Posts
|
Oh, embeded in your executable? Usually it is:
AssemblyName.Properties.Reserouces.name_of_resourc e
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Microsoft Visual Basic 3 Decompiler | Munki | Programming & Webmastering | 1 | Jun 4, 2009 04:32 AM |
| Visual Basic Tutorials | Wozzer | Programming & Webmastering | 9 | Nov 30, 2008 10:06 AM |
| Visual Basic Type Mismatch | Dark_Webster | Programming & Webmastering | 17 | Jul 20, 2008 07:48 PM |
| Visual Basic HELP | dcf-joe | Programming & Webmastering | 6 | Mar 5, 2008 01:36 PM |
| DOS File Transfer Commands: | djmonson | General Software | 22 | Jun 18, 2007 01:19 PM |