-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
I'm working on an application that launches powershell scripts (using powershell core) to setup and launch processes. I want to be able to send ctrl+break or 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. So when stopping my launched powershell processes, I call GenerateConsoleCtrlEvent and send the powershell's process group a ctrl+break. While this effectively closes child processes of the powershell process, it puts the powershell process into debug mode which is undesirable.
I have tried 2 strategies to work around this both fail in different ways:
-
In the powershell process I launch, I call
SetConsoleCtrlHandler(NULL,FALSE)to turn ctrl+c back on and have my application generate ctrl+c events. This works perfectly the first time. However, oddly, the parent console now no longer seems to propogate ctrl+c to child processes. For example, if I run my app and start some ps processes and succesfully stop them with ctrl+c events and then terminate my app and THEN runping 8.8.8.8 -t, ctrl+c does nothing. -
I register a new handler to capture BREAK events in the powershell process in hopes this will override the powershell behavior that enters debug mode. However, when this handler is called, even if I have it do absolutely nothing, via a ctrl+break signal, an access violation is thrown in the powershell process.
Is there a clean way to disable the debug behavior invoked from ctrl+break or is there any thoughts why ctrl+c signals do not get propogated if I turn that handler on?