Declare Function Process32Next Lib "kernel32.dll" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Process32Next retrieves information about the next unread process in the process list contained in a system snapshot. This snapshot must have previously been generated using the CreateToolhelp32Snapshot function. Calling this function repeatedly, after an initial call to Process32First, will allow your program to read the entire process list.
If successful, the function returns a nonzero value. If an error occured, most likely if there are no more unread processes in the list, the function returns zero.
None.
Print a list of all the processes currently running on the computer when the user clicks button Command1. To do this, a snapshot of the running process list is taken, and then each process in it is analyzed. The filename of the process and the number of threads owned by it is then displayed. To use this example, place a command button named Command1 on a form window.
' This code is licensed according to the terms and conditions listed here.
' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32.dll" (ByVal dwFlags As Long, ByVal _
th32ProcessID As Long) As Long
Public Const TH32CS_SNAPPROCESS = &H2
Public 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
Public Declare Function Process32First Lib "kernel32.dll" (ByVal hSnapshot As Long, _
lppe As PROCESSENTRY32) As Long
Public Declare Function Process32Next Lib "kernel32.dll" (ByVal hSnapshot As Long, _
lppe As PROCESSENTRY32) As Long
Public Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
' *** Place the following code inside a form window. ***
Private Sub Command1_Click()
Dim hSnapshot As Long ' handle to the snapshot of the process list
Dim processInfo As PROCESSENTRY32 ' information about a process in that list
Dim success As Long ' success of having gotten info on another process
Dim exeName As String ' filename of the process
Dim retval As Long ' generic return value
' First, make a snapshot of the current process list.
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
' Get information about the first process in the list.
processInfo.dwSize = Len(processInfo)
success = Process32First(hSnapshot, processInfo)
' Make sure a handle was returned.
If hSnapshot = -1 Then
Debug.Print "Unable to take snapshot of process list!"
Exit Sub
End If
' Loop for each process on the list.
While success <> 0
' Extract the filename of the process (i.e., remove the empty space)
exeName = Left(processInfo.szExeFile, InStr(processInfo.szExeFile, vbNullChar) - 1)
' Display the process name and the number of threads it owns.
Debug.Print "Process: "; exeName
Debug.Print " - Number of threads:"; processInfo.cntThreads
' Get information about the next process, if there is one.
processInfo.dwSize = Len(processInfo)
success = Process32Next(hSnapshot, processInfo)
Wend
' Destroy the snapshot, now that we no longer need it.
retval = CloseHandle(hSnapshot)
End Sub
Back to the Function list.
Back to the Reference section.
Last Modified: September 24, 2000
This page is copyright © 2000 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/p/process32next.html