[FONT=Fixedsys]Option Explicit[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]Public PermittedProcesses As String
Public process() As Variant
Public iLimit As Integer
Public KillCount As Integer[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]Private Const TH32CS_SNAPPROCESS As Long = 2&
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Const MAX_PATH As Integer = 260[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]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[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)[/FONT]
[FONT=Fixedsys]Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long[/FONT]
[FONT=Fixedsys]Private Declare Function ProcessFirst Lib "kernel32.dll" _
Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long[/FONT]
[FONT=Fixedsys]Private Declare Function ProcessNext Lib "kernel32.dll" _
Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long[/FONT]
[FONT=Fixedsys]Private Declare Function CreateToolhelpSnapshot Lib "kernel32.dll" _
Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long[/FONT]
[FONT=Fixedsys]Private Declare Function TerminateProcess Lib "kernel32.dll" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]Public Sub InitialisePermittedProcesses(argProcessImageName As String)[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys] Dim hSnapshot As Long
Dim procEntry As PROCESSENTRY32
Dim result As Long
Dim process(3) As Variant
Dim iPtr As Integer
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0)
If hSnapshot = -1 Then Err.Raise 999, , "Unable to get process snapshot"
procEntry.dwSize = Len(procEntry)
PermittedProcesses = ""
result = ProcessFirst(hSnapshot, procEntry)
Do While result
process(0) = procEntry.th32ProcessID
process(1) = Left$(procEntry.szexeFile, InStr(procEntry.szexeFile & vbNullChar, vbNullChar) - 1)
process(2) = procEntry.cntThreads
process(3) = procEntry.th32ParentProcessID
If LCase(process(1)) = LCase(argProcessImageName) Then
PermittedProcesses = PermittedProcesses & "/" & CStr(process(0)) & "/"
End If
result = ProcessNext(hSnapshot, procEntry)
iPtr = iPtr + 1
Loop
CloseHandle hSnapshot
End Sub[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]Public Sub KillNonPermittedProcesses(argProcessImageName As String)[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys] Dim hSnapshot As Long
Dim procEntry As PROCESSENTRY32
Dim result As Long
Dim process(3) As Variant
Dim iPtr As Integer
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0)
If hSnapshot = -1 Then Err.Raise 999, , "Unable to get process snapshot"
procEntry.dwSize = Len(procEntry)
result = ProcessFirst(hSnapshot, procEntry)
Do While result
process(0) = procEntry.th32ProcessID
process(1) = Left$(procEntry.szexeFile, InStr(procEntry.szexeFile & vbNullChar, vbNullChar) - 1)
process(2) = procEntry.cntThreads
process(3) = procEntry.th32ParentProcessID
If LCase(process(1)) = LCase(argProcessImageName) Then
If InStr(PermittedProcesses, "/" & CStr(process(0)) & "/") = 0 Then
Call EndProcess(procEntry.th32ProcessID)
End If
End If
result = ProcessNext(hSnapshot, procEntry)
iPtr = iPtr + 1
Loop
CloseHandle hSnapshot
End Sub[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]Public Sub EndProcess(argProcess As Long)[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys] 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 AppKill As Boolean
Dim i As Integer
Dim WinDirEnv As String
uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
RProcessFound = ProcessFirst(hSnapshot, uProcess)
Do
i = InStr(1, uProcess.szexeFile, Chr(0))
SzExename = LCase$(Left$(uProcess.szexeFile, i - 1))
WinDirEnv = Environ("Windir") + "\"
WinDirEnv = LCase$(WinDirEnv)
' check for a process specified by process id and kill it
If argProcess = uProcess.th32ProcessID Then
MyProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
AppKill = TerminateProcess(MyProcess, ExitCode)
Call CloseHandle(MyProcess)
End If
RProcessFound = ProcessNext(hSnapshot, uProcess)
Loop While RProcessFound
Call CloseHandle(hSnapshot)
End Sub[/FONT]