Option Explicit
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 Declare Function WaitForSingleObject Lib "kernel32" _
( _
ByVal hHandle As Long, _
ByVal dwMilliseconds As Long _
) As Long
Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = &HFFFFFFFF
'PROCESS_ALL_ACCESS All possible access rights for a process object.
'PROCESS_CREATE_PROCESS Required to create a process.
'PROCESS_CREATE_THREAD Required to create a thread.
'PROCESS_DUP_HANDLE Required to duplicate a handle using DuplicateHandle.
'PROCESS_QUERY_INFORMATION Required to retrieve certain information about a process, such as its exit code and priority class (see GetExitCodeProcess and GetPriorityClass).
'PROCESS_SET_QUOTA Required to set memory limits using SetProcessWorkingSetSize.
'PROCESS_SET_INFORMATION Required to set certain information about a process, such as its priority class (see SetPriorityClass).
'PROCESS_TERMINATE Required to terminate a process using TerminateProcess.
'PROCESS_VM_OPERATION Required to perform an operation on the address space of a process (see VirtualProtectEx and WriteProcessMemory).
'PROCESS_VM_READ Required to read memory in a process using ReadProcessMemory.
'PROCESS_VM_WRITE Required to write to memory in a process using WriteProcessMemory.
'SYNCHRONIZE Required to wait for the process to terminate using the wait functions.
Private Sub Command1_Click()
'Dim pId&, pHnd&
'声明pId
变量存储Process Id
'声明pHnd
变量存储Process Handle
'pId = Shell("Notepad", vbNormalFocus)
'Shell传回Process Id
'pHnd = OpenProcess(SYNCHRONIZE, 0, pId)
'取得 Process Handle
Dim pHnd& '进程句柄
pHnd = OpenProcess(SYNCHRONIZE, 0, Shell("Notepad", vbNormalFocus))
If pHnd <> 0 Then
'程序正在运行
Call WaitForSingleObject(pHnd, INFINITE)
'无限等待,直到程序结束
Call CloseHandle(pHnd)
MsgBox ("记事本已经关闭!")
End If
End Sub