Option Explicit
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Private Const INFINITE = -1&
Private Const MAX_FILENAME_LEN = 256
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
Private Declare Function CreateProcessA Lib "kernel32" _
(ByVal lpApplicationName As Long, ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function FindExecutableA Lib "shell32.dll" _
(ByVal lpFile As String, ByVal lpdirectory As String, _
ByVal lpResult As String) As Long
Private Declare Function CloseHandle Lib "kernel32" (hObject As Long) As Boolean
Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
'----------------------------------------------------------
' Procedure : ShellWait
' Purpose : Runs a command as the Shell command does but waits for the command
' to finish before returning.
' The full path and filename extention are required
' Arguments : cCommandLine The command to be shelled.
' Returns : FALSE if the shell failed
'----------------------------------------------------------
'
Public Function ShellWait(cCommandLine As String) As Boolean
Dim NameOfProc As PROCESS_INFORMATION
Dim NameStart As STARTUPINFO
Dim i As Long
NameStart.cb = Len(NameStart)
i = CreateProcessA(0&, cCommandLine, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, NameStart, NameOfProc)
If i <> 0 Then
Call WaitForSingleObject(NameOfProc.hProcess, INFINITE)
Call CloseHandle(NameOfProc.hProcess)
ShellWait = True
Else
ShellWait = False
End If
End Function
'----------------------------------------------------------
' Procedure : ExecuteWait
' Purpose : Executes a file with it's associated program and waits for the
' : process to finish before returning.
' Arguments : FileName The name of the file (path & extension) to open.
' Returns : returns true on success.
'----------------------------------------------------------
'
Public Function ExecuteWait(ByVal Filename As String) As Boolean
Dim ApplicationName As String
ApplicationName = FindExecutable(Filename)
If ApplicationName <> "" Then
ExecuteWait = ShellWait(ApplicationName & " " & Filename)
Else
ExecuteWait = False
End If
End Function
'---------------------------------------------------------------------------------------
' Procedure : FindExecutable
' Purpose : Finds the executable associated with a file
' Returns : "" if no file is found.
'---------------------------------------------------------------------------------------
'
Private Function FindExecutable(Filename As String) As String
Dim i As Integer
Dim ApplicationName As String
ApplicationName = String(MAX_FILENAME_LEN, 32) & Chr$(0)
i = FindExecutableA(Filename & Chr$(0), vbNullString, ApplicationName)
If i > 32 Then
FindExecutable = Left$(ApplicationName, InStr(ApplicationName, Chr$(0)) - 1)
Else
FindExecutable = ""
End If
End Function