windows ctrl + c signals not firing

Viewed 571

I'm working on an application that launches powershell scripts to setup and launch service processes (not formal windows services). I want to be able to send ctrl+c signals to these powershell processes and their child processes to cleanly shut them down.

When I create the powershell process I set CREATE_NEW_PROCESS_GROUP which implicitly calls SetConsoleCtrlHandler(NULL,TRUE) which effectively turns off ctrl+c for the created process and its children. I can't use ctrl+break because that puts powershell into debug mode. So in the powershell process I launch, I call SetConsoleCtrlHandler(NULL,FALSE) to turn ctrl+c back on and have my application then calls GenerateConsoleCtrlEvent to send ctrl+c events to powershell's process group when I want to stop that process tree. This works perfectly but with one major flaw.

If I manuall type ctrl+c in the console where my application is running, my application intercepts the ctrl+c and then calls GenerateConsoleCtrlEvent for each powershell process. This seems to work well but ONLY the first time.

After my application exits it seems ctrl+c is now turned off for any program i run in that console. If I run PING or my own application, ctrl+c does nothing and I'm unsure how to debug further.

Interestingly, I sometimes see handles in process explorer to the dead powershell processes in conhost.exe. If I close those handles in process explorer, SOMETIMES ctrl+c comes back. So I suspect some handle leakage but this observation is not consistent and I am careful to free my process handles in my application.

Any thoughts on what may be causing my console's ctrl+c signals to be swallowed?

1 Answers
Related