Запустить программу и дождаться окончания работы

morpheus

скриптописец
07.08.2006
3 915
1
#1
Очень часто необходимо запустить приложение и дождаться когда оно(приложение) отработаеться. Например архиватор или какойто внешний обработчик. Привожу пример реализации
Не забываем что также реализовать подобную задачу можно чуть проще используя W Script

[codebox]Public Const NORMAL_PRIORITY_CLASS = &H20&
Public Const INFINITE = -1&

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

Public Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type

Declare Function WaitForSingleObject Lib "kernel32" (_
Byval hHandle As Long, _
Byval dwMilliseconds As Long) As Long

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

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

Declare Function CloseHandle Lib "kernel32" (Byval hObject As Long) As Long
Dim RetVal As Long
Dim proc As PROCESS_INFORMATION
Dim StartInf As STARTUPINFO

Private Function ShellAndWait(Byval RunProg As String) As Long
StartInf.cb = Len(StartInf)
StartInf.wShowWindow = 0&

RetVal = CreateProcessA(0&, RunProg, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, StartInf, proc)

RetVal = WaitForSingleObject(proc.hProcess, INFINITE)
RetVal = CloseHandle(proc.hProcess)

ShellAndWait = RetVal
End Function

Function RunExternalProgram(CmdLine As String) As Boolean
On Error Goto Errh

If CmdLine = "" Then Exit Function

Dim Result As Long
Result = ShellAndWait(CmdLine)

RunExternalProgram = True

Exit Function
Errh:
Exit Function
End Function[/codebox]

Как Вы уже поняли RunExternalProgram и будет принимать путь к запускаемому приложению. Попробует запустить, дождаться окончания работы и в случае успешной отработки вернёт True

Автор Omh
Ну я тут на автор, а скорее адаптатор.
Код найден на просторах интернета.


Обсуждалось тут