VB执行某程序后,如何判断这个程序已经执行完毕?

来源:百度知道 编辑:UC知道 时间:2024/05/06 07:21:48
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Function IsRunning(ByVal ProgramID) As Boolean ' 传入进程标识ID
Dim hProgram As Long '被检测的程序进程句柄
hProgram = OpenProcess(0, False, ProgramID)
If Not hProgram = 0 Then
IsRunning = True
Else
IsRunning = False
End If
CloseHandle hProgram
End Function
========================================================================
x = Shell(App.Path & "/x.exe", vbHide)
While IsRunning(x)
DoEvents
Wend
MsgBox "安装完毕!"
====================================================

Option Explicit
Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = &HFFFFFFFF

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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Sub Form_Load()
Dim Pid As Long
Dim hWnd As Long

Pid = Shell("calc.exe", vbMinimizedFocus)

hWnd = OpenProcess(SYNCHRONIZE, 0, Pid)

Do
DoEvents
Loop Until WaitForSingleObject(hWnd, 0) = 0

MsgBox "Process Finished!"
CloseHandle hWnd
End Sub