Re: howto kill a windows process by name ?
- From: stef mientki <stef.mientki@xxxxxxxxx>
- Date: Sat, 06 Oct 2007 01:30:16 +0200
Adam Pletcher wrote:
Take a look at "killProcName.py", in the win32 extension package.thanks Adam,
- Adam
-----Original Message-----Behalf
From: python-list-bounces+adam=volition-inc.com@xxxxxxxxxx
[mailto:python-list-bounces+adam=volition-inc.com@xxxxxxxxxx] On
Of stef mientki
Sent: Friday, October 05, 2007 4:40 PM
To: python-list@xxxxxxxxxx
Subject: howto kill a windows process by name ?
hello,
is there a library to kill a windows process by name ?
thanks,
Stef Mientki
--
http://mail.python.org/mailman/listinfo/python-list
that indeed it works,
but googling for killProcName.py I found messages about it being slow,
and indeed it's slow,
and that same google search turned up a faster method, see below.
thanks again,
Stef Mientki
import time
import win32api, win32pdhutil, win32con
import win32pdh, string
# ***********************************************************************
# ***********************************************************************
def GetAllProcesses():
object = "Process"
items, instances = win32pdh.EnumObjectItems(None,None,object, win32pdh.PERF_DETAIL_WIZARD)
return instances
# ***********************************************************************
# ***********************************************************************
# ***********************************************************************
def GetProcessID ( name ) :
object = "Process"
items, instances = win32pdh.EnumObjectItems(None,None,object, win32pdh.PERF_DETAIL_WIZARD)
val = None
if name in instances :
hq = win32pdh.OpenQuery()
hcs = []
item = "ID Process"
path = win32pdh.MakeCounterPath( (None,object,name, None, 0, item) )
hcs.append(win32pdh.AddCounter(hq, path))
win32pdh.CollectQueryData(hq)
time.sleep(0.01)
win32pdh.CollectQueryData(hq)
for hc in hcs:
type, val = win32pdh.GetFormattedCounterValue(hc, win32pdh.PDH_FMT_LONG)
win32pdh.RemoveCounter(hc)
win32pdh.CloseQuery(hq)
return val
# ***********************************************************************
"""
THIS IS SLOW !!
def Kill_Process ( process ) :
#get process id's for the given process name
pids = win32pdhutil.FindPerformanceAttributesByName ( process )
for p in pids:
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p) #get process handle
win32api.TerminateProcess(handle,0) #kill by handle
win32api.CloseHandle(handle) #close api
"""
# ***********************************************************************
# ***********************************************************************
def Kill_Process_pid ( pid ) :
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, pid) #get process handle
win32api.TerminateProcess(handle,0) #kill by handle
win32api.CloseHandle(handle) #close api
# ***********************************************************************
# ***********************************************************************
# ***********************************************************************
def Kill_Process ( name ) :
pid = GetProcessID ( name)
if pid:
Kill_Process_pid ( pid )
# ***********************************************************************
# ***********************************************************************
# ***********************************************************************
if __name__ == "__main__":
a = GetAllProcesses()
print a
process = 'UPD'
Kill_Process ( process )
# ***********************************************************************
.
- Prev by Date: Re: TwistedMatrix missing OpenSSL?
- Next by Date: embedding python..
- Previous by thread: RE: howto kill a windows process by name ?
- Next by thread: anyone know howto IRC to freenode over port 80?
- Index(es):
Relevant Pages
|