Private Const PROCESS_TERMINATE = &H1Private 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_SNAPmodulePrivate Type PROCESSENTRY 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 * 260 End TypePublic Enum PROCESSACTION paCheckExist = 0 paClose = 1 End EnumPrivate Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY) As Long Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function TerminateProcess Lib "kernel32.dll" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long'自定义函数GetProc 'strProcessName 为进程名,例如:Explorer.exe 'Action 是一个我自己定义的类型,paClose代表关闭进程;paCheckExist代表检测进程是否存在 Public Function GetProc(ByVal strProcessName As String, Optional ByVal Action As PROCESSACTION = paClose) As Long Dim hProc As Long, Proc As PROCESSENTRY Dim hSnap As Long, ExeName As String Dim lngHwndProcess As Long hSnap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0&) With Proc .dwSize = Len(Proc) hProc = ProcessFirst(hSnap, Proc) Do While hProc ExeName = Left(.szExeFile, InStr(1, .szExeFile, Chr(0)) - 1) If LCase(ExeName) = LCase(strProcessName) Then If Action = paClose Then lngHwndProcess = OpenProcess(PROCESS_TERMINATE, True, .th32ProcessID) Call TerminateProcess(lngHwndProcess, 0) Call CloseHandle(lngHwndProcess) Else GetProc = True End If Exit Do End If .szExeFile = String(260, Chr$(0)) hProc = ProcessNext(hSnap, Proc) Loop End With Call CloseHandle(hSnap) End Function当你需要关闭一个进程的时候,你可以这样调用:GetProc("Explorer.exe") 当你需要检测一个程序是否在运行的时候,你可以这样调用:GetProc("Explorer.exe", paCheckExist)
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_SNAPmodulePrivate Type PROCESSENTRY
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 * 260
End TypePublic Enum PROCESSACTION
paCheckExist = 0
paClose = 1
End EnumPrivate Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32.dll" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long'自定义函数GetProc
'strProcessName 为进程名,例如:Explorer.exe
'Action 是一个我自己定义的类型,paClose代表关闭进程;paCheckExist代表检测进程是否存在
Public Function GetProc(ByVal strProcessName As String, Optional ByVal Action As PROCESSACTION = paClose) As Long
Dim hProc As Long, Proc As PROCESSENTRY
Dim hSnap As Long, ExeName As String
Dim lngHwndProcess As Long
hSnap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0&)
With Proc
.dwSize = Len(Proc)
hProc = ProcessFirst(hSnap, Proc)
Do While hProc
ExeName = Left(.szExeFile, InStr(1, .szExeFile, Chr(0)) - 1)
If LCase(ExeName) = LCase(strProcessName) Then
If Action = paClose Then
lngHwndProcess = OpenProcess(PROCESS_TERMINATE, True, .th32ProcessID)
Call TerminateProcess(lngHwndProcess, 0)
Call CloseHandle(lngHwndProcess)
Else
GetProc = True
End If
Exit Do
End If
.szExeFile = String(260, Chr$(0))
hProc = ProcessNext(hSnap, Proc)
Loop
End With
Call CloseHandle(hSnap)
End Function当你需要关闭一个进程的时候,你可以这样调用:GetProc("Explorer.exe")
当你需要检测一个程序是否在运行的时候,你可以这样调用:GetProc("Explorer.exe", paCheckExist)