Problems with CloseMainWindow() to close a Windows Explorer window

Posted by MorgoZ on Stack Overflow See other posts from Stack Overflow or by MorgoZ
Published on 2010-04-05T15:06:58Z Indexed on 2010/04/05 15:13 UTC
Read the original article Hit count: 324

Filed under:
|
|
|

Hello!

I´m facing a problem when trying to close a Windows Explorer (not Internet Explorer) window through another application, using the "Process.CloseMainWindow()" method; because it doesn´t close the Explorer window, it tries to close the full Windows (Operative System), by the way, Windows XP.

The code is as follows:

[DllImport("user32.dll")]
    static extern int GetForegroundWindow();

    [DllImport("user32.dll")]
    private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);


    public String[] exeCommand()
    {

        try
        {
            //Get App
            Int32 hwnd = 0;
            hwnd = GetForegroundWindow();
            Process actualProcess = Process.GetProcessById(GetWindowProcessID(hwnd));

            //Close App
            if (!actualProcess.CloseMainWindow())
                actualProcess.Kill();

        }
        catch { throw; }

        return null;

    }

Suppose that the "actualProcess" is "explorer.exe"

Any help will be appreciated!! Salutes!

© Stack Overflow or respective owner

Related posts about c#

Related posts about process