SIGINT on Windows

djberg96 on 2004-12-05T02:09:00

I've been working on some C code to send a Ctrl-C to a Windows console process. Consider the following code snippet: $SIG{INT} = sub{ die "Caught signal\n" };

print "PID: $$\n";

while(1){ print "Looping\n"; sleep(5); }
If you run this in a terminal, and hit Ctrl-C, the sig handler will (eventually) pick it up and terminate. But, if you were to run that and then, in a separate program, try to do "kill(2,$pid) or die $!" it won't work. It's going to return "invalid parameter". What's going on?

The problem is in the way the GenerateConsoleCtrlEvent() works. In short, you can't kill a remote console process like that. So, I've been researching the hell out of this trying to figure out how it can be done. I'm pretty sure it CAN be done, but it's been rough going so far.

My research leads me to believe that this can be accomplished with something along the lines of this abbreviated code: GetModuleHandle("Kernel32.dll") OpenProcess(pid) VirtualAllocEx() WriteProcessMemory() CreateRemoteThread() WaitForSingleObject() But, so far all I've been able to do is kill the process, while causing an Application Error in perl.exe itself. So, I'm doing something wrong but I don't know what. I've posted my problem on one of the windows newsgroups. We'll see what happens.

PS - I have no idea why there's a space showing up in "WriteProcessMemory" above. It's not that way in my text.

update: I have been told that the latter approach still won't work. It *might* be possible with AttachConsole(), but that would XP or later only. We shall see.