'*************************************************************************
'**模 塊 名:ModFindProcess
'**說 明:進程相關(guān)操作
'**創(chuàng) 建 人:馬大哈 http://www.m5home.com/
'**日 期:2006年3月18日
'**日 期:2007年1月23日
'**描 述:改進了結(jié)束進程的條件,可以根據(jù)PID來結(jié)束
'**版 本:V1.3
'*************************************************************************
Option Explicit
Private Declare Function ProcessFirst Lib"kernel32" Alias"Process32First" (ByValhSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib"kernel32" Alias"Process32Next" (ByValhSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CreateToolhelpSnapshot Lib"kernel32" Alias"CreateToolhelp32Snapshot"(ByVal lFlags As Long, lProcessID As Long) As Long
Private Declare Function TerminateProcess Lib"kernel32" (ByVal hProcess AsLong, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib"kernel32" (ByValdwDesiredAccess As Long, ByVal bInheritHandle As Long, ByValdwProcessId As Long) As Long
Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const TH32CS_SNAPPROCESS As Long = 2&
Private Const PROCESS_TERMINATE = 1
Private Type PROCESSENTRY32
dwSize AsLong
cntUsage AsLong
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleIDAs Long
cntThreadsAs Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags AsLong
szexeFile AsString * 260
End Type
Private Type MyProcess
ExeName AsString
PID AsLong
End Type
Public Function CloseProcess(Optional ByVal ProName As String,Optional ByVal PID As Long) As Integer
'傳入進程名或PID,結(jié)束相應(yīng)進程
Dim tPID AsLong
Dim tPHwndAs Long
Dim ProArr()As String, PIDArr() As Long
Dim I AsLong
CallListProcess(ProArr, PIDArr)
For I = 1 ToUBound(ProArr)
If PIDArr(I) = PID Or ProArr(I) = ProNameThen '配對進程ID或進程名
Exit For
End If
Next I
If I >UBound(PIDArr) Then Exit Function
tPID =PIDArr(I)
tPHwnd =OpenProcess(PROCESS_TERMINATE, False, tPID)
Debug.PrinttPHwnd
If tPHwndThen
CloseProcess = TerminateProcess(tPHwnd, 0)
End If
End Function
Public Function FindProcess(ByVal ProName As String, Optional ByRefPID As Long) As Boolean
'傳入進程名,如果進程存在,在PID里返回進程ID,函數(shù)返回True,否則返回Flase
'ProName:指定進程名
'PID:如果進程名存在,返回其PID
'返回值:進程名存在返回TRUE,否則返回FALSE
Dim ProArr()As String, PIDArr() As Long
Dim I AsLong
CallListProcess(ProArr, PIDArr)
For I = 1 ToUBound(ProArr)
If ProArr(I) = ProName Then
PID = PIDArr(I)
FindProcess = True
Exit For
End If
Next I
End Function
Public Function ListProcess(ByRef ProExeName() As String, ByRefProPid() As Long)
'列出進程以及相應(yīng)PID
'ProExeName(): 進程名
'ProPid():相應(yīng)的PID
DimMyProcess As PROCESSENTRY32
DimmySnapshot As Long
DimProData() As MyProcess
Dim I AsLong
ReDimProData(0)
MyProcess.dwSize = Len(MyProcess)