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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Louis LaBrunda
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

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/qGTWvU1-PtIJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.
Reply | Threaded
Open this post in threaded view
|

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

Louis LaBrunda
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

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/eLqs5VB4MFYJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.
Reply | Threaded
Open this post in threaded view
|

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

Normand Mongeau-2
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



On Monday, October 29, 2012 3:37:45 PM UTC-4, Louis LaBrunda wrote:
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

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/u4lnGRSJflIJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.
Reply | Threaded
Open this post in threaded view
|

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

Louis LaBrunda
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

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/ydNreO77FU8J.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.
Reply | Threaded
Open this post in threaded view
|

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

Louis LaBrunda
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.

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/_0wJgmNqE84J.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.