Process List and Locate VB6

From Free Knowledge Base- The DUCK Project: information for everyone
Revision as of 17:49, 19 August 2007 by Admin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Show a list of Windows Processes

Example 1:

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