Difference between revisions of "Process List and Locate VB6"

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search
(New page: == 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 = 2...)
 
 
(One intermediate revision by one user not shown)
Line 1: Line 1:
 
== Show a list of Windows Processes ==
 
== Show a list of Windows Processes ==
  
=== Example 1: ===
+
=== Example 1: Get a Windows Process List ===
 
Add the following to a module:
 
Add the following to a module:
  
Line 57: Line 57:
 
   Call CloseHandle(hSnapShot)
 
   Call CloseHandle(hSnapShot)
 
  End Sub
 
  End Sub
 
  
 
 
 
 
 +
 +
=== Example 2: Get Process ID of calc.exe ===
 +
 +
This was taken from a working program.  You can make this into a function.
 +
 +
<nowiki>  Dim hSnapShot As Long</nowiki>
 +
<nowiki>  Dim uProcess As PROCESSENTRY32</nowiki>
 +
<nowiki>  Dim success As Long</nowiki>
 +
<nowiki>  </nowiki>
 +
<nowiki>  Dim strLine As String, cnt As Integer, cntMax As Integer, strProc(99, 2)</nowiki>
 +
<nowiki></nowiki>
 +
<nowiki>  hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)</nowiki>
 +
<nowiki></nowiki>
 +
<nowiki>  If hSnapShot = -1 Then Exit Sub</nowiki>
 +
<nowiki></nowiki>
 +
<nowiki>  uProcess.dwSize = Len(uProcess)</nowiki>
 +
<nowiki>  success = ProcessFirst(hSnapShot, uProcess)</nowiki>
 +
<nowiki></nowiki>
 +
<nowiki>  If success = 1 Then</nowiki>
 +
<nowiki>    </nowiki>
 +
<nowiki>    Do</nowiki>
 +
<nowiki>      If (uProcess.th32ProcessID) > 0 Then</nowiki>
 +
<nowiki>        cnt = cnt + 1</nowiki>
 +
<nowiki>        strProc(cnt, 0) = uProcess.th32ProcessID</nowiki>
 +
<nowiki>        strProc(cnt, 1) = uProcess.szExeFile</nowiki>
 +
<nowiki>        cntMax = cnt</nowiki>
 +
<nowiki>      End If</nowiki>
 +
<nowiki>    Loop While ProcessNext(hSnapShot, uProcess)</nowiki>
 +
<nowiki>    </nowiki>
 +
<nowiki>  For cnt = 1 To cntMax</nowiki>
 +
<nowiki>'  Locate our process, calc.exe</nowiki>
 +
<nowiki>'  Debug.Print Trim(LCase(strProc(cnt, 1)))</nowiki>
 +
<nowiki>'  Debug.Print InStr(1, Trim(LCase(strProc(cnt, 1))), "calc.exe")</nowiki>
 +
<nowiki>    If InStr(1, Trim(LCase(strProc(cnt, 1))), "calc.exe") > 0 Then intTargetPid = strProc(cnt, 0)</nowiki>
 +
<nowiki>  Next cnt</nowiki>
 +
<nowiki></nowiki>
 +
<nowiki>  End If</nowiki>
 +
<nowiki>  Call CloseHandle(hSnapShot)</nowiki>
 +
<nowiki></nowiki>
 +
  
 
&nbsp;
 
&nbsp;

Latest revision as of 21:18, 19 August 2007

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)