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 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 Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long 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 GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long 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
Public Function Testtxt(txt As String) As Boolean If Trim(txt) = "" Then Testtxt = False Else Testtxt = True End If End Function
Public Function GetTargetProcessID(ByVal lpProcess As String) As Long 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 dwRet = pe32.th32ProcessID Exit Do Else pe32.szExeFile = String(MAX_PATH, 0) End If Loop While (ProcessNext(hSnapShot, pe32)) CloseHandle (hSnapShot) GetTargetProcessID = dwRet End Function
Public Function CloseTargetProcess(ByVal dwProcessID As Long) As Boolean Dim hProcess As Long Dim lpExitCode As Long hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, dwProcessID) If hProcess = 0 Then CloseTargetProcess = False Exit Function End If If GetExitCodeProcess(hProcess, lpExitCode) = 0 Then CloseTargetProcess = False Exit Function End If If TerminateProcess(hProcess, lpExitCode) = 0 Then CloseTargetProcess = False Else CloseTargetProcess = True End If CloseHandle (hProcess) End Function
Dim ret As Long ret = GetTargetProcessID("kmplayer.exe") '注意进程名字母要区分大小写 If ret <> 0 Then ret = CloseTargetProcess(ret) End If
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 TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Const PROCESS_TERMINATE = 1 Dim appdisk$, ProID&, hProcess&, ExeNm$
Private Sub Form_Load() appdisk = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\") ExeNm = appdisk & "kmplayer.exe" If Dir(ExeNm) <> "" Then ProID = Shell(ExeNm, 3) End Sub
Private Sub Command1_Click() On Error Resume Next If Dir(ExeNm) <> "" Then hProcess = OpenProcess(PROCESS_TERMINATE, False, ProID) TerminateProcess hProcess, 1 CloseHandle hProcess End If End Sub