# Visual Basic > Visual Basic FAQs >  Classic VB - How can I retrieve a list of running processes?

## dee-u

To retrieve the processes running in one's computer we need to use some API functions. These are the main APIs involved:
The CreateToolhelp32Snapshot API retrieves a snapshot of what is running on a computer the moment it is called. With this snapshot, you can then examine what things were running when the snapshot was made.


```
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32.dll" ( _
               ByVal dwFlags As Long, _
               ByVal th32ProcessID As Long) As Long
```

Process32First retrieves information about the first process in the process list contained in a system snapshot which is taken by our first API listed above.


```
Public Declare Function Process32First 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. After an initial call to Process32First, calling this API repeatedly until its return value becomes 0 will allow your program to read the entire process list. When its return value is 0 it means an error occurred, most likely there are no more unread processes in the list.


```
Public Declare Function Process32Next Lib "kernel32.dll" ( _
               ByVal hSnapshot As Long, _
               lppe As PROCESSENTRY32) As Long
```

You need a few more declarations too (such as the PROCESSENTRY32 type), as shown in the attachment.

This is a procedure that uses those API's to retrieve the running processes:


```
Public Sub ListProcesses()
    Dim processInfo As PROCESSENTRY32   ' information about a process in that list
    Dim hSnapshot   As Long             ' handle to the snapshot of the process list
    Dim success     As Long             ' success of having gotten info on another process
    Dim retval      As Long             ' generic return value
    Dim exeName     As String           ' filename of the process
    
    ' 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!"
    Else
        ' Loop for each process on the list.
        Do 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
            Debug.Print "Process: "; exeName
            
            ' Get information about the next process, if there is one.
            processInfo.dwSize = Len(processInfo)
            success = Process32Next(hSnapshot, processInfo)
        Loop
        
        ' Destroy the snapshot, now that we no longer need it.
        retval = CloseHandle(hSnapshot)
    End If
End Sub
```

The output of this is displayed in the Immediate window of VB, so will need to be changed to suit what you are doing - just change the Debug.Print line to what is apt for your situation.

----------


## dee-u

Using Windows Management Instrumentation (WMI) we could actually retrieve the running processes also. We could do it like this.



```
Dim Process As Object
For Each Process In GetObject("winmgmts:").ExecQuery("Select * from Win32_Process")
    Debug.Print Process.Caption
Next
```

----------

