Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to execute DOS commands from VB? 1

Status
Not open for further replies.

Firemyst

Programmer
Mar 10, 2003
62
0
0
US
Hi everyone,

I'm writing a VB6 application, and need to have it execute the DOS "net send userid message" command so that the particular user "userid" who's logged into the network will receive the "message" in the popup box on their workstation.

Can anyone please tell me how this is done?

Thanks!
 
x = Shell("COMMAND.COM /C NET SEND " & UserID & " " & Message)

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
Sorry, that should say "CMD.EXE" not "COMMAND.COM"

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
Guys

I tried the following:

Form load:

Dim User As Variant
Dim Message As Variant
User = TXT_User.Text
Message = TXT_Message.Text

Command Click:

x = Shell("CMD.exe /C NET SEND " & User & " " & Message)

Nada.

Any thoughts...?

----------------------------
Cogito Ergo Sum [jester2]
----------------------------
 
Use strings not variants. This works for me:

Dim strText as String
Dim strUser as String
strText = "Hello"
strUser = "User1"
shell("cmd /c net send " & struser & " " &strtext)


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Doh...!

I'm at work - no reference material to work with otherwise I'd have picked up on that.

Thanks Mate.

----------------------------
Cogito Ergo Sum [jester2]
----------------------------
 
John

I changed the Variants to strings but I'm still getting nothing. The program executes but no Net Send message appears.

I'm thinking it's the x in this string:

x = Shell("CMD.exe /C NET SEND " & User & " " & Message)

I haven't declared it explicitly and have no idea what it's doing there but without it the command doesn't execute at all.

----------------------------
Cogito Ergo Sum [jester2]
----------------------------
 
John

Here's what I ended up with:

Form Load:

Dim User As String
Dim Message As String
User = "TXT_User.Text"
Message = "TXT_Message.Text"

Command Click:

Shell("cmd /c net send " & User & " " & Message)

Still nothing though - but no errors either. Strange.

----------------------------
Cogito Ergo Sum [jester2]
----------------------------
 
I suppose you've checked that the Messenger service is running? There was a flap about messenger vulnerabilities a few months ago, and some sysadmins took the service off.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Harlequin007

First in the code above you have in the load event,
Code:
    Dim User As String
    Dim Message As String
    User = "TXT_User.Text"
    Message = "TXT_Message.Text"
The variable declaration shouldn't be here if you plan to use them in another procedure. They must be at the beginning of the file and should be like
Code:
    Public User As String
    Public Message As String
In the form load event you also have the User and Message variable with string values but no the ones in the textboxs, use this instead
Code:
    User = TXT_User.Text
    Message = TXT_Message.Text
The same methods that you're trying should work, however, this works for me, I get it some where in this forum
Code:
Private Sub Command1_Click()
 Dim User As String
 Dim Message As String
 User = "david"
 Message = "hello"
 DosCommand "net send " + User + " " + Message, vbHide
End Sub

Public Sub DosCommand(sCmd As String, WindowsStyle As VbAppWinStyle)
    Dim hShell As Long
    hShell = Shell(Environ$("Comspec") & " /c " & sCmd, WindowsStyle)
End Sub

Hope this helps,
David.
 
Ok David.

These are the changes I made mate:

*** At the top of the code window I have:

Public User As String
Public Message As String

*** In the Form Load I have:

Private Sub Form_Load()

User = "TXT_User.Text"
Message = "TXT_Message.Text"

End Sub

*** And for the on click:

Private Sub CMD_Net_Send_Click()

X = Shell("CMD.exe /c net send" + " User" + " Message")
TXT_User.Text = ""
TXT_Message.Text = ""

End Sub

And still nothing.

Sorry David, this is so damn frustrating.

----------------------------
Cogito Ergo Sum [jester2]
----------------------------
 
First check the code
Code:
Private Sub Form_Load()
    
    User = "TXT_User.Text"
    Message = "TXT_Message.Text"
    
End Sub
It should be
Code:
Private Sub Form_Load()
    
    User = TXT_User.Text
    Message = TXT_Message.Text
    
End Sub
Also change
Code:
Private Sub CMD_Net_Send_Click()

    X = Shell("CMD.exe /c net send" + " User" + " Message")
    TXT_User.Text = ""
    TXT_Message.Text = ""
    
End Sub
with
Code:
Private Sub CMD_Net_Send_Click()

     X = Shell("CMD.exe /c net send " + User + " " + Message)
    TXT_User.Text = ""
    TXT_Message.Text = ""
    
End Sub

It works for me

Hope this helps,
David.
 
Harlequin7,

sticking as close as I can to your code, here is a working version (assuming that TXT_User and TXT_Message contain valid info):
Code:
 Public User As String
    Public Message As String

Private Sub Form_Load()
    
    User = TXT_User.Text
    Message = TXT_Message.Text
    
End Sub

Private Sub CMD_Net_Send_Click()
    Dim x As Long
    x = Shell("CMD.exe /c net send " & User & " " & Message)
    TXT_User.Text = ""
    TXT_Message.Text = ""
    
End Sub
I'm guessing from some elementary errors of understanding in this thread that you are a VB beginner. It is probably worth pointing out that we often won't have the time (or inclination) to teach the basics of the language in this forum, which migfht be why you are getting frustrated

 
Yeah - I am a beginner. But I really do appreciate your help, all of you. The books only go so far and I like to experiment, experiment a hell of a lot and I was sending a net send message to a friend at work and just wonderd "Hey - it can't be that difficult".

Guess how wrong was I...?

I'm going to tinker with this over the coming days. Thanks for all your efforts guys [colorface]

----------------------------
Cogito Ergo Sum [jester2]
----------------------------
 
Harlequin007,

You need to insert the key words: Option Explicit at the beginning of the code window to require variable declarations. You can do the following to make it default.

Tools menu -> Options -> and check Require Variable Declaration

If this was done in the beginning you would have received an error with your original code explaining that your variables haven't been declared. Which would have made debugging much easier.

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 
Woo, just found this place and here is my 1st post!
Err, if you want to extract the values from 2 text fields on a form and pass to a shell command here is wot I would do:

Command_Click()
dim User as String
dim Message as String
User = TXT_User.Text
Message = TXT_Message.Text

x = Shell("CMD.exe /C NET SEND " & User & " " & Message)

Can't think why you would want the 2 vars to be declared on form load cause then the fields would be blank.
Hope this helps somewhat
 
Guys

I know I've been REALLY stupid here but Moon made something so obvious it's unreal.

why declare the text field values at form load - Doh...!

Guess what...? I moved the User = yadda, uadda and hey presto...!

Thanks once again for all your patience . . .

----------------------------
Cogito Ergo Sum [jester2]
----------------------------
 
That meant you didn't read my first reply nor the strongm one, we did tell you that, declare the variables outside the procedures and at the beginning of the file, also DrJavaJoe told you that your variables weren't declare.

You should take the time to read and understand the replys you get in here.

 
My first post also covered that point! In fact Moonspell's is just the same as that first post

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
as i am new here i will not start an arguement with johnwm but i think you will find my post is quite different. for one thing it answered and solved the problem! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top