[python-win32] Re: TerminateProcess issues

Roger Upole rwupole at msn.com
Fri Sep 23 00:16:10 CEST 2005


Without knowing how the GUI executes the methods (eg in a different 
thread,etc)
it's hard to tell what might be happening with the handle between the calls.

You might be able to open another handle to the process and use that to kill 
it.
h=win32api.OpenProcess(True, win32con.PROCESS_TERMINATE, self.newProc.pid)
win32api.TerminateProcess(h,-1)

       hth
            Roger


"Sean Q. Hendricks wrote:
> Hi all,
>
> I've been working on a Python script that executes an external Windows
> program when the user presses a button, and I would like it to stop when
> they press another button.  To that end, I created a class that
> represents the GUI, and there are two methods that are bound to button
> events.  The following is the code for the two methods:
>
> def startEncode(self,event):
>        self.newProc = subprocess.Popen(["C:\\Documents and
> Settings\Administrator\My Documents\Hendricks\Current
> Projects\ITAFeedback\Program\producer.exe"])
>        #The following line, when uncommented, allows the process to
> terminate with no problem
>        #win32api.TerminateProcess(int(self.newProc._handle),-1)
>
> def stopEncode(self,event):
>        if self.newProc:
>            win32api.TerminateProcess(int(self.newProc._handle),-1)
>
> The problem is that I receive the following error when I run the program:
>
> pywintypes.error: (5, 'TerminateProcess', 'Access is denied.')
>
> Now, I'm running as an administrator, so I don't see that as an issue,
> and when I call TerminateProcess from within the same method wherein the
> process is created, there's no problem.  I have seen some email in
> various places that mention that this can occur if a new handle is not
> created by a separate script, but I assume now that this means that even
> from a separate function in the same class, this is not possible.  So,
> is there some way to communicate the process id correctly between two
> methods of a class?  I'd really appreciate any help anyone can give me
> on this.  I've tried many different ways of starting and stopping this
> process, and I have not found an adequate solution, as yet.  I'm on
> Win2k, using Python 2.3 (having installed the subprocess module and the
> win32 extensions).
>
> Sean



More information about the Python-win32 mailing list