Option Explicit
'on win2k you need the following:
Private Const PROCESS_TERMINATE = &H1
'On nt 9X the following could be enough:
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
'in any case, let us use both...
Private Const MAX_PATH As Integer = 260
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 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 TerminateProcess Lib "kernel32" (ByVal ApphProcess 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
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Function KillApp(myName As String) As Boolean
Dim uProcess As PROCESSENTRY32
Dim rProcessFound As Long
Dim hSnapShot As Long
Dim szExename As String
Dim exitCode As Long
Dim myProcess As Long
Dim iFound As Integer
On Error GoTo ErrHandler
Const TH32CS_SNAPPROCESS As Long = 2&
uProcess.dwSize = Len(uProcess)
hSnapShot = CreateToolHelpSnapshot(TH32CS_SNAPPROCESS, 0&)
rProcessFound = ProcessFirst(hSnapShot, uProcess)
Do While rProcessFound
iFound = InStr(1, uProcess.szExeFile, Chr(0)) - 1
If iFound > 0 Then
szExename = LCase$(Left$(uProcess.szExeFile, iFound))
If Right$(szExename, Len(myName)) = LCase$(myName) Then
myProcess = OpenProcess(PROCESS_ALL_ACCESS Or PROCESS_TERMINATE, False, uProcess.th32ProcessID)
KillApp = TerminateProcess(myProcess, exitCode)
Call CloseHandle(myProcess)
'if you have only one, exit here:
'Exit Function
'if you think you may have more than one,
'let this go on
End If
rProcessFound = ProcessNext(hSnapShot, uProcess)
End If
Loop
Call CloseHandle(hSnapShot)
Exit Function
ErrHandler:
MsgBox Err.Description
End Function