Declare Function CreateToolhelp32Snapshot Lib "kernel32.dll" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
CreateToolhelp32Snapshot creates a snapshot of what is running on the computer the moment the function is called. Depending on the flags specified, this snapshot can include running processes and/or threads, among other things. With this snapshot, you can then examine what things were running when the snapshot was made. After your program no longer needs the snapshot, destroy it using CloseHandle.
If successful, the function returns a handle to the snapshot that was made. If an error occured, the functions returns -1 (use GetLastError to get the error code).
None.
Const TH32CS_INHERIT = &H80000000
Const TH32CS_SNAPALL = &HF
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
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/c/createtoolhelp32snapshot.html