Process32First Function

Declare Function Process32First Lib "kernel32.dll" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long

Platforms

Description & Usage

Process32First retrieves information about the first process in the process list contained in a system snapshot. This snapshot must have previously been generated using the CreateToolhelp32Snapshot function.

Return Value

If successful, the function returns a nonzero value. If an error occured, possibly if there is an empty or nonexistent process list in the snapshot, the function returns zero.

Visual Basic-Specific Issues

None.

Parameters

hSnapshot
A handle to the system snapshot created by the CreateToolhelp32Snapshot function. This snapshot contains the process list from which the first process is read.
lppe
Receives information about the first process in the snapshot. The dwSize member of this structure must be initialized before calling the function.

Example

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

See Also

Process32Next

Category

Tool Help

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/process32first.html