Tech Support > Microsoft Windows > Development Resources > Determining the process identifier (PID) given a handle to the process?
Determining the process identifier (PID) given a handle to the process?
Posted by russandheather@gmail.com on October 4th, 2005


How does one retrieve the Process ID (PID) of a process when you have a
handle to the process?

I know how to do the reverse when I already know the process id (at
least one way)...

ProcessHandle = OpenProcess(win32_PROCESS_ALL_ACCESS, 0, pid)

But I can't seem to find a function available to determine the PID.

If it helps, the reason I want this is that I have an application that
launches a series of processes using a spawn command (which leaves me
the handles to all of the spawned processes), but I want to be able to
figure out the process IDs so I can monitor/kill the spawned processes
with the task manager.

Thanks,
Russ

PS: is there a more active win32 api group than this one?

Posted by David Jones on October 5th, 2005


russandheather@gmail.com wrote:

GetProcessId

Posted by Kellie Fitton on October 5th, 2005


Hi,

You can use the following API's to get the process ID, however,
there could be more than one copy of the prcoess running, thus,
you might need to enumerate all the running processes on your
machine, before you can get the ID for a particular process.

That said, when you launch or spawn a process program, you should
have these processes terminate on their own, rather than killing
them with the task manager, most likely by waiting on the process
by using the API WaitForSingleObjectEx(), until it exits.

GetCurrentProcessId()
GetProcessIdOfThread()
GetWindowThreadProcessId()
NtQueryInformationProcess()

http://msdn.microsoft.com/library/de...tprocessid.asp

http://msdn.microsoft.com/library/de...idofthread.asp

http://msdn.microsoft.com/library/de...dprocessid.asp

http://msdn.microsoft.com/library/de...ionprocess.asp

Hope these information helps,

Kellie.

Posted by TC on October 5th, 2005



russandheather@gmail.com wrote:

It seems fairly active to me. There are some very smart win32 folks
here (I do not count myself one), and you will usually get a good
answer if you ask a sensible question.

Otherwise try microsoft.public.win32.programmer.*

(* = kernel, ui, gdi, etc.)

HTH,
TC


Posted by russandheather@gmail.com on October 5th, 2005


Thanks very much for the help... I've looked into them all (my notes
below). Looks like (no surprise) GetProcessId is what I want. Sort of
embarrassing... my win32 docs don't have that function in it and nether
does the python library that I'm using.

I have GetProcessId working now through other means.

---

GetProcessId()
---
So unbelievably obvious I wondered why I didn't find it (reason is it
is not in my local msdn docs... likely because this is an XP/Longhorn
only call). This is clearly what I'm after but it looks like I can't
easily use it - I'm using Python and it is unavilable in the canned
win32api docs. I'll have to figure out a way. Seems strange that this
(and other calls) are only since Longhorn/XP.

GetCurrentProcessId()
---
This is not what I need (though I do use it in other instances)... it
gives you the pid for the process who calls it. I need to get it from
another process - the one that spawned it and has a handle.

GetProcessIdOfThread()
---
This is for a thread, not a process handle. If no other option I'll
pursue this, whioch means my next step is figuring out hwo to get a
handle to a thread in another process from another process, given a
handle to the thread containing proces. Seems convoluted... if
only GetProcessID() were available.

GetWindowThreadProcessId()
---
Similar to GetProcessIdOfThread() since now I need to get an hWnd of a
window in the process of interest (not even sure the process of
interest will have oine since the process is windowless).

NtQueryInformationProcess()
---
Looks like this has been deprecated so I'd like to stay away.

Thanks again,
Russ

Posted by TC on October 6th, 2005


I find the best way to locate things in msdn is to type the item name
(known or guessed), then msdn, then hit the [I'm feelin' lucky!]
button. It works for me about 99 times from 100! For example, this took
me directly to getprocessid in msdn:

www.google.com
getprocessid msdn
[I'm feelin' lucky!]

HTH,
TC


Similar Posts