Is there anyone who knows how to retrieve a list of all programs running (and their HWND please) at the time the retrieve function/sub is called. I won't accept a DLL or OCX solution. Only plain API code, please.
Printable View
Is there anyone who knows how to retrieve a list of all programs running (and their HWND please) at the time the retrieve function/sub is called. I won't accept a DLL or OCX solution. Only plain API code, please.
All running programs with their HWNDS? I always thought, only windows have HWNDS..
Anyways, I can give you an example to get all running processes with their process handles.
All processes's handles and their path will be in RProcessesCode: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)
Public RProcesses() As PROCESSENTRY32
Public Const PROCESS_TERMINATE = &H1
Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Sub GetRunningProcesses()
Dim hSnapShot As Long
Dim uProcess As PROCESSENTRY32
Dim r As Long
Dim i&
ReDim RProcesses(0)
hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
If hSnapShot = 0 Then Exit Sub
uProcess.dwSize = Len(uProcess)
r = ProcessFirst(hSnapShot, uProcess)
i = 1
Do While r
ReDim Preserve RProcesses(UBound(RProcesses) + 1)
RProcesses(i) = uProcess
i = i + 1
r = ProcessNext(hSnapShot, uProcess)
Loop
Call CloseHandle(hSnapShot)
End Sub
Hope I could help you