简体   繁体   中英

WM_CLOSE message does not seem to close chrome

I am trying to close chrome from my application.

I am using the following method:

public class CloseChrome 
{

    static int WM_CLOSE = 0x0010;
    static int WM_QUIT = 0x0012;



    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);



    static public void closeCrome()
    {

        var process = Process.GetProcesses();

        foreach (var item in process)
        {
            if (item.ProcessName.Contains("chrome"))
            {
                //item.Kill();

                int ret = SendMessage(item.Handle, WM_CLOSE, 0, 0);
                Console.WriteLine("Chrome Reply: " + a);
            }

        }
    }
}

Using the kill method kills the chrome process, but when I send the WM_CLOSE message nothing happens even though the return value is 0 from sendMessage. Am I doing something wrong is chrome just ignoring my request?

Process.Handle will be the HANDLE of the process, not the HWND of the main window. In fact a process can own many windows, so the concept of a main window doesn't really apply.

What you need to do is call the (csharp equivalent of) EnumWindows and then call GetWindowProcessThreadId to test each HWND to see if it belongs to your target process.

The more usual alternative is to examine a target window with Spy++ to see if the class name is pretty unique, and if it is, you can use FindWindow .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM