Share your advanced PureBasic knowledge/code with the community.
Randy Walker
Addict
Posts: 989 Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA
Post
by Randy Walker » Wed Oct 02, 2024 9:04 pm
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.
BarryG
Addict
Posts: 4128 Joined: Thu Apr 18, 2019 8:17 am
Post
by BarryG » Thu Oct 03, 2024 10:15 am
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
Posts: 7582 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Thu Oct 03, 2024 11:59 am
No need to open the library if you bought PB
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
Posts: 4128 Joined: Thu Apr 18, 2019 8:17 am
Post
by BarryG » Thu Oct 03, 2024 12:27 pm
I do own PB, but didn't know I could do it this way. Thanks!
infratec
Always Here
Posts: 7582 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Thu Oct 03, 2024 4:19 pm
Enable API functions for code completion, then you will see the available functions
Randy Walker
Addict
Posts: 989 Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA
Post
by Randy Walker » Thu Oct 03, 2024 6:29 pm
infratec wrote: Thu Oct 03, 2024 4:19 pm
Enable API functions for code completion, then you will see the available functions
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
Posts: 989 Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA
Post
by Randy Walker » Thu Oct 03, 2024 6:49 pm
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
Posts: 7582 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Thu Oct 03, 2024 7:07 pm
Look in
'File'->'Preferences...'->'Editor'->'AutoComplete'->'Displayed Items'
at 'API Functions'
Randy Walker
Addict
Posts: 989 Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA
Post
by Randy Walker » Thu Oct 03, 2024 7:36 pm
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.
AZJIO
Addict
Posts: 2143 Joined: Sun May 14, 2017 1:48 am
Post
by AZJIO » Thu Oct 03, 2024 7:58 pm
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
Posts: 5 Joined: Mon Mar 20, 2023 7:34 am
Location: Uttoxeter, UK
Post
by davidow » Mon Oct 07, 2024 6:53 pm
@infratec,
Nice code, thank you.
I shall find this very useful.
Randy Walker
Addict
Posts: 989 Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA
Post
by Randy Walker » Tue Feb 25, 2025 1:30 am
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.