En Wed, 30 Apr 2008 15:06:13 -0300, gamename <[EMAIL PROTECTED]> escribió:

win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_EVENT, pgid)

How do you determine the value of 'pgid'?

Make the child start a new process group, then its pid is the process group ID. You have to use the "creationflags" parameter of subprocess.open The documentation for GenerateConsoleCtrlEvent http://msdn.microsoft.com/en-us/library/ms683155.aspx states that you can't send CTRL_C_EVENT to another process group, only CTRL_BREAK_EVENT (and only to the same console as the sender process). A little example:

<code>
import subprocess
import ctypes
import time

CREATE_NEW_PROCESS_GROUP = 512
CTRL_C_EVENT = 0
CTRL_BREAK_EVENT = 1
GenerateConsoleCtrlEvent = ctypes.windll.kernel32.GenerateConsoleCtrlEvent

print "start child process"
p = subprocess.Popen("cmd /c for /L %d in (10,-1,0) do @(echo %d && sleep 1)",
        creationflags = CREATE_NEW_PROCESS_GROUP)
print "pid=", p.pid
print "wait 3 secs"
time.sleep(3)
print "send Ctrl-Break"
GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, p.pid)
print "wait for child to stop"
print "retcode=", p.wait()
print "done"
</code>

Output:

start child process
pid= 872
wait 3 secs
10
9
8
7
send Ctrl-Break
wait for child to stop
retcode= 255
done

(Instead of ctypes and those magical constants, you can install the pywin32 package and use win32api.GenerateConsoleCtrlEvent, win32con.CTRL_BREAK_EVENT and win32process.CREATE_NEW_PROCESS_GROUP)

The only way I know of to send a Ctrl-C event to a different console involves remote code injection.

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to