|
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 * 260
End Type
Private 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 PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPheaplist = &H1
Private Const TH32CS_SNAPthread = &H4
Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread
Private Const PROCESS_TERMINATE As Long = &H1
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Function KillApp(exe As String) As Boolean
Dim i As Long
Dim Proc As PROCESSENTRY32
Dim Snap As Long
Dim exeName As String
Dim theLoop As Long
Dim lHand As Long, lProcessID As Long
Snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
Proc.dwSize = Len(Proc)
theLoop = ProcessFirst(Snap, Proc) '获取第一个进程,并得到其返回值
Do While theLoop <> 0 '当返回值非零时继续获取下一个进程
exeName = Proc.szExeFile '进程名
exeName = Left$(exeName, IIf(InStr(1, exeName, Chr$(0)) > 0, InStr(1, exeName, Chr$(0)) - 1, 0))
lProcessID = Proc.th32ProcessID '得到进程ID
Debug.Print exeName
If LCase(exeName) <> LCase(exe) Then
theLoop = ProcessNext(Snap, Proc)
Else
GoTo Kill
End If
Loop
KillApp = False
CloseHandle Snap '关闭进程“快照”句柄
Exit Function
Kill:
CloseHandle Snap '关闭进程“快照”句柄
lHand = OpenProcess(PROCESS_TERMINATE, True, CLng(lProcessID)) '获取进程句柄
TerminateProcess lHand, 0 '关闭进程
KillApp = True
End Function
Private Sub Command2_Click()
KillApp (Me.Text0)
End Sub |
|