Omega_
Omega_

Reputation: 31

How to destroy a Third Party WPF Child Window using c#?

I am trying to destroy a child window created by a third party application. I tried to use WinApi to make this happen but it seems that DestroyWindow(hwnd) doesn't do the trick. I indeed get a handle to work with but nothing happens, the child window remains. I tried to use CloseWindow(hwnd) just to verify the child window can be modified and yes, CloseWindow(hwnd) minimizes the Child Window but I want to close it.

This is my Code:

[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DestroyWindow(IntPtr hwnd);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CloseWindow(IntPtr hWnd);

private static void CloseChildWindow();
{
  IntPtr _hwnd = IntPtr.Zero;

  if (WindowController.FindWindow(null, "System Alert") != IntPtr.Zero)
  {
    _hwnd = WindowController.FindWindow(null, "System Alert");
    DestroyWindow(_hwnd); //Returns error code 5: Access Denied
    CloseWindow(_hwnd); //Minimizes the window therefore _hwnd is correct
  }
}

Any ideas on what is happening or a workaround would be appreciated, thanks in advance.

EDIT1: Destroywindow returns code error 5: Access Denied. Any ideas on a workaround?

Upvotes: 3

Views: 1844

Answers (1)

manuell
manuell

Reputation: 7620

The documentation for the win32 API DestroyWindow is pretty clear:

A thread cannot use DestroyWindow to destroy a window created by a different thread.

You may be able to inject a DLL in the third party application, hooking the correct thread and then issuing the DestroyWindow call from here, but I will not recommand that, as it may have weird side effects (read: crash). Some applications don't react well when their windows are destroyed under their feet.

The proper way is probably to fake an user interaction, using regular Windows message (as WM_CLOSE), or SendInput, or UI Automation.

If the user has no way to close that window, you are out of luck.

Upvotes: 2

Related Questions