Results 1 to 10 of 10

Thread: [RESOLVED] Execute command prompt commands within VB

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Resolved [RESOLVED] Execute command prompt commands within VB

    Hi all,

    I would like to make a program in which I need to execute commands from the command prompt. Basically, I would like to utilize net send to make a small (basic) messaging client, using the net send function. It would basically provide a friendly graphical interface for net send, for normal users to use (rather than having to type in cryptic commands at a command prompt).

    http://www.arbital24.com/Pics/IMess.JPG

    That is a basic idea of what I want to do. The "send message to" combo-box will either be a pre-defined list, a text box for the user to enter themselves, or best of all (if possible), an enumerated list of local network device names, generated on program launch or dynamically.

    The "Message" box will of course be the message text. The idea is that the users inputs the data, and the program uses a "net send <var1> <var2>", where var1 and var2 will of course be the user and message box texts, copied into variablesa, respectively.

    Basically, I need to know how to generate command prompt commands from within VB, if it is possible. Also, if anyone known how to enumerate locally network connected stations within VB, that would be great too .

    Thanks,
    Arby.

  2. #2
    New Member
    Join Date
    Nov 2006
    Posts
    1

    Re: Execute command prompt commands within VB

    I was able to use this:
    Code:
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (
     ByVal hwnd As Long, 
     ByVal lpOperation As String, 
     ByVal lpFile As String, 
     ByVal lpParameters As String, 
     ByVal lpDirectory As String, 
     ByVal nShowCmd As Long
    ) As Long
    
    Const SW_SHOWNORMAL = 1
    
    Private Sub Form_Load()
        ShellExecute Me.hwnd, vbNullString, "net", "send 192.6.3.125 Hello", "C:\", SW_SHOWNORMAL
    End Sub
    You can add variables as needed in the parameters section.

    -Philip

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Execute command prompt commands within VB

    Ahh, looks good, thanks!

    So, I guess to use variables, you you use
    ShellExecute Me.hwnd, vbNullString, "net", "send " & ipvar & " " & msgvar", "C:\", SW_SHOWNORMAL
    ?
    Also, what do the "C:\" and SW_SHOWNORMAL parameter do?

    Thanks!

  4. #4
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Execute command prompt commands within VB

    Why not simply use the intrinsic Shell() function in VB for this?

    You might also be interested to know that your program could do this directly without shelling a command. It's a little messier since you need to write to the Messenger mailslot, which requires a few API calls in VB.

    There is an example at:

    http://vbnet.mvps.org/index.html?cod...tbroadcast.htm

  5. #5
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: Execute command prompt commands within VB

    You could also make a BAT file. That makes it easy to find errors. Of course you might not want vbNormalFocus and Pause after it is debugged.

    Mac

    Code:
    Option Explicit
    Dim Sent1 As Boolean
    
    Private Sub cmdSend_Click()
    If Not Sent1 Then
      Open "doit.bat" For Output As #1
      Print #1, "net send /Users"
      Print #1, "pause"
      Print #1, "exit"
      Close #1
      Sent1 = True
    End If
    Shell "Doit.bat", vbNormalFocus
    End Sub

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Execute command prompt commands within VB

    dilettante - I'm not sure, how could I use the Shell() function to perform this? Could you post some sample code?
    Also, that sounds good, but it's complex, and a lot of code just to do something which (i believe) should be fairly simple.

    Mr.Mac - Thanks, but I'd rather stay away from bat files, as it means that the end users have to install/locate the files themselves. It means keeping track of more than just one self-contained file, and if the bat gets deleted somehow, the program will screw up. I did think about using a .bat, though.

  7. #7
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: Execute command prompt commands within VB

    Quote Originally Posted by Arby
    it means that the end users have to install/locate the files themselves
    Not arguing with your decision to stay away from BAT files, but if you are ever in a situation where they are required, keep in mind: you can generate the file, run it, and then on EXIT, delete. The user is not the wiser. You could use a name like "v8m43m2a.bat" if you feared destroying a user file.

    I have had trouble with
    SHELL "xxx > z.lst"
    for some reason, but putting it in a BAT file overcomes that.

    (Maybe I am thinking of QBasic)

    Anyway, good luck. I guess you can flag this topic as resolved if you have everything you need.

    Mac

  8. #8
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Execute command prompt commands within VB

    Why not use the shell like this

    Shell "cmd /c shutdown -l"

    replace shutdown with your own command?

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  9. #9
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: Execute command prompt commands within VB

    Quote Originally Posted by Mxjerrett
    cmd /c
    Yeah. That works! Thanks for reminding me. So BAT files would only be useful if several associated commands were there. Single commands should work fine with cmd /c.

    Mac

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Execute command prompt commands within VB

    Ah, lots of great information, thanks all!

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