Sign in with
Sign up | Sign in
Your question

Running a Command Within Visual Basic 6?

Last response: in Windows 7
Share
May 20, 2010 10:30:02 PM

I'm trying to run a command WITHIN Visual Basic 6, i don't want a dos command window to pop up, that can't happen. More specifically, I want net view to run from within the Visual basic program i am designing, I want to be able to see the list of computers on a network from the program, is this possible? Any ideas? I'm still a novice at this, so a little step by step would help a bit maybe? Thanks guys. Peace.
May 21, 2010 8:41:32 AM

Visual Basic Language Reference
Shell Function

Runs an executable program and returns aninteger containing the program's process ID if it is still running.
Copy

Public Function Shell( _
ByVal Pathname As String, _
Optional ByVal Style As AppWinStyle = AppWinStyle.MinimizedFocus, _
Optional ByVal Wait As Boolean = False, _
Optional ByVal Timeout As Integer = -1 _
) As Integer

Parameters

Pathname
Required. String. Name of the program to execute, together with any required arguments and command-line switches. Pathname can also include the drive and the directory path or folder.
Style
Optional. AppWinStyle. A value chosen from the AppWinStyle enumeration corresponding to the style of the window in which the program is to be run. If Style is omitted, Shell uses AppWinStyle.MinimizedFocus, which starts the program minimized and with focus.

The Style argument can have one of the following settings:
Enumeration value Description
AppWinStyle.Hide The window is hidden and focus is given to the hidden window.
AppWinStyle.NormalFocus The window is given focus and displayed in its most recent size and position.
AppWinStyle.MinimizedFocus The window is given focus and displayed as an icon.
AppWinStyle.MaximizedFocus The window is given focus and displayed using the entire screen.
AppWinStyle.NormalNoFocus The window is set to its most recent size and position. The currently active window remains in focus.
AppWinStyle.MinimizedNoFocus The window is displayed as an icon. The currently active window remains in focus.
Wait
Optional. Boolean. A value indicating whether the Shell function should wait for completion of the program. If Wait is omitted, Shell uses False.
Timeout
Optional. Integer. The number of milliseconds to wait for completion if Wait is True. If Timeout is omitted, Shell uses -1, which means there is no timeout and Shell does not return until the program completes. Therefore, if you omit Timeout or set it to -1, it is possible that Shell might never return control to your program.

Exceptions/Errors
Exception type Error number Condition
ArgumentException 5 Style is not within range 0 through 9, inclusive.
FileNotFoundException 53 Shell cannot start the named program.
Remarks

The return value of the Shell function depends on whether the program named in Pathname is still executing when Shell returns. If you set Wait to True and the program finishes before the timeout expires, Shell returns zero. If the timeout expires, or if you omit Wait or set it to False, Shell returns the process ID of the program. The process ID is a unique number that identifies the running program.

If the Shell function cannot start the named program, a System.IO.FileNotFoundException error occurs. This can happen, for example, when you attempt to run a 16-bit program, such as command.com, from an application using System.Windows.Forms. For a workaround, you can run a 32-bit program that calls the desired 16-bit program. In the case of command.com, you can run cmd.exe as an alternative.

By default, the Shell function runs the program asynchronously. This means that a program started with the Shell function might not finish executing before the statements following the Shell function are executed. If you want to wait for the program to finish before you continue, set Wait to True.

You should always enclose the entire path and file specification in quotes, as the following example shows:
Copy

ID = Shell("""C:\Program Files\MyFile.exe"" -a -q", , True, 100000)

Each pair of adjacent double quotes ("") within the string literal is interpreted as one double quote character in the string. Therefore, the preceding example presents the following string to the Shell function:
Copy

"C:\Program Files\MyFile.exe" -a -q

If you did not have the path enclosed in quotes, Windows would look for a file called Program.exe in the C:\ directory, instead of MyFile.exe in the C:\Program Files directory.

Security Note If you do not enclose the path and file specification in quotes, there is a security risk if the file name or a path node contains spaces. In the preceding example, the path node \Program Files includes a space. If the specification were not inside quotes and a program named Program.exe had been installed in C:\, for example by illicit tampering, Windows would execute it instead of MyFile.exe.

Security Note The Shell function requires unmanaged code permission, which might affect its execution in partial trust situations. For more information, see SecurityPermission Class and Code Access Permissions.

Example

This example uses the Shell function to run an application specified by the user. Specifying AppWinStyle.NormalFocus as the second argument opens the application in normal size and gives it the focus.
Copy

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.
m
0
l
a b $ Windows 7
May 21, 2010 8:14:22 PM

Or to keep things neat and tidy, download this VB6 class ..

http://www.vbforums.com/attachment.php?s=1eaba47a86f4ce...

  1. Dim SW As CNetworkEnum 'Decalre class
  2. Set SW = New CNetworkEnum 'Set class to SW
  3. Call SW.SetResourceType(0) 'Set resource type
  4. Call SW.Reset
  5. Dim ST As String
  6. ST = Replace(SW.GetServerList, "\\", "") 'Replace the \\ in the computer names
  7. 'loop through the list of computer names and put the in the listbox
  8. For i = 1 To Len(ST)
  9. If Mid(ST, i, 1) = "," Then
  10. lstTerminals.AddItem (Left(ST, i - 1))
  11. ST = Right(ST, Len(ST) - i)
  12. i = 1
  13. End If
  14. Next i


Not my stuff, but you should be able to alter to match your code.
m
0
l
!