[python-win32] How to get PID from a Proces Handle ?

Scott Hogsten scotthogsten at yahoo.com
Thu Oct 12 16:01:35 CEST 2006


I tried a bunch of stuff yesterday but couldn't get a good solution. It doesn't appear that
MS gives a good way for a process to return it's PID.  I ended up wrapping the program
under test in C and using that to dump the PID to disk.

Scott
----- Original Message ----
From: Mark Hammond <mhammond at skippinet.com.au>
To: Scott Hogsten <scotthogsten at yahoo.com>; python-win32 at python.org
Sent: Thursday, October 12, 2006 7:18:34 AM
Subject: RE: [python-win32] How to get PID from a Proces Handle ?

> I have some code that spawns processes using the "spawnv"
> command this returns a process handle.
> Can I use the process handle to retrieve the actual PID of
> the process ? This is part of a test harness
> and I need to be able have the actual PID to use against some output.

Good question!  It's not immediately obvious how to do this simply, but at a
pinch, maybe win32pdh or WMI could be used?

Other (more painful) option is to use CreateProcess to spawn your process.
The best Python implemented example of how to do this I know of is at
http://svn.zope.org/*checkout*/Zope/trunk/lib/python/nt_svcutils/service.py?
content-type=text%2Fplain - the main advantage is that you can redirect the
output any place you like.

Hope that helps.

Mark






More information about the Python-win32 mailing list