killing a process in windows

Stephen Boulet stephen.no at spam.theboulets.net.please
Fri Oct 17 09:07:43 EDT 2003


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.

-- 
Stephen      
              From here to there
             and there to here,
           funny things are everywhere.  -- Dr Seuss





More information about the Python-list mailing list