Resources » Code Snippets » VB.NET Syntax

Shell & Shell wait Command in VB


Posted Date: 12-Aug-2009  Last Updated:   Category: VB.NET Syntax    
Author: Member Level: Bronze    Points: 10



Running another Executable Program Using Shell Command
Visual Basic 6.0

Introduction:

We've lot of components in VB, but some of these we rarely need like “Running another Executable program from the Visual basic application”.

Purpose:
The purpose of this document is to explain how to run another executable program from the visual basic Application.
Runs an executable program and returns an integer containing the program's process ID if it is still running.

This Procedure is intended to execute another application from the visual basic function.

Shell
Syntax:

Shell(PathName,[WindowStyle as VbAppWinstyle = AppWinStyle.MinimizedFocus]
) As Double


Parameters

Pathname
It is a String parameter. You should give the 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.

If you do not know the path to the program,
You can use the My.Computer.FileSystem.GetFiles Method to locate it.
For example,
You can call My.Computer.FileSystem.GetFiles("C:\", True, "testFile.txt"), which returns the full path of every file named testFile.txt anywhere on drive C:\.

Style
This is Optional argument. AppWinStyle.
A value chosen from the AppWinStyle Enumeration specifying the style of the window in which the program is to run.
If Style is omitted, Shell uses AppWinStyle.MinimizedFocus, which starts the program minimized and with focus.


Code:
The following example uses the Shell function to run an application specified by the user. Specifying Microsoft.VisualBasic.AppWinStyle.NormalFocus as the second argument opens the application in normal size and gives it the focus.




' **************************************************************
' Function Name : Shell
' Purpose : Running another Executable program from the Visual basic application
' Parameter List : PathName, WindowStyle as VbAppWinstyle

'****************************************************************

'Declaring Variable
Dim procID As Integer

Private Sub Cmd_Calc_Click()

'Run Calculator.
procID = Shell("C:\Windows\system32\calc.exe", vbNormalFocus)

End Sub

Private Sub Cmd_Notepad_Click()

'Run Notepad.
procID = Shell("C:\Windows\system32\Notepad.exe", vbNormalFocus)

End Sub



ShellAndWaitForTermination

Syntax:


Public Function ShellAndWaitForTermination(sShell As String, Optional ByVal eWindowStyle As VBA.VbAppWinStyle = vbNormalFocus, Optional ByRef sError As String, Optional ByVal lTimeOut As Long = 2000000000) As Boolean


Parameters:

sShell:
It is a String parameter. You should give the name of the program to execute.
Style:
This is Optional argument. AppWinStyle.
A value chosen from the AppWinStyle Enumeration specifying the style of the window in which the program is to run.
If Style is omitted, Shell uses AppWinStyle.MinimizedFocus, which starts the program minimized and with focus

sError:
This is Optional argument. If there is any error during shell execute, this variable will hold the error message

lTimeOut:
This is Optional argument. By default the value will be 2000000000



Code:
The following example uses the ShellAndWaitForTermination function to run an application specified by the user.

The control will wait up to the Executable application has complete the job.



Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Declare Function timeGetTime Lib "winmm.dll" () As Long

Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long

Private Const PROCESS_QUERY_INFORMATION = &H400

Private Const STILL_ACTIVE = &H103



' *******************************************************************
'Function Name : ShellAndWaitForTermination
'Purpose : Running another Executable program from the Visual basic application
'Parameter List : PathName, WindowStyle as VbAppWinstyle

'********************************************************************
Public Function ShellAndWaitForTermination(sShell As String, Optional ByVal eWindowStyle As VBA.VbAppWinStyle = vbNormalFocus, Optional ByRef sError As String, Optional ByVal lTimeOut As Long = 2000000000) As Boolean
Dim hProcess As Long
Dim lR As Long
Dim lTimeStart As Long
Dim bSuccess As Boolean

On Error GoTo ShellAndWaitForTerminationError

hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(sShell, eWindowStyle))

If (hProcess = 0) Then
sError = "This program could not determine whether the process started. Please watch the program and check it completes."
' Only fail if there is an error - this can happen when the program completes too quickly.
Else
' wait for five seconds
Sleep 5000
bSuccess = True
lTimeStart = timeGetTime()
Do
' Get the status of the process
GetExitCodeProcess hProcess, lR

' Sleep during wait to ensure the other process gets
' processor slice:
DoEvents: Sleep 1000

If (timeGetTime() - lTimeStart > lTimeOut) Then
' Too long!
sError = "The process has timed out."
lR = 0
bSuccess = False
End If

'Loop While nFMSDOSHandle > 0 'lR = STILL_ACTIVE
Loop While (lR = STILL_ACTIVE) And bSuccess

End If
ShellAndWaitForTermination = bSuccess

Exit Function

ShellAndWaitForTerminationError:
sError = Err.Description
Exit Function
End Function



How it works
The return value of the Shell function depends on whether the program named in PathName is still executing when Shell returns.
Shell returns the process ID of the program. The process ID is a unique number that identifies the running program.

Waiting for Completion
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,
Use the function ShellAndWaitForTermination.

Conclusion:
This shell command used to run another Executable application from the Visual basic Code.
The function ShellAndWaitForTermination provide the option to wait the control until the program has to complete, which is started by the shell function might not finish executing.

A program started with the shell function might not finish executing, if you want to wait the control until the program has to complete.


Did you like this resource? Share it with your friends and show your love!

Responses to "Shell & Shell wait Command in VB"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    jack
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India