Option Explicit
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPheaplist = &H1
Private Const TH32CS_SNAPthread = &H4
Private Const TH32CS_SNAPmodule = &H8
Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
Private Const MAX_PATH As Integer = 260
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Const PROCESS_CREATE_PROCESS = &H80
Private Const PROCESS_CREATE_THREAD = &H2
Private Const PROCESS_DUP_HANDLE = &H40
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const PROCESS_QUERY_LIMITED_INFORMATION = &H1000
Private Const PROCESS_SET_QUOTA = &H100
Private Const PROCESS_SET_INFORMATION = &H200
Private Const PROCESS_SUSPEND_RESUME = &H800
Private Const PROCESS_TERMINATE = &H1
Private Const PROCESS_VM_OPERATION = &H8
Private Const PROCESS_VM_READ = &H10
Private Const PROCESS_VM_WRITE = &H20
Private Const SYNCHRONIZE = &H100000
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Private Declare CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
Private Declare TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Public GetTargetProcessID(ByVal lpProcess As String) As Long '自定义函数用于根据进程名称获得进程pid
Dim dwRet As Long
Dim hSnapShot As Long
Dim pe32 As PROCESSENTRY32
hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0) '创建进程快照
pe32.dwSize = LenB(pe32)
ProcessFirst hSnapShot, pe32
Do
If InStr(pe32.szExeFile, lpProcess) > 0 Then 'pe32.szExeFile获取进程名称
dwRet = pe32.th32ProcessID 'pe32.th32ProcessID 获取进程pid
Exit Do
Else
pe32.szExeFile = String(MAX_PATH, 0)
End If
Loop While (ProcessNext(hSnapShot, pe32))
CloseHandle (hSnapShot) '关闭进程快照句柄
GetTargetProcessID = dwRet
End
Public CloseTargetProcess(ByVal dwProcessID As Long) As Boolean '自定义函数,根据进程pid结束该进程
Dim hProcess As Long
Dim lpExitCode As Long
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, dwProcessID) '获取制定pid进程的进程句柄
If hProcess = 0 Then
CloseTargetProcess = False
&nb
作者在 2007-04-19 05:31:00 发布以下内容