c# - 向没有窗口的进程发送 WM_CLOSE 消息

标签 c# windows winapi

开始一个进程-

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

发送WM_CLOSE使用 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;
} 

使用 CreateNoWindow = false 启动 exe,发送 WM_CLOSE 消息,应用程序正常关闭。 使用 CreateNoWindow = true,WM_CLOSE 消息永远不会到达进程。甚至 PostThreadMessage 似乎也不起作用。有什么方法可以发送 WM_CLOSE 消息吗?我已经搜索了一天来找到解决这个问题的方法.. 运气不好。

编辑: 为每个应用程序安装一个 Windows 服务。启动/停止服务启动/停止应用程序。目前我们在服务停止时终止应用程序。由于它是蛮力杀戮,应用程序不会优雅地死去。一些应用程序监听 CTRL signals .现在,我只需要一些方法来向这些应用程序发送 WM_CLOSE 消息。

Edit2:如果有一个窗口,WM_CLOSE 触发一个 CTRL_CLOSE_EVENT .但是当任何进程以 CreateNoWindow = true 启动时,这永远不会触发。

最佳答案

Is there any way to send WM_CLOSE message?

WM_CLOSE 被发送到一个窗口。如果进程中没有窗口,那么就没有什么可以处理消息的。如果您希望关闭没有窗口的进程,那么发送 WM_CLOSE 不是解决方案。

看起来您只是想终止进程。当控制台进程关联窗口时,您使用 WM_CLOSE 消息触发 CTRL_CLOSE_EVENT 信号。

由于 CTRL_CLOSE_EVENT 已经是一种相当残酷的终止进程的方式,在我看来,您完全有理由简单地终止它。您已经有了一个 Process 对象。使用 Process.Kill() 简单地杀死它。

另一方面,也许您真正想问的问题是:

How do I signal CTRL_CLOSE_EVENT in another process?

相关问题可以在这里找到:Can I send a ctrl-C (SIGINT) to an application on Windows?在我看来,最好的答案是 Nemo1024,而不是公认的答案。该答案链接到他的博客文章:http://stanislavs.org/stopping-command-line-applications-programatically-with-ctrl-c-events-from-net/

据我了解,该文章应该为您提供解决问题所需的信息。它列出了四种情况。前两个涉及 WM_CLOSE 作为触发关闭信号的手段。三是杀掉进程。第四个可能是您正在寻找的。当然,这第四个选项是最复杂的。引用文章:

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.

关于c# - 向没有窗口的进程发送 WM_CLOSE 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23197669/

相关文章:

c# - WPF 中带有 BackgroundWorker 的 InvalidOperationException

windows - pipenv 在安装依赖项时找不到模块 'runpy'

Windows:没有主目录的用户

ios - Cordova Build失败,错误为带有args的命令xcodebuild错误代码65

winapi - 在没有 .NET 的系统上启动 .NET 应用程序时,带有 SEE_MASK_FLAG_NO_UI 的 ShellExecuteEx 显示错误

c++ - 将子窗口添加到分层窗口

c - MinGW 上函数 'getaddrinfo' 的隐式声明

c# - iTextSharp 中的 Pades LTV 验证抛出公钥不是用于根 CA 证书的证书签名

c# - 如何在c#中调用delphi dll的事件?

c# - ASP.NET/DotNetNuke - 将 HTML 添加到用户控件的正文中