Using GenerateConsoleCtrlEvent to exit a console app
Hi,
I'm new here as you can see. I'm new to C++ too which figures!
Anyway I've got an app that I've inherited at work which is used to start up and shut down other apps on other local pcs. We've had issues with shutting down some apps cleanly so that they could then be started again without any problems.
The method used to shut down console applications previously has been with TerminateProcess which hasn't been allowing the app to exit cleanly, hence the issues when restarting the app.
I've been doing a lot of hunting around trying to find a better way to perform this. The other apps which we've developed have a crtl-c handler in them and this is the preferred method to exit them when there is a console window open.
From my messing around, I've discovered I can't close a console application that's running in task manager (no console window) by using WM_CLOSE and as mentioned before, TerminateProcess has been causing issues. I've been trying to implement GenerateConsoleCtrlEvent so I can take advantage of the ctrl-c exit method but to no success. I've seen plenty of threads relating to this and they all make it sound so simple but in practice this just doesn't seem the case!
For info, when the process is created, the process id is obtained from lpProcessInformation and the dwProcessId. I have also used the CREATE_NEW_PROCESS_GROUP flag that is specified to be used when using GenerateConsoleCtrlEvent.
I've then passed the process id into the function as follows:
GenerateConsoleCtrlEvent( CTRL_C_EVENT, dwPID );
I've also tried using the SetConsoleCtrlHandler function both in the calling package and also within the console application to retieve the crtl event, but still I can't get this to work. I'm clearly doing something wrong but would really appreciate some guidance. Thanks Ben
|