Is a win32 process running using the pid.

Guy guy.flowers at Machineworks.com
Tue Feb 3 06:53:19 EST 2004


Hi, I created a function, which seems to work, quite well, it tells me
if a process is running in widows using the pid number (I'm aware that
the pid number in widows is not called a pid and this is really a unix
thing but what the hell). The function keeps crashing (I've included
the crash but I don't think it will do any good.), only sometimes,
most of the time it works fine and I don't know why, Could any body
suggest anything, maybe someone has a stable function that can do the
below :

CRASH
======

 Traceback (most recent call last):
  File "E:\MW\MAINLINE\Makefile\Build\PCBuild.py", line 607, in ?
    PCBuild_Support.MakefileBuild(Makefile_Modules_lst,Flavour_lst,BuildSetUp,Enviroment)
  File "E:\MW\MAINLINE\Makefile\Build\PCBuild_Support.py", line 100,
in MakefileBuild
    CreateBuildLogs(ModulesBeingBuilt_lst)
  File "E:\MW\MAINLINE\Makefile\Build\PCBuild_Support.py", line 252,
in CreateBuildLogs
    if(process.IsProcessRunning(Build[4]._processId)==N):             
         # Checks to see is process has ended. (The processID is the
pid number
 and is obtained from the process class.(Your Flavorite Simon.))
  File "E:\MW\MAINLINE\Makefile\Build\process.py", line 2450, in
IsProcessRunning
    hcs.append(win32pdh.AddCounter(hq, path))
pywintypes.error: (-1, 'AddCounter', 'No error message is available')

FUNCTION
=========

# IsProcessRunning - Supply the process Id number and this function
will tell you if the process is running or not by returning Y and N.
def IsProcessRunning(pid):
    if sys.platform.startswith("win"):
        object = "Process"
        win32pdh.EnumObjects(None, None, 0, 1)
        items, instances = win32pdh.EnumObjectItems(None,None,object,
win32pdh.PERF_DETAIL_WIZARD)
        # Need to track multiple instances of the same name.
        instance_dict = {}
        for instance in instances:
            try:
                instance_dict[instance] = instance_dict[instance] + 1
            except KeyError:
                instance_dict[instance] = 0
        # Bit of a hack to get useful info.
        items = ["ID Process"] + items[:5]
        for instance, max_instances in instance_dict.items():
            for inum in xrange(max_instances+1):
                try:
                	hq = win32pdh.OpenQuery()
                	hcs = []
                	for item in items:
                    	path = win32pdh.MakeCounterPath(
(None,object,instance, None, inum, item) )
                    	hcs.append(win32pdh.AddCounter(hq, path))
                	win32pdh.CollectQueryData(hq)
                    type, val =
win32pdh.GetFormattedCounterValue(hcs[0],win32pdh.PDH_FMT_LONG)
                except:
                    val=0
                if (val == pid):
                    return Y
                win32pdh.RemoveCounter(hcs[0])
                win32pdh.CloseQuery(hq)
        return N
    else:
        return N

TIA
TTFN
Guy



More information about the Python-list mailing list