Get Windows Process List

Share your advanced PureBasic knowledge/code with the community.
Randy Walker
Addict
Addict
Posts: 989
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Get Windows Process List

Post by Randy Walker »

I was only interested in capturing the process name itself, but you can easily tweak the code to capture full details if needed (remove the stringfield part). Probably an API means to get the process names but I could not figure it out so I resorted to a cmd.exe "tasklist" command to get my list:

Code: Select all

Global Output$
Procedure ProcessList()
ProcessList = RunProgram("tasklist","","",#PB_Program_Hide | #PB_Program_Open | #PB_Program_Read)
  If ProcessList
    While ProgramRunning(ProcessList)
      If AvailableProgramOutput(ProcessList)
        Catch$ = ReadProgramString(ProcessList)
        s$ = StringField(Catch$,1," ")
        Output$ + s$ + Chr(13) ; replace s$ with Catch$ for full listing
      EndIf
    Wend
    Output$ + Chr(13) + Chr(13)
    ;Output$ + "Exitcode: " + Str(ProgramExitCode(ProcessList))
    CloseProgram(ProcessList) ; Close the connection to the program
  EndIf
  EndProcedure
ProcessList()
Debug Output$
[edit] 10-02-24 -- I changed the stringfield separator from 7 spaces down to just one space and now gets cleaner results.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get Windows Process List

Post by infratec »

Without calling an external program:

viewtopic.php?p=562611#p562611

serach for

Code: Select all

Procedure ProcessList (Array a$(1),Array b$(1))
BarryG
Addict
Addict
Posts: 4128
Joined: Thu Apr 18, 2019 8:17 am

Re: Get Windows Process List

Post by BarryG »

Smallest and fastest:

Code: Select all

Procedure GetProcessList()
  lib=OpenLibrary(#PB_Any,"kernel32.dll")
  If lib
    snap=CallFunction(lib,"CreateToolhelp32Snapshot",#TH32CS_SNAPPROCESS,0)
    If snap<>0 And snap<>#INVALID_HANDLE_VALUE
      Proc32.PROCESSENTRY32\dwSize=SizeOf(PROCESSENTRY32)
      While CallFunction(lib,"Process32NextW",snap,@Proc32)
        Debug PeekS(@Proc32\szExeFile)
      Wend
    EndIf
    CloseLibrary(lib)
  EndIf
EndProcedure

GetProcessList()
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get Windows Process List

Post by infratec »

No need to open the library if you bought PB :wink:

Code: Select all

Structure ProcessList_Structure
  ID.i
  Name$
EndStructure


Procedure.i GetProcessList(List ProcessList.ProcessList_Structure())
  
  Protected snap.i, Proc32.PROCESSENTRY32
  
  
  ClearList(ProcessList())
  snap = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0)
  If snap And snap <> #INVALID_HANDLE_VALUE
    Proc32\dwSize = SizeOf(PROCESSENTRY32)
    While Process32Next_(snap, @Proc32)
      AddElement(ProcessList())
      ProcessList()\ID = Proc32\th32ProcessID
      ProcessList()\Name$ = PeekS(@Proc32\szExeFile)
    Wend
  EndIf
  
  ProcedureReturn ListSize(ProcessList())
  
EndProcedure


NewList ProcessList.ProcessList_Structure()

If GetProcessList(ProcessList())
  ForEach ProcessList()
    Debug RSet(Str(ProcessList()\ID), 6) + " " + ProcessList()\Name$
  Next
EndIf
BarryG
Addict
Addict
Posts: 4128
Joined: Thu Apr 18, 2019 8:17 am

Re: Get Windows Process List

Post by BarryG »

I do own PB, but didn't know I could do it this way. Thanks!
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get Windows Process List

Post by infratec »

Enable API functions for code completion, then you will see the available functions :wink:
Randy Walker
Addict
Addict
Posts: 989
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Get Windows Process List

Post by Randy Walker »

infratec wrote: Thu Oct 03, 2024 4:19 pm Enable API functions for code completion, then you will see the available functions :wink:
Can you clarify that comment? Where are API functions enabled for code completion?

@everyone else -- Thanks for the contributions. Now we (and others) have many choices.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 989
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Get Windows Process List

Post by Randy Walker »

infratec wrote: Thu Oct 03, 2024 9:32 am Without calling an external program:

viewtopic.php?p=562611#p562611

serach for

Code: Select all

Procedure ProcessList (Array a$(1),Array b$(1))
Yeah, uh. I looked at that a couple times and still can't figure out how to get a list out of that procedure. I think it goes into buffer but I don't know how to get it out of there. :?
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get Windows Process List

Post by infratec »

Look in

'File'->'Preferences...'->'Editor'->'AutoComplete'->'Displayed Items'

at 'API Functions'
Randy Walker
Addict
Addict
Posts: 989
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Get Windows Process List

Post by Randy Walker »

infratec wrote: Thu Oct 03, 2024 7:07 pm Look in

'File'->'Preferences...'->'Editor'->'AutoComplete'->'Displayed Items'

at 'API Functions'
Ok.yeah. I see that now. Looks like a default setting. Mine is already checked.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 989
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Get Windows Process List

Post by Randy Walker »

BTW, looks like another option might be in here somewhere:
viewtopic.php?p=616362&hilit=tasklist#p616362
But its all way over my head.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: Get Windows Process List

Post by AZJIO »

Use this tool to search. You will get a lot of solutions to the ProcessList() function.
The link to my task has nothing to do with the list of processes

video - The video shows how it works
davidow
New User
New User
Posts: 5
Joined: Mon Mar 20, 2023 7:34 am
Location: Uttoxeter, UK

Re: Get Windows Process List

Post by davidow »

@infratec,
Nice code, thank you.
I shall find this very useful.
Randy Walker
Addict
Addict
Posts: 989
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Get Windows Process List

Post by Randy Walker »

Randy Walker wrote: Wed Oct 02, 2024 9:04 pm I was only interested in capturing the process name itself,
I found a cleaner way to get there using "Get-process" cmdlet", except in my code sample here I am using the shorthand alias name "ps":

Code: Select all

param$ ="ps | sort -Unique | Select-Object ProcessName"
Exe = RunProgram("powershell.exe",param$,"",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read|#PB_Program_Error)
Debug "Please wait for info to be collected"
If Exe
    While ProgramRunning(Exe)
      If AvailableProgramOutput(Exe)
        Output$=ReadProgramString(Exe)
        Debug Output$
      EndIf
    Wend
EndIf
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Post Reply