List of running processes...

Share your advanced PureBasic knowledge/code with the community.
Hi-Toro
Enthusiast
Enthusiast
Posts: 270
Joined: Sat Apr 26, 2003 3:23 pm

List of running processes...

Post by Hi-Toro »

Code updated for 5.20+

FIXED: This post and next one -- now works on '98!

Code: Select all

; MAKE SURE DEBUGGER IS ON!!!

#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
#PROCESS32LIB = 9999

; NOTE: I've chosen to add processes to this list so that it can be played with as necessary...

NewList Process32.PROCESSENTRY32 ()

; 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

; List processes...

ResetList (Process32 ())

While NextElement (Process32 ())
    Debug PeekS (@Process32 ()\szExeFile)
Wend
Last edited by Hi-Toro on Sun Nov 30, 2003 1:00 am, edited 3 times in total.
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Hi-Toro
Enthusiast
Enthusiast
Posts: 270
Joined: Sat Apr 26, 2003 3:23 pm

... and even if no-one cares, there's more!

Post by Hi-Toro »

Code updated for 5.20+

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
Last edited by Hi-Toro on Sun Nov 30, 2003 12:57 am, edited 1 time in total.
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

... and even if no-one cares, there's more!
it is very usefull for my next project, thx for this snippet .

forgotten to say, cannot run under NT4 and Win95 (because of missing 'CreateToolhelp32Snapshot') but who cares ?
SPAMINATOR NR.1
Hi-Toro
Enthusiast
Enthusiast
Posts: 270
Joined: Sat Apr 26, 2003 3:23 pm

Windows 9x...

Post by Hi-Toro »

Hi Rings,

MSDN says it's in 95 and 98 at least...
Client: Included in Windows XP, Windows 2000 Professional, Windows Me, Windows 98, and Windows 95.
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

Hi-de-hi Hi-Toro!

Both versions only produce one line (kernel) here on w98 :cry:
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

who cares on the old OS's .
another reason to drop all those Win89 into trashcan !
SPAMINATOR NR.1
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

sorry rings, some of my stuff is running on 95, 98, NT, 2K, as well as XP

you should have seen me go ballistic with gfabasic when microsoft decided to remove (part of) thunking support in xp!!! all was fine under 2k, then xp came and all my long file names routines i could throw out of the window... oh yeah, i *could* rewrite them as xp appearently suddenly supported irq (!) calls again... in fact, i did that on a few cases, but thinking about it still p*ss*s me of, so please don't utterly ignore old systems :-)

but you are right for most newer, bigger stuff... it's just a choice: should your code run on older machines yes or no? i prefer it to run on 98 / 2k and xp, don't care much about nt
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

@Hi-Toro:

Damn ... good work. Thanks for sharing ! :!:
regards,
benny!
-
pe0ple ar3 str4nge!!!
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post by Seldon »

Thanks for sharing.

I only get 1 process running (under Win98) , that is : kernel32.dll
Is that normal ?
Hi-Toro
Enthusiast
Enthusiast
Posts: 270
Joined: Sat Apr 26, 2003 3:23 pm

Weird...

Post by Hi-Toro »

Weird. The MSDN site states this is supported on 9x, but I seem to vaguely remember reading that it might have to be called a different way, ie. by opening the library directly; however, that's what I'm doing! I might try it with the Windows equivalent of OpenLibrary/CallFunction, etc...
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Hi-Toro
Enthusiast
Enthusiast
Posts: 270
Joined: Sat Apr 26, 2003 3:23 pm

Gah!

Post by Hi-Toro »

Gah! It makes no sense!

http://support.microsoft.com/default.as ... bContent=1

This page claims these functions are *only* available in 95/98/Me, where it's apparently not working properly, yet this code works on 2000 and XP, for which they suggest you must use the PSAPI functions!

Those on 9x -- do you get only one process for the code in the very first post here?
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Berikco
Administrator
Administrator
Posts: 1328
Joined: Wed Apr 23, 2003 7:57 pm
Location: Belgium
Contact:

Post by Berikco »

Cool, thanks Hi-Toro

Works fine under XP an Windows 2003 Server

Forget windows 9.... bhaa, i forgot the name of that old OS :D
Berikco
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Re: Gah!

Post by Henrik »

Hi-Toro wrote: Those on 9x -- do you get only one process for the code in the very first post here?
Hi Hi-Toro

Yep only one. :(
kernel32.dll

Best regards
Henrik
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

Yep, only one.

Re "old" os: give me ONE good reason to upgrade and I'll give several against. How's it feel to be one of Billy Boy's slobbering masses? :lol:
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

i'll dig into the archive, see if i can find an old visualbasic source that worked on all os'es and listed all processes and tasks including 'hidden' ones... must have it somewhere...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply