Code: Select all
; -----------------------------------------------------------------------------
; Constants required by process functions, etc...
; -----------------------------------------------------------------------------
#TH32CS_SNAPHEAPLIST = $1
#TH32CS_SNAPPROCESS = $2
#TH32CS_SNAPTHREAD = $4
#TH32CS_SNAPMODULE = $8
#TH32CS_SNAPALL = #TH32CS_SNAPHEAPLIST | #TH32CS_SNAPPROCESS | #TH32CS_SNAPTHREAD | #TH32CS_SNAPMODULE
#TH32CS_INHERIT = $80000000
#INVALID_HANDLE_VALUE = -1
#MAX_PATH = 260
#PROCESS32LIB = 9999
; -----------------------------------------------------------------------------
; GLOBAL PROCESS LIST! Used to retrieve information after getting process list...
; -----------------------------------------------------------------------------
Global NewList Process32.PROCESSENTRY32 ()
; This function sorts processes into TreeGadget list, so that child processes branch off from parents...
Procedure CompareProcs (gadget, currentid, currentname$)
Debug "Comparing " + currentname$ + " [" + Str (currentid) + "]"
ResetList (Process32 ())
While NextElement (Process32 ())
; Skip if checking against 'currentid', ie. same process...
If Process32 ()\th32ProcessID <> currentid
; Check currentid against this one...
againstid = Process32 ()\th32ProcessID
againstparent = Process32 ()\th32ParentProcessID
; If 'currentid' is parent of this process...
If currentid = againstparent
proc$ = PeekS (@Process32 ()\szExeFile)
Debug "--------> " + proc$ + " [" + Str (Process32 ()\th32ProcessID) + "]" + " / " + " [" + Str (Process32 ()\th32ParentProcessID) + "]"
AddGadgetItem (gadget, -1, PeekS (@Process32 ()\szExeFile))
current = ListIndex (Process32 ())
CompareProcs (gadget, againstid, proc$)
SelectElement (Process32 (), current)
DeleteElement (Process32 ())
EndIf
EndIf
Wend
EndProcedure
; Add processes to Process32 () list...
If OpenLibrary (#PROCESS32LIB, "kernel32.dll")
snap = CallFunction (#PROCESS32LIB, "CreateToolhelp32Snapshot", #TH32CS_SNAPPROCESS, 0)
If snap
Define.PROCESSENTRY32 Proc32
Proc32\dwSize = SizeOf (PROCESSENTRY32)
If CallFunction (#PROCESS32LIB, "Process32First", snap, @Proc32)
AddElement (Process32 ())
CopyMemory (@Proc32, @Process32 (), SizeOf (PROCESSENTRY32))
While CallFunction (#PROCESS32LIB, "Process32Next", snap, @Proc32)
AddElement (Process32 ())
CopyMemory (@Proc32, @Process32 (), SizeOf (PROCESSENTRY32))
Wend
EndIf
CloseHandle_ (snap)
EndIf
CloseLibrary (#PROCESS32LIB)
EndIf
; Window hook, used to resize/redraw TreeGadget when window is resized...
Procedure WinHook (WindowID, Message, wParam, lParam)
If Message = #WM_PAINT
ResizeGadget (0, 0, 0, WindowWidth (0), WindowHeight (0))
RedrawWindow_ (GadgetID (0), #Null, #Null, #RDW_INVALIDATE)
EndIf
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
; GUI...
OpenWindow (0, 320, 200, 320, 400, "Process list...", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
TreeGadget (0, 0, 0, WindowWidth (0), WindowHeight (0))
SetWindowCallback (@WinHook ())
; Add processes to TreeGadget...
ResetList (Process32 ())
While NextElement (Process32 ())
AddGadgetItem (0, -1, PeekS (@Process32 ()\szExeFile))
current = ListIndex (Process32 ())
CompareProcs (0, Process32 ()\th32ProcessID, PeekS (@Process32 ()\szExeFile))
SelectElement (Process32 (), current)
Wend
Repeat
Until WaitWindowEvent () = #PB_Event_CloseWindow
End