5

Starting a process -

 ProcessStartInfo psi = new ProcessStartInfo("G:\\SampleWinApp.exe");            
 psi.UseShellExecute = false;
 psi.CreateNoWindow = true;            
 Process prcs = Process.Start(psi);

Send WM_CLOSE using PostMessage

const int WM_CLOSE = 0x0010;

public void SendCloseSignal(Process proc)
{
    uint uiPid = (uint) proc.Id;
    bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
    if (!bResult && Marshal.GetLastWin32Error() == 0) {
        object objWnd = processWnd[uiPid];
        if (objWnd != null) {
            IntPtr ptrWnd = (IntPtr) objWnd;
            PostMessage(ptrWnd, WM_CLOSE, 0, 0);
            return;
        }
    }

    foreach (ProcessThread thread in proc.Threads) {
        PostThreadMessage((uint) thread.Id, WM_CLOSE, UIntPtr.Zero, IntPtr.Zero);
    }

}

private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
{
    uint uiPid = 0;
    if (GetParent(hwnd) == IntPtr.Zero)
    {
        GetWindowThreadProcessId(hwnd, ref uiPid);
        if (uiPid == lParam)
        {
            processWnd[uiPid] = hwnd;
            return false;
        }
    }
    return true;
} 

Start exe with CreateNoWindow = false, WM_CLOSE message is send and application shuts down gracefully. With CreateNoWindow = true, WM_CLOSE message never reach the proess. Even PostThreadMessage does not seem to work. Is there any way to send WM_CLOSE message? I have search for a day to find a solution to this.. no luck.

Edit: A windows service is installed for every application. Starting/Stopping the service starts/stops the application. Currently we kill the application on service stop. As its a brute force kill, applications do not die gracefully. Some of the applications listen for CTRL signals. Now, I just need some way to send WM_CLOSE message to these applications.

Edit2: If there is a window, WM_CLOSE triggers a CTRL_CLOSE_EVENT. But when any process is started with CreateNoWindow = true, this never triggers.

A G
  • 21,087
  • 11
  • 87
  • 112

1 Answers1

5

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.

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    So if that's not the solution, what is? – Gabe Apr 21 '14 at 12:51
  • 1
    @Gabe What is the problem? – David Heffernan Apr 21 '14 at 12:52
  • David, Not to be very strict. You can understand from the question right OP is struggling "How to close a window....". You explained why it doesn't work, It will be better if you can say what are the options op have in hand. 1) Kill is a brutal option available. – Sriram Sakthivel Apr 21 '14 at 12:54
  • 1
    @SriramSakthivel Maybe. But I tend to believe in answering the question that was asked. I don't really want to speculate as to what the actual problem is. The asker didn't state the underlying problem. I don't feel terribly confident in trying to guess what it is. – David Heffernan Apr 21 '14 at 12:56
  • I still don't really understand, and I don't know what `FireDaemon` is. You want to send `CTRL_BREAK_EVENT` or `CTRL_C_EVENT` signals? If so I wonder why the question asks about `WM_CLOSE`. – David Heffernan Apr 21 '14 at 13:05
  • Yes, some of the processes are command line programs. CTRL_CLOSE_EVENT would trigger a clean exit. – A G Apr 21 '14 at 13:08
  • PostMessage(WM_CLOSE) triggers a CTRL_CLOSE_EVENT, but does not work with processes with no window. – A G Apr 21 '14 at 13:08
  • CTRL_CLOSE_EVENT might be brutal, but a console application can trap this event. I made a simple exe, I could debug this event and do some quick cleanup tasks. – A G Apr 21 '14 at 13:22
  • 1
    It sounds to me as though the question you wanted to ask is, *how do I send a `CTRL_CLOSE_EVENT` signal to another process?* – David Heffernan Apr 21 '14 at 13:27