Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
734 views
in Technique[技术] by (71.8m points)

c# - Send WM_CLOSE message to a process with no window

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...