killing a process in windows

Stephen Boulet stephendotboulet at motorola_._com
Fri Oct 17 10:51:45 EDT 2003


The exact error is:

 >>> win32api.TerminateProcess(100,0)
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
pywintypes.error: (6, 'TerminateProcess', 'The handle is invalid.')

What seems to be happening is that exiting a python script (leaving a 
process running, let's call it "process X", that I launched with the 
script) and starting a new script changes the value of the process ID 
for "process X".

Stephen Boulet wrote:
> This is wierd. I can launch and kill a process in windows, but only if I do
> it in the same script.
> 
> Just using xemacs as an example, I can launch it like so:
> 
> import os, win32api, sys,os.path
> prog = "xemacs.exe"
> command = os.path.join(base,prog)
> mode = os.P_NOWAIT
> id = os.spawnl(mode,command)
> 
> and then kill it:
> win32api.TerminateProcess(id,0)
> 
> This works. But I want the script to launch the program if the script is
> passed the argument "start", or kill it otherwise. Since there's no way
> that I know to query the process ID if python doesn't launch the program in
> the first place, I write the ID to a temp file and then read the id to kill
> the process. But this doesn't work:
> 
> import os, win32api, sys,os.path
> 
> argument = sys.argv[1]
> 
> base = "C:\\Program Files\\XEmacs\\XEmacs-21.4.13\\i586-pc-win32"
> if argument == 'start':
>     prog = "xemacs.exe"
>     command = os.path.join(base,prog)
> 
>     mode = os.P_NOWAIT
>     id = os.spawnl(mode,command)
>     f = open(os.path.join(base,'temp.txt'),'wb')
>     f.write(str(id))
>     f.close()
> else:
>     id = int(open(os.path.join(base,'temp.txt')).read())
>     win32api.TerminateProcess(id,0)
> 
> Sorry I don't remember the error exactly, but it involved 'improper handle'.
> 
> Is there something I'm missing? Is there a way to query for running process
> IDs? Thanks.
> 





More information about the Python-list mailing list