Const INFINITE = &HFFFF
'StartupInfo constants
Public Const STARTF_FORCEOFFFEEDBACK = &H80
Public Const STARTF_FORCEONFEEDBACK = &H40
Public Const STARTF_RUNFULLSCREEN = &H20
Public Const STARTF_USECOUNTCHARS = &H8
Public Const STARTF_USEFILLATTRIBUTE = &H10
Public Const STARTF_USEPOSITION = &H4
Public Const STARTF_USESHOWWINDOW = &H1
Public Const STARTF_USESIZE = &H2
Public Const STARTF_USESTDHANDLES = &H100
'ShowWindow constants
Public Const SW_HIDE = 0
Public Const SW_SHOWNORMAL = 1
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_MAXIMIZE = 3
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_SHOWNOACTIVATE = 4
Public Const SW_SHOW = 5
Public Const SW_MINIMIZE = 6
Public Const SW_SHOWMINNOACTIVE = 7
Public Const SW_SHOWNA = 8
Public Const SW_RESTORE = 9
Public Const SW_SHOWDEFAULT = 10
Public Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Public 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
Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (Byval lpApplicationName As String, Byval lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, Byval bInheritHandles As Long, Byval dwCreationFlags As Long, lpEnvironment As Any, Byval lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Declare Function CloseHandle Lib "kernel32" (Byval hObject As Long) As Long
Declare Function WaitForSingleObject Lib "kernel32" (Byval hHandle As Long, Byval dwMilliseconds As Long) As Long
Declare Function GetLastError Lib "kernel32" () As Long
Function RunAndWait(ComLine As String, DefaultDir As String) As Boolean
Dim si As STARTUPINFO
Dim pii As PROCESS_INFORMATION
si.cb = 72
si.wShowWindow = 1
si.dwFlags = STARTF_USESHOWWINDOW
Dim ret As Long
ret = CreateProcess(0, ComLine, Byval 0&, Byval 0&, False, 0, Byval 0&, DefaultDir, si, pii)
If ret Then
WaitForSingleObject pii.hProcess, INFINITE
CloseHandle pii.hProcess
RunAndWait = True
Exit Function
Else
Dim err1 As Long
err1 = GetLastError()
Msgbox "LastError = " & err1
End If
RunAndWait = False
End Function
....
If RunAndWait("calc.exe", "c:\windows") Then
Msgbox "Run OK!"
End If