Page 1 of 1

Get Windows Process List

Posted: Wed Oct 02, 2024 9:04 pm
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.

Re: Get Windows Process List

Posted: Thu Oct 03, 2024 9:32 am
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))

Re: Get Windows Process List

Posted: Thu Oct 03, 2024 10:15 am
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()

Re: Get Windows Process List

Posted: Thu Oct 03, 2024 11:59 am
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

Re: Get Windows Process List

Posted: Thu Oct 03, 2024 12:27 pm
by BarryG
I do own PB, but didn't know I could do it this way. Thanks!

Re: Get Windows Process List

Posted: Thu Oct 03, 2024 4:19 pm
by infratec
Enable API functions for code completion, then you will see the available functions :wink:

Re: Get Windows Process List

Posted: Thu Oct 03, 2024 6:29 pm
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.

Re: Get Windows Process List

Posted: Thu Oct 03, 2024 6:49 pm
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. :?

Re: Get Windows Process List

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

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

at 'API Functions'

Re: Get Windows Process List

Posted: Thu Oct 03, 2024 7:36 pm
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.

Re: Get Windows Process List

Posted: Thu Oct 03, 2024 7:38 pm
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.

Re: Get Windows Process List

Posted: Thu Oct 03, 2024 7:58 pm
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

Re: Get Windows Process List

Posted: Mon Oct 07, 2024 6:53 pm
by davidow
@infratec,
Nice code, thank you.
I shall find this very useful.

Re: Get Windows Process List

Posted: Tue Feb 25, 2025 1:30 am
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