...

View Full Version : Visual basic: sending a command to DOS



Anthony2oo4
01-14-2005, 05:27 PM
Hey guys im tryin to make a very basic messenger useing the DOS function "netsend". For those of you that havnt yet seen netsend, you basicly open a dos promt and type "netsend <user name of person sending to> <Message>".

Currently I have wrote the following:


Private Sub cmdsend_Click()
'Make Varibles
Dim virbname As String
Dim virbsendto As String
Dim virbmessage As String
Dim all As String

'Give Varible values
virbname = tbname.Text
virbsendto = tbsendto.Text
virbmessage = tbmessege.Text
all = "netsend" + " " + virbsendto + " " + virbname + ":" + " " + virbmessage
MsgBox all
End Sub

My only problem is that i dont know how to get the send button to send the command to dos on windows 2000.

I have used a msgbox only for testing purposes to see if the code was working correctly.

And help please?

Thanks for your time, Anthony ;)

P.S., i dont think this is possible, but is there a way that i can get the messege that is recived from the other users on the network, and place it in a lable or anythin, instead of it showing as a prompt on screen?

sage45
01-14-2005, 07:35 PM
Use Shell:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctshell.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/objects/objects.asp


Dim ProcID As Integer
' Run Calculator.
ProcID = Shell("C:\Windows\system32\calc.exe", AppWinStyle.NormalFocus)
' The preceding path is for Windows XP;
' The Windows 2000 path is C:\WINNT\system32\calc.exe.

HTH,

-sage-

Anthony2oo4
01-14-2005, 11:25 PM
i may be wrong, im only a newbie, but aint integer for numbers?

Also this aint showing me how to run dos commands :confused:

Sorry if it does and im just stupid :D

sage45
01-15-2005, 12:09 AM
You could also use:

Dim SomeVar As String

The whole point of the code is just to provide one way that the keyword was used... ;)

Hence providing links to MSDN to show you other possible ways to implement it... :D

-sage-

Anthony2oo4
01-15-2005, 12:14 AM
OK, thanks for the sites and the code, but i dont fully understand. What would i need to do here?

Thanks for your help

scroots
01-15-2005, 02:35 AM
I once used this sort of thing but for ftp via dos. I saved the command to a dos batch file then i used the shell command to run it. I will see if i can locate some code.

scroots

obiwanjabroni
01-15-2005, 04:32 AM
It's been a while since I've programmed in Visual Basic, but check the MSDN documentation on MsgBox. I believe you can set other options like how many buttons will appear on the message box etc. I believe you can also specify various function to call in the eventuality one of the buttons are pressed. Or maybe it was that the call to MsgBox returns a value depending on what was clicked by the used. Again, check MSDN documentation, but if this is the case, you can use a control structure to get the program to write the command to the console.

Again, use the Shell command. The Shell command executes a string, but returns an integer. I'm still a little fuzzy on it, but the returned value either specifies the processor id (PID) of the command you just executed or it returns 1 for successful execution and 0 for an unsuccessful execution. Be careful with the Shell command because it's a forked process, that is, it's not sequential to the rest of your code.

Anthony2oo4
01-15-2005, 02:07 PM
Ok, well is there a way that i can get it to:

make a bat file where the .exe file is located
write to it
then delete it

??

Thanks for your time guys ;)

Anthony2oo4
01-15-2005, 02:41 PM
OK guys i think im getting some where :D


Private Sub cmdsend_Click()
'Make Varibles
Dim virbname As String
Dim virbsendto As String
Dim virbmessage As String
Dim all As String

'Give Varible values
virbname = tbname.Text
virbsendto = tbsendto.Text
virbmessage = tbmessege.Text
all = "netsend" + " " + virbsendto + " " + virbname + ":" + " " + virbmessage
tbmessege.Text = Delete
Open App.Path & "\run.bat" For Output As #1
Print #1, all
Close #1
Shell (App.Path & "\run.bat")
End Sub

I dont know if the shell part is correct and also, what code would i use to delete the run.bat file after it has been executed.

Thanks again :D

obiwanjabroni
01-15-2005, 09:54 PM
Shell ("del " & App.Path & "\run.bat")

But if your batch file is long, you need to make sure that the batch file is finished running before you execute the above command, or else you might get strange errors. Use a while loop that will test some sort of condition that will exist after the batch file finishes executing, and the moment it detects this, use the above:

While (TEMINATION CONDITION)
End While
Shell("del " & App.Path & "\run.bat")

It's in pseudo code since I don't remember the exact syntax.

Anthony2oo4
01-15-2005, 11:39 PM
Well the bat file aint gonna have no more than say:


netsend jjones hello mate hows it goin?

so hopefully this wont be a problem

is there a way i can delay the script execution for like 1 sec.

your soloution would probibly work great but me being a newbie, i aint got a clue on how to fill in the missing parts. Unless someone else can :o

Thanks again :thumbsup:

obiwanjabroni
01-16-2005, 12:23 AM
Okay, well, create a timer class with an interval of 1000 (milliseconds) and leave it disabled. Once you run the shell command to execute the batch, activate the timer. Put the code inside the timer to delete the file and it should all be dandy.

Anthony2oo4
01-16-2005, 12:29 AM
Ok, iv found a way around the problem of if the file is getting used, it cant delete stated above. here is my new code.


Private Sub cmdsend_Click()
'Make Varibles
Dim virbname As String
Dim virbsendto As String
Dim virbmessage As String
Dim all As String
Dim location As String

'Give Varible values
location = App.Path & "\run.bat"
virbname = tbname.Text
virbsendto = tbsendto.Text
virbmessage = tbmessege.Text
all = "netsend" + " " + virbsendto + " " + virbname + ":" + " " + virbmessage
tbmessege.Text = Delete

'Open the file if it exists, if not, create it.
Open location For Output As #1

'Write the messege into the file
Print #1, all
Close #1

'Send the file
Shell (location)

End Sub

Private Sub Command1_Click()
'Delete the file run.bat
Shell ("del " & App.Path & "\run.bat")
End
End Sub

Private Sub Form_Load()
'Create the file run.bat
Open App.Path & "\run.bat" For Output As #1
Close #1
End Sub

Now my last problem is that it wont delete the file on clicking the exit button (which is command1). It givens me an error of "Can't find file".

I dont mean to sound pussy or nothing but i need this file by the end of tomorrow at about 8PM (its 11:30PM now). :p

So if anyone can help me on this last problem, then i would be very happy :D

Thanks for all your help :thumbsup:

obiwanjabroni
01-16-2005, 12:36 AM
If you can, try printing out the contents of App.Path to another msgbox. I had a problem once with VB where it would reset the base directory while changing subs. Is App.Path an absolute path (C:\blah\blah) or a relative path (.\blah\blah). The error you are getting is related to this pathing issue. Check this first before I give any solutions because I'm not sure of the "official" way around this error, although I do know of a workaround solution that's more involved (but is better for systems since it won't clutter the registry).

Anthony2oo4
01-16-2005, 12:41 AM
ok i fixed it, all works great now. Instead of:


'Delete the file run.bat
Shell ("del " & App.Path & "\run.bat")
End

i used:


Dim location As String
location = App.Path & "\run.bat"
Kill location

And that workede great. I'd like to that all of your for the work that was placed into here. And for your time ;)

:D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum