Find all our projects in development below.
All source code is GNU General Public License (GPL)
Browsing EnumProcessesMod.bas (3.47 KB)
Attribute VB_Name = "EnumProcessesMod"
Option Explicit
Private Const MAX_PATH = 260
Private Const TH32CS_SNAPPROCESS = &H2
Private Const PROCESS_TERMINATE = &H1
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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId 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
Public Function GetProcessFilename(hProcess As Long)
Dim hSnapshot As Long
Dim uProcess As PROCESSENTRY32
Dim rProcess As Long
Dim i As Integer
Dim szExename As String
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
If hSnapshot Then
uProcess.dwSize = Len(uProcess)
rProcess = ProcessFirst(hSnapshot, uProcess)
Do While rProcess
If uProcess.th32ProcessID = hProcess Then
szExename = uProcess.szExeFile
If InStr(szExename, Chr(0)) Then szExename = Left(szExename, InStr(szExename, Chr(0)) - 1)
GetProcessFilename = szExename
Exit Function
End If
rProcess = ProcessNext(hSnapshot, uProcess)
Loop
CloseHandle hSnapshot
End If
GetProcessFilename = ""
End Function
Public Function EndProcess(ProcessID As Long) As Long
Dim hProcess As Long
Dim lExitCode As Long
hProcess = OpenProcess(PROCESS_TERMINATE, 0&, ProcessID)
If hProcess Then
If GetExitCodeProcess(hProcess, lExitCode) Then
EndProcess = TerminateProcess(hProcess, lExitCode)
End If
End If
End Function
Public Sub GetProcesses(ProcessExeList() As String, ProcessThreadList() As Long)
Dim uProcess As PROCESSENTRY32
Dim rProcess As Long
Dim hSnapshot As Long
Dim i As Long
ReDim ProcessExeList(1 To 1)
ReDim ProcessThreadList(1 To 1)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
If hSnapshot Then
uProcess.dwSize = Len(uProcess)
rProcess = ProcessFirst(hSnapshot, uProcess)
Do While rProcess
i = i + 1
ReDim Preserve ProcessExeList(1 To i)
ReDim Preserve ProcessThreadList(1 To i)
ProcessExeList(UBound(ProcessExeList)) = IIf(InStr(uProcess.szExeFile, Chr(0)), _
Left(uProcess.szExeFile, InStr(uProcess.szExeFile, Chr(0)) - 1), _
uProcess.szExeFile)
ProcessThreadList(UBound(ProcessThreadList)) = uProcess.th32ProcessID
rProcess = ProcessNext(hSnapshot, uProcess)
Loop
CloseHandle hSnapshot
End If
End Sub