You Are Here Home > Python win32api and pywintypes.error: (5,...

Python win32api and pywintypes.error: (5, ‘TerminateProcess’, ‘Access is denied.’)

I was working on a piece of code that was supposed to close a bunch of processes ‘with the same name’ right after each other and was getting this error message:
pywintypes.error: (5, ‘TerminateProcess’, ‘Access is denied.’)

The problem was that I was doing this in a loop and Windows didn’t get enough time to close the first process yet. To fix this, just add a time.sleep(0.5) in your loop and you will be all set…

I hope this helps :)

Here is the code I’m using (found in Python mailing list):

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
# ***********************************************************************
 
# ***********************************************************************
# ***********************************************************************
def Kill_Process_pid ( pid ) :
  handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, pid) #get process handle
  win32api.TerminateProcess(handle, -1) #kill by handle
  win32api.CloseHandle(handle)        #close api
# ***********************************************************************
 
 
# ***********************************************************************
# ***********************************************************************
def Kill_Process ( name ) :
  pid = GetProcessID(name)
  if pid:
    try:
        Kill_Process_pid(pid)
        return True
    except:
        pass
  else:
    return False
# ***********************************************************************

I just modified the last function, it’s kind of funny I know but that works for me, then I call it like:

print 'Killing IEs...',
while Kill_Process('iexplore'):
   time.sleep(0.5)
print 'Done!'
Python win32api and pywintypes.error: (5, ‘TerminateProcess’, ‘Access is denied.’)
Filed under: Programming, Python   Posted by: Codehead

Got a Question?

Get answers here.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment