Process List and Locate VB6
Jump to navigation
Jump to search
Show a list of Windows Processes
Example 1: Get a Windows Process List
Add the following to a module:
Option Explicit
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)
Then to the program:
Private Sub doShowProcessList()
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
txtOut.Text = txtOut.Text & vbCrLf & uProcess.szExeFile
Loop While ProcessNext(hSnapShot, uProcess)
End If
Call CloseHandle(hSnapShot)
End Sub
Example 2: Get Process ID of calc.exe
This was taken from a working program. You can make this into a function.
Dim hSnapShot As Long
Dim uProcess As PROCESSENTRY32
Dim success As Long
Dim strLine As String, cnt As Integer, cntMax As Integer, strProc(99, 2)
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If hSnapShot = -1 Then Exit Sub
uProcess.dwSize = Len(uProcess)
success = ProcessFirst(hSnapShot, uProcess)
If success = 1 Then
Do
If (uProcess.th32ProcessID) > 0 Then
cnt = cnt + 1
strProc(cnt, 0) = uProcess.th32ProcessID
strProc(cnt, 1) = uProcess.szExeFile
cntMax = cnt
End If
Loop While ProcessNext(hSnapShot, uProcess)
For cnt = 1 To cntMax
' Locate our process, calc.exe
' Debug.Print Trim(LCase(strProc(cnt, 1)))
' Debug.Print InStr(1, Trim(LCase(strProc(cnt, 1))), "calc.exe")
If InStr(1, Trim(LCase(strProc(cnt, 1))), "calc.exe") > 0 Then intTargetPid = strProc(cnt, 0)
Next cnt
End If
Call CloseHandle(hSnapShot)