0% found this document useful (0 votes)
38 views

Posted: 7 Jun 00: List All Applications / Processes Currently Running

This document provides two solutions for listing all applications and processes currently running on a system in Visual Basic. The first solution uses API calls to enumerate through windows and get the window text. The second solution uses the Toolhelp API to enumerate through processes and get the executable path and filename. Both solutions demonstrate populating a list box with the results.

Uploaded by

sherlinsam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Posted: 7 Jun 00: List All Applications / Processes Currently Running

This document provides two solutions for listing all applications and processes currently running on a system in Visual Basic. The first solution uses API calls to enumerate through windows and get the window text. The second solution uses the Toolhelp API to enumerate through processes and get the executable path and filename. Both solutions demonstrate populating a list box with the results.

Uploaded by

sherlinsam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

List all applications / processes currently running

faq222-61
Posted: 7 Jun 00
To get a list of all applications/proceses currently running, start a new project, add a command
button and a list box to Form1.
This first solution was found at vb-world.net (I can't take credit for it!)
'<><><><><><><><><><><><><><><><>
' Solution 1
'<><><><><><><><><><><><><><><><>
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As
Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA"
(ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd
As Long, ByVal lpString As String, ByVal cch As Long) As Long
Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2
Private Sub Command1_Click()
LoadTaskList
End Sub
Sub LoadTaskList()
Dim CurrWnd As Long
Dim Length As Long
Dim TaskName As String
Dim Parent As Long
List1.Clear
CurrWnd = GetWindow(Form1.hwnd, GW_HWNDFIRST)
While CurrWnd <> 0
Parent = GetParent(CurrWnd)
Length = GetWindowTextLength(CurrWnd)
TaskName = Space$(Length + 1)
Length = GetWindowText(CurrWnd, TaskName, Length + 1)
TaskName = Left$(TaskName, Len(TaskName) - 1)
If Length > 0 Then

If TaskName <> Me.Caption Then


List1.AddItem TaskName
End If
End If
CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
DoEvents
Wend
End Sub
'<><><><><><><><><><><><><><><><><><>
' Solution 2 - from user: Alt255 (Need to give credit to the actual provider!!!)
'<><><><><><><><><><><><><><><><><><>
Add this one to your tool box. It returns the physical path and filename of any EXE or DLL
running on a system.
You 'll need a command button and a list box:
Public Const TH32CS_SNAPPROCESS As Long = 2&
Public Const MAX_PATH As Integer = 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 CreateToolhelpSnapshot Lib "Kernel32" Alias _
"CreateToolhelp32Snapshot" (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)
Private Sub Command1_Click()
Dim hSnapShot As Long

Dim uProcess As PROCESSENTRY32


Dim r As Long
hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
If hSnapShot = 0 Then
Exit Sub
End If
uProcess.dwSize = Len(uProcess)
r = ProcessFirst(hSnapShot, uProcess)
Do While r
List1.AddItem uProcess.szExeFile
r = ProcessNext(hSnapShot, uProcess)
Loop
Call CloseHandle(hSnapShot)
End Sub

You might also like