*** QUESTIONS IS IN LINUX SECTION TOO BY ERROR! I APOLOGIZE , THIS APPLIES TO WINDOWS API ***
I'm struggling with a weird scenario, I'll try to explain everything, let's see if there's a guru out-there able to show me the way....
I've a console-based application, it may be run as a windows service or as a console application, but it also may be spawned by another application using CreateProcess with hidden console Window.
If I want to terminate cleanly the application, I can stop the service (when running as service), I can type CTRL+C in the console window and trap the SIGINT signal (when as a console), I can also install a ControlHandler and trap the console close (when as a console), but when spawning it with CreateProcess, I cannot find a reliable way to terminate it cleanly.
From the documents I read, the ::TerminateProcess API sends a WM_CLOSE , that is obviously ignored by a console application, and there're people out there suggesting to install an event pump inside the console application to handle WM_CLOSE. Well, I'm lost...The solution seams to be to spawn an hidden thread with an hidden window and it's own message pump, but I cannot find a "WORKING" and "COMPLETE" example on the web.