Visual Basic System Services
CreateToolhelp32Snapshot: List Running Processes
     
Posted:   Monday March 3, 1997
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   Windows 95, Windows 98, Windows 2000, , Windows XP and 32-bit VB
Author:   VBnet - Randy Birch
     
 Prerequisites
Windows 95, Windows 98 or Windows 2000, Windows XP. CreateToolhelp32Snapshot is not available on NT.

Start a new project, and to the form add a listbox (List1), and a command button (Command1).
 BAS Module Code
Place the following code into the general declarations area of a bas module:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Const TH32CS_SNAPPROCESS As Long = 2&
Public Const MAX_PATH As Long = 260

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 * MAX_PATH
End Type        
    

Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" _
   (ByVal lFlags As Long, ByVal lProcessID As Long) As Long

Public Declare Function ProcessFirst Lib "kernel32" _
    Alias "Process32First" _
   (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

Public Declare Function ProcessNext Lib "kernel32" _
    Alias "Process32Next" _
   (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

Public Declare Sub CloseHandle Lib "kernel32" _
   (ByVal hPass As Long)
 Form Code
To a form containing a list box (List1) and a command button (Command1), add the following code:

Option Explicit
Private Sub Command1_Click()

    Dim hSnapShot As Long
    Dim uProcess As PROCESSENTRY32
    Dim success As Long

    hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)

    If hSnapShot = -1 Then Exit Sub

    uProcess.dwSize = Len(uProcess)
    success = ProcessFirst(hSnapShot, uProcess)

    If success = 1 then
    
        Do 
            List1.AddItem uProcess.szExeFile
        Loop While ProcessNext(hSnapShot, uProcess)
            
    End If

    Call CloseHandle(hSnapShot)

End Sub
 Comments
Run the project. Clicking the command button will load the listbox with the names of the currently-executing applications. Note that on XP systems only the exe name is returned, not the full path.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter