1

I'm facing an issue with TerminateProcess() function. The application I'm currently writing a JobScheduler app that schedules and launches job at a specific time. For this purpose, I'm using CreateProcess() to execute my JobLauncher.

The JobLauncher process then launches a console program (using createprocess ) which effectively executes the job executable, waits for its termination and monitors the duration, user and kernel times elapsed etc.

In order to kill the job from the JobScheduler I firstly started using TerminateProcess() but it does not allow me to close the executable itself properly. I mean i found no way to hook any termination event.

Until I find a better way than a brutal TerminateProcess(), I wrote an intermediate solution using the GenerateConsoleCtrlEvent() in the calling program.

In the job application that launches the target job executable, I installed a handler using SetConsoleCtrlHandler(). And in the handler, I can terminate the process of the job and notifies my thirdparties properly. This is the better solution I found for now.

Is there a better way to programmaticaly and properly close a process ? Do you this solution is completly absurd ? I'm not a "system-level" specialist developer though...

Z.

Zyend
  • 572
  • 1
  • 4
  • 24
  • Send the `WM_CLOSE` message to that process? – John Dibling Jan 24 '14 at 15:47
  • What do you mean by "[`TerminateProcess`] does not allow me to close the executable itself properly"? `TerminateProcess` is the most brutal way to terminate a process, it yanks the rug out -- no DLL detach notifications, no file buffers flushed, goodbye right now. Any handles that process had open will be closed on its behalf (including to its own executable file). – Adam Rosenfield Jan 24 '14 at 15:50
  • @Adam: I mean that TerminateProcess simply "terminate" the process. Period. The "terminated" process is not notified beforehand. – Zyend Jan 24 '14 at 16:07
  • @John: The joblauncher is a console application. I've no HWND handle and I do not wish to create one. – Zyend Jan 24 '14 at 16:08
  • Raise an event in JobLauncher, which the console program has a thread blocked on `WaitForSingleObject` for? – John Dibling Jan 24 '14 at 16:10

1 Answers1

0

This well know Windows console problem and you can find some solutions here.

We used on internal console utility which has name "Kamikaze". It worked as described here and for me it's a best solution cause there is no problem with porting between Windows versions and Windows architectures (x86, x64).

Community
  • 1
  • 1
Igor
  • 11
  • 1
  • Thanks for the links. I'm happy to see that the solution I wrote is not that bad at all ! – Zyend Jan 24 '14 at 16:51