Starting a process -
ProcessStartInfo psi = new ProcessStartInfo(\"G:\\\\SampleWinApp.exe\");
psi.UseShellExecute = false;
psi.CreateNoWindow
Is there any way to send WM_CLOSE message?
WM_CLOSE
is sent to a window. If there is no window in the process, then there is nothing to process the message. If you wish to close a process that has no window, then sending WM_CLOSE
is not the solution.
It looks like you simply want to kill processes. And you are using WM_CLOSE
messages to trigger CTRL_CLOSE_EVENT
signals when the console processes have associated windows.
Since CTRL_CLOSE_EVENT
is already a rather brutal way to kill a process, you could be perfectly well justified, in my view, to simply kill it. You already have a Process
object. Simply kill it using Process.Kill()
.
On the other hand, perhaps the question that you really wanted to ask was:
How do I signal
CTRL_CLOSE_EVENT
in another process?
A related question can be found here: Can I send a ctrl-C (SIGINT) to an application on Windows? The best answer there in my view, not the accepted one, is that of Nemo1024. That answer links to his blog article: http://stanislavs.org/stopping-command-line-applications-programatically-with-ctrl-c-events-from-net/
As I understand it, that article should give you the information needed to solve your problem. It lists four scenarios. The first two involve WM_CLOSE
as a means to trigger the close signal. The third is to kill the process. And the fourth is probably what you are looking for. Of course this fourth option is the most complex. To quote from the article:
After finding a hint at the bottom of this thread, I finally bit the bullet and started the process without a window. When the time comes to stop a process, the parent attaches its console to the child, stops itself listening to Ctrl-C event and issues the event to the console, so that the attached child terminates. It then reinstates the Ctrl-C handler for itself and frees the console.