VBNotebook Home | Functions | Extended Shell Functions
These functions shell out to other programs and associated documents.
__________________________________________________________________________________________
The first routine, ExecuteProgram, shells and waits until the called process is no longer active or it can wait for the optionally specified time out period. The sCommandLine parameter is the Command Line to use. The second parameter, ExecMode, is optional and uses the ShowWindowTypes enum to determine how the shelled process will be displayed. The final parameter, dwTimeOut, tells how long to wait for the shelled process to complete before returning. The time is in milliseconds. The default value of zero indicates an infinite wait time. The routine returns a boolean that, if True, indicates that the program ran successfully.
The following code is needed in the General|Declarations area of the module.
__________________________________________________________________________________________
'
' ExecuteProgram Constants
'
Private Const IGNORE = 0 'Ignore signal
Private Const INFINITE = -1& 'Infinite timeout
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const STILL_ACTIVE = &H103
Private Const SYNCHRONIZE = &H100000
Private Const WAIT_ABANDONED = &H80&
Private Const WAIT_FAILED = -1& 'Error on call
Private Const WAIT_OBJECT_0 = 0 'Normal completion
Private Const WAIT_TIMEOUT = &H102& 'Timeout period elapsed
'
' ShowWindow() Enum Constants
'
Public Enum ShowWindowTypes
SW_HIDE = 0
SW_SHOWNORMAL = 1
SW_NORMAL = 1
SW_SHOWMINIMIZED = 2
SW_SHOWMAXIMIZED = 3
SW_MAXIMIZE = 3
SW_SHOWNOACTIVATE = 4
SW_SHOW = 5
SW_MINIMIZE = 6
SW_SHOWMINNOACTIVE = 7
SW_SHOWNA = 8
SW_RESTORE = 9
SW_SHOWDEFAULT = 10
End Enum
'
' API Calls
'
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, _
lpExitCode As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
__________________________________________________________________________________________
Now, the routine itself....
__________________________________________________________________________________________
Public Function ExecuteProgram(ByVal sCommandLine As String, _
Optional ByVal ExecMode As ShowWindowTypes = SW_SHOWNORMAL, _
Optional ByVal dwTimeOut As Long = 0) As Boolean
On Error GoTo ExecuteProgram_Error
Dim hProcessID As Long
Dim hProcess As Long
Dim nRet As Long
If ExecMode < SW_HIDE Or ExecMode > SW_RESTORE Then
ExecMode = SW_SHOWNORMAL
End If
If dwTimeOut = 0 Then
hProcessID = Shell(sCommandLine, CLng(ExecMode))
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, hProcessID)
Do
GetExitCodeProcess hProcess, nRet
DoEvents
Sleep 100
Loop While nRet = STILL_ACTIVE
Else
hProcessID = Shell(sCommandLine, CLng(ExecMode))
hProcess = OpenProcess(SYNCHRONIZE, False, hProcessID)
nRet = WaitForSingleObject(hProcess, CLng(dwTimeOut))
End If
ExecuteProgram = True
Exit Function
ExecuteProgram_Error:
ExecuteProgram = False
End Function
__________________________________________________________________________________________
The second routine opens a document by executing the associated application.
The first parameter is the name of the document. The second is the path for the document. The first optional parameter is the hWnd of the calling program's window. If this parameter isn't used, the hWnd of the desktop is used. The second optional parameter describes how to show the program's window. The routine returns a True if the document was launched successfully.
__________________________________________________________________________________________
Public Function OpenDocument(ByVal hHostWnd As Long, ByVal sDocumentName As String, _
ByVal sDocumentPath As String, _
Optional ByVal nShow As ShowWindowTypes = SW_SHOWMAXIMIZED)
_
As Boolean
Dim nRet As Long
If hHostWnd = 0 Then
hHostWnd = GetDesktopWindow()
End If
nRet = ShellExecute(hHostWnd, vbNullString, sDocumentName,
_
vbNullString, sDocumentPath, nShow)
If nRet > 32 Then
OpenDocument = True
Else
OpenDocument = False
End If
End Function
__________________________________________________________________________________________
Copyright 2000-2005, J. Frank Carr