c++ - Old school Win32 Message Only 窗口无法接收消息

标签 c++ winapi sendmessage

我意识到控制台 win32 应用程序没有完全退出,所以我正在尝试切换到仅消息窗口。我正在从另一个进程启动该应用程序并尝试彻底终止它。

这是 win32 应用程序,它在启动和干净关闭时生成一个 calc.exe,它应该杀死 calc.exe

LRESULT CALLBACK    WindowProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_USER:   PostQuitMessage (0); break;
    default: return DefWindowProc (hWnd, message, wParam, lParam);break;
    }
    return 0;
}

int CALLBACK WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int show)
{
    WNDCLASSEX wc = { 0 };
    wc.cbSize        = sizeof (WNDCLASSEX);
    wc.lpfnWndProc   = WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = L"WindowClass1";

    RegisterClassEx (&wc);

    HWND hWnd = CreateWindowEx (NULL,
                L"WindowClass1",    // name of the window class
                L"Our First Windowed Program",   // title of the window
                0,
                //WS_OVERLAPPEDWINDOW,    // window style
                300,300,500,400,
                HWND_MESSAGE,
                //NULL,    // parent window, NULL
                NULL, hInstance, NULL);

    //ShowWindow (hWnd, SW_HIDE);

    PROCESS_INFORMATION pi;
    CreateWindowProcess (L"calc.exe", pi); // helper class to createprocess

    MSG msg = { 0 };
    while (true)
    {
        if (PeekMessage (&msg, 0, 0, 0, PM_REMOVE))
        {
            if (WM_QUIT == msg.message)
                break;
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
    }

    // terminate the spawn process, this is not called cleanly
    TerminateProcess (pi.hProcess, 0);
    return (int)msg.wParam;
}

我制作了一个 c# 程序,通过发送 WM_QUIT/WM_CLOSE/WM_USER 消息来干净地启动/终止应用程序(calc.exe 被销毁)。除非窗口可见(WS_OVERLAPPED 和 ShowWindow 为真),否则 Win32 应用程序不会接收消息。收到 PostMessage WM_QUIT 但 calc.exe 没有被销毁,这意味着它不是一个干净的退出。

我应该如何从 C# 应用程序中彻底杀死它?

    class Program
    {
        [DllImport ("user32.dll")]
        public static extern bool    PostMessage (IntPtr hwnd, uint msg, int wparam, int lparam);

        [DllImport ("User32.dll")]
        public static extern int     SendMessage (IntPtr hWnd, uint uMsg, int wParam, int lParam);

        static void                                                             Main (string [] args)
        {
            try
            {
                Process myProcess;
                myProcess = Process.Start ("My.exe");

                // Display physical memory usage 5 times at intervals of 2 seconds.
                for (int i = 0; i < 3; i++)
                {
                    if (myProcess.HasExited) break;
                    else
                    {
                        // Discard cached information about the process.
                        myProcess.Refresh ();
                        Thread.Sleep (4000);

                        Console.WriteLine ("Sending Message");

                        const int WM_USER = 0x0400;
                        const int WM_CLOSE = 0xF060;      // Command code for close window
                        const int WM_QUIT = 0x0012;

                        // Received only when windows is visible
                        //int result = SendMessage (myProcess.MainWindowHandle, WM_USER, 0, 0);

                        // not clean exit
                        PostMessage (myProcess.MainWindowHandle, WM_QUIT, 0, 0);
                        // doesn't receive
                        SendMessage (myProcess.MainWindowHandle, WM_QUIT, 0, 0);
                    }
                }
            }
    }
}

最佳答案

Windows 上的进程并没有真正的“MainWindow”。 Process.MainWindowHandle 是 C# 进行的猜测,它通过查看所讨论的进程是否有一个具有焦点的窗口来进行猜测——这只会找到可见的窗口。使用 FindWindowEx 查找您要关闭的窗口句柄。

接下来,当一个窗口关闭时,它不会自动尝试退出当前线程的消息循环。您需要处理 WM_DESTROY 才能调用 PostQuitMessage

在您的消息循环中,如果您没有做任何其他工作,请使用 GetMessage 而不是 PeekMessage,因为如果没有消息,PeekMessage 会立即返回这意味着应用程序线程将永远没有机会休眠。

有了这些更改,您应该可以简单地将 WM_CLOSE 发送到有效的窗口句柄,因为它将被销毁,发送一个 WM-QUIT 消息到退出消息循环,正确终止计算进程。

关于c++ - Old school Win32 Message Only 窗口无法接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48759081/

相关文章:

c++ - 如何阅读我的套接字消息

c++ - 应用程序重新启动,提升 "runas",在 ITaskbarList 使用之前不捕获 TaskbarButtonCreated Msg?

c++ - 在 MessageBox 中显示一个 int 变量

c# - SendMessage,何时使用 KEYDOWN、SYSKEYDOWN 等?

c++ - 如何在 C++ Builder 中隐藏 PageControl TabSheets 的标题

C++ 无法获取进程 ID (windows)

javascript - Chrome 扩展消息传递架构

c++ - 简单程序生成二维城市的算法?

c++ - OpenCV tracking.hpp 在哪里

c++ - 使用恰好 N 个参数创建通用 Lambda