Classic VB - How can I retrieve a list of running processes?-VBForums
Results 1 to 2 of 2

Thread: Classic VB - How can I retrieve a list of running processes?

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Philippines
    Posts
    10,590

    Arrow Classic VB - How can I retrieve a list of running processes?

    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.
    Code:
    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.
    Code:
    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.
    Code:
    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:
    Code:
    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.
    Attached Files Attached Files
    Last edited by si_the_geek; Dec 13th, 2008 at 07:48 AM. Reason: minor formatting/wording changes

  2. #2

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Philippines
    Posts
    10,590

    Re: Classic VB - How can I retrieve a list of running processes?

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

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.