Trouble killing a process on windows

Thomas Nelson thn at mail.utexas.edu
Sat Jun 2 22:49:21 EDT 2007


On Jun 2, 11:43 am, Tim Golden <m... at timgolden.me.uk> wrote:
> Thomas Nelson wrote:
> > from subprocess import Popen
> > from time import sleep
> > import win32api
> > war3game = Popen(["C:\Program Files\Warcraft III\Frozen Throne.exe"])
> > sleep(30)
> > print "slept for 30"
> > print win32api.TerminateProcess(int(war3game._handle),-1)
> > #print
> > ctypes.windll.kernel32.TerminateProcess(int(war3game._handle),-1)
> > print "terminated process"
>
> > Here's the output:
> > slept for 30
> > Traceback (most recent call last):
> >   File "C:\Python24\warcraft\runwar3.py", line 7, in ?
> >     print win32api.TerminateProcess(int(war3game._handle),-1)
> > error: (5, 'TerminateProcess', 'Access is denied.')
>
> > I'm logged in as adminstrator.  Does anyone know how to fix this
> > problem?
>
> There's nothing obvious. I assume you got your info
> from here?
>
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/347462
>
> Just for completeness, have you tried the pid-based technique
> shown there? I've no idea why it should work if this one
> doesn't but... (I have a slight suspicion that the fact that
> it opens the process with a "for-termination" flag might help).
>
> Failing that, you can see if WMI can do it (although I assume
> that, under the covers, WMI just calls TerminateProcess):
>
> http://timgolden.me.uk/python/wmi_cookbook.html#create_destroy_notepad
>
> I suppose you might have to adjust your token privs to include,
> say the Debug priv. This is designed to let you take control
> of any process (and terminate it, or whatever). If it looks
> like you need to do that, post back and I -- or someone else --
> can try to run up an example.
>
> TJG

I Tried the PID method, and the Taskkill method, and neither worked.
This is what I get for trying to work on a windows machine.  If you
could explain how to add the debug privilege, that would be great.
Just out of curiosity, is there a reason a python module like os
couldn't have a universal terminateProcess type function, that works
on all systems?  Something like os.listdir seems to work really well
everywhere.  Maybe killing processes is too complicated for this?

Thanks for the help,
Tom




More information about the Python-list mailing list