WinAPI : How to Enumerate all Windows Process and their Process IDs?

I have this clarion application that sometimes crashes and I want to see wether the exe is still present/running (after I killed it via cmd taskkill), based on its process id (before I launch a new session)

Process32First
Process32Next

2 Likes

You can find code to call Process32First / Process32Next in LibSrc in WinExt.Inc / .Clw for the WindowExtenderClass.


A quick simple way would be to RUN('TASKLIST.exe [parms] > TaskList.TXT',1) redirected to a text file. You can Pipe it to the Clipboard with RUN('TASKLIST.exe [parms] | CLIP',1) .


Examples checking if Clarion.exe is running in TABLE format.

C:\>TASKLIST /FI "IMAGENAME eq Clarion.exe" /FO TABLE

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
Clarion.exe                  21528 Console                    1    203,424 K
Clarion.exe                   4680 Console                    1    170,272 K
Clarion.exe                  13432 Console                    1    157,944 K

CSV is an option

C:\>TASKLIST /FI "IMAGENAME eq Clarion.exe" /FO CSV

"Image Name","PID","Session Name","Session#","Mem Usage"
"Clarion.exe","21528","Console","1","203,384 K"
"Clarion.exe","4680","Console","1","170,276 K"
"Clarion.exe","13432","Console","1","157,980 K"

If the EXE is not running you always get the same line no matter the /FO format:

C:\>TASKLIST /FI "IMAGENAME eq NotRunningExe.exe" /FO TABLE

INFO: No tasks are running which match the specified criteria.

I used TaskList in a BAT file to list all the Clarion Window Previewer EXE’s running, then offer to kill them using Task Kill. Maybe this would be good enough for you.

image

TITLE Halt Window Previews
ECHO OFF
COLOR 1E
CLS

REM Confirm Kill List
ECHO TASKLIST /FI "IMAGENAME eq WinPreview*"
TASKLIST /FI "IMAGENAME eq WinPreview*"
ECHO+
CHOICE /C:YN /N /T 99 /D N /M "Halt the above Window Previews? (Y/N)" 
IF %ERRORLEVEL% EQU 2 EXIT /B 666
ECHO+ 

REM Kill Previews 
ECHO+
ECHO TASKKILL /FI "IMAGENAME eq WinPreview*"
TASKKILL /FI "IMAGENAME eq WinPreview*"

Note that you can use Wild Cards *? in the EQ filter match.

2 Likes