Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send WM_CLOSE message to a process with no window

Tags:

c#

windows

winapi

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.

like image 708
Aseem Gautam Avatar asked Sep 30 '22 23:09

Aseem Gautam


1 Answers

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.

like image 132
David Heffernan Avatar answered Oct 13 '22 10:10

David Heffernan