How to get os handle (windows) to a running program (not current) so I can get other info about it like its PID

69 views
Skip to first unread message

Louis LaBrunda

unread,
Oct 17, 2012, 11:40:12 AM10/17/12
to va-sma...@googlegroups.com
Hi Gang,

Does anyone know how to get an os handle (windows) to a running program (not current, like xxx.exe).  I want to get info about it like its PID, how much memory it is using and the like.

Lou

Louis LaBrunda

unread,
Oct 29, 2012, 3:37:44 PM10/29/12
to va-sma...@googlegroups.com
Well, I have an answer to my own question but I'm still a little confused:(

The snippet of code below will get handles and PIDs for all the running programs (at least that is what I think they are).  What I find confusing is that some of the handles look like PIDs (when checked in the Windows Task Managed) those handles that look like PIDs get 0 when I ask for its PID.  Does anyone know enough about C and Windows to tell me what's going on here?

| pf osProcessesArray osProcessCountReturned osProcesses osProcessCount result pIds |
osProcessesArray := ByteArray new: 1000.
osProcessCountReturned := ByteArray new: 4.
pf := PlatformFunction fromArray: #('C' 'EnumProcesses' nil 'Psapi.dll' #(#pointer #int32 #pointer) #bool).
result := pf callWith: osProcessesArray with: osProcessesArray size with: osProcessCountReturned.

result ifTrue: [
osProcessCount := (osProcessCountReturned uint32At: 0) // 4.
osProcesses := Array new: osProcessCount.
pIds := Array new: osProcessCount.
1 to: osProcessCount do: [:i | | offset h pId |
offset := (i - 1) * 4.
h := OSHwnd immediate: (osProcessesArray uint32At: offset).
h notNull ifTrue: [
osProcesses at: i put: h.
pId := ByteArray new: 4.
h getWindowThreadProcessId: pId.
pIds at: i put: (pId uint32At: 0).
].
].
].

osProcesses inspect.
pIds inspect.

Bigger picture.  I am trying to find the handle of one program (maybe two) in-particular.  So, I need to find the name of the Exe that is running from the handle.  When I find the program I want I then need to get its PID and other info like how much memory and CPU it is using.

Lou

Normand Mongeau

unread,
Nov 2, 2012, 8:32:19 AM11/2/12
to va-sma...@googlegroups.com
The code you posted is flawed.  The enumProcesses API returns a list of PIDs, yet you later interpret it as if they were window handles (which is what getWindowThreadProcessId uses). 

Why don't you use FindWindow or FindWindowEx to get a window handle, they you can use getWindowThreadProcessId to get its PID?

Normand

Louis LaBrunda

unread,
Nov 5, 2012, 12:08:22 PM11/5/12
to va-sma...@googlegroups.com
Hi Normand,

Thanks for the reply.  For some reason I thought EnumProcesses was returning a list of handles, which is among other things is what I was looking for.  I will look at FindWindow again (but what I'm looking for doesn't run in a window).  At this point I am still in a bit of a fog as we just got power back on after six days.

Lou

Louis LaBrunda

unread,
Nov 9, 2012, 4:33:07 PM11/9/12
to va-sma...@googlegroups.com
I extended OSCall with the code below (getProcessIds) to get an array of program ids (PIDs).  I then use: 

OSHandle openProcess: ProcessAllAccess fInherit: false iDProcess: pId

to get a handle.

Lou 


getProcessIds
"Call Windows (EnumProcesses) to get the PIDs (process Ids)."
| pf pIdsRequested osProcessesArray osProcessCountReturned osProcessCount result pIds |

pIdsRequested := 1000.

osProcessCountReturned := ByteArray new: 4.
[
osProcessesArray := ByteArray new: (pIdsRequested * 4).
pf := PlatformFunction callingConvention: 'C' function: 'EnumProcesses' library: 'Psapi.dll'
parameterTypes: #(#pointer #int32 #pointer) returnType: #bool.

result := pf callWith: osProcessesArray with: osProcessesArray size with: osProcessCountReturned.
osProcessCount := (osProcessCountReturned uint32At: 0) // 4.
osProcessCount < pIdsRequested.
] whileFalse: [pIdsRequested := pIdsRequested * 2].

result ifTrue: [

pIds := Array new: osProcessCount.
1 to: osProcessCount do: [:i | | offset pId |

offset := (i - 1) * 4.
pId := osProcessesArray uint32At: offset.
pIds at: i put: pId.
].
].

^pIds.
Reply all
Reply to author
Forward
0 new messages