[Q] how to find process id on windows? (anyone have killProcName .py?)

John Nielsen nielsenjf at my-deja.com
Wed May 17 17:36:51 EDT 2000


Even though it sounds simple, it's rather involved.

Here is a python COM object that works on NT and W2K:

john

-----------

import win32pdh, string, win32api
from win32com.server.exception import COMException
import win32com.server.util
import win32com.client.dynamic

#to generate guids use:
#import pythoncom
#print pythoncom.CreateGuid()

class pyperf:
    # COM attributes.
    _reg_clsid_ = '{763AE791-1D6B-11D4-A38B-00902798B22B}'
               #guid for your class in registry
    _reg_desc_ = "get process list and ids"
    _reg_progid_ = "PyPerf.process" #The progid for this class

    _public_methods_ = ['procids','proclist' ]  #names of callable
methods
    def __init__(self):
        self.object='process'
        self.item='ID Process'
    def proclist(self):
        try:
            junk, instances =
win32pdh.EnumObjectItems(None,None,self.object,
win32pdh.PERF_DETAIL_WIZARD)
            return instances
        except:
            raise COMException("Problem getting process list")
    def procids(self):
        #each instance is a process, you can have multiple processes
w/same name
        instances=self.proclist()
        proc_ids=[]
        proc_dict={}
        for instance in instances:
            if proc_dict.has_key(instance):
                proc_dict[instance] = proc_dict[instance] + 1
            else:
                proc_dict[instance]=0
        for instance, max_instances in proc_dict.items():
            for inum in xrange(max_instances+1):
                try:
                    hq = win32pdh.OpenQuery() # initializes the query
handle
                    path = win32pdh.MakeCounterPath(
(None,self.object,instance, None, inum, self.item) )
                    counter_handle=win32pdh.AddCounter(hq, path)
#convert counter path to counter handle
                    win32pdh.CollectQueryData(hq) #collects data for the
counter
                    type, val =
win32pdh.GetFormattedCounterValue(counter_handle, win32pdh.PDH_FMT_LONG)
                    proc_ids.append(instance+'\t'+str(val))
                    win32pdh.CloseQuery(hq)
                except:
                    raise COMException("Problem getting process id")

        proc_ids.sort()
        return proc_ids

if __name__=='__main__':
    import win32com.server.register
    win32com.server.register.UseCommandLine(pyperf)




In article
<857F15D7E3D8D3118D290008C7CF05862707EB at mail-naeast1.brooks.com>,
  "Lee, Jaeho" <Jhlee at Brooks.com> wrote:
> Hello,
>
> I want to find process id from process name. I read about
'killProcName.py'
> shows how to do that, in 'python programming on win32'. But I could
not find
> it. Anyone can point me where I can find it? Or could you let me know
how to
> find process id from process name (on NT 4.0)?
>
> Thanks in advance,
> Jaeho Lee
>
>

--
nielsenjf at my-Deja.com


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list