-1

I am getting all the windows using various P/Invoke functions and filtering it using code from here (IsAltTabWindow) converted to C#:

Why does EnumWindows return more windows than I expected?

public static List<IntPtr> GetWindows()
{
    List<IntPtr> result = new List<IntPtr>();
    GCHandle listHandle = GCHandle.Alloc(result);
    try
    {
        EnumWindowProc callback = new EnumWindowProc(EnumWindow);
        EnumWindows(callback, GCHandle.ToIntPtr(listHandle));
    }
    finally
    {
        if (listHandle.IsAllocated)
            listHandle.Free();
    }
    return result;
}

public static List<IntPtr> GetTaskBarWindows()
{
    List<IntPtr> result = GetWindows().Where(i => IsTaskBarWindow(i)).ToList();
    return result;
}

now I need to kill a window by its IntPtr but when I call var b = DestroyWindow(item.window); I got false as return and the window is unaffected.

Are there known limitations on using DestroyWindow? What can I do to kill a window?

Note: there are windows which share same process (eg Edge Browser) hence I cant kill it by process ID - it will kill all of them and I only need one at a time.

Boppity Bop
  • 9,613
  • 13
  • 72
  • 151
  • 3
    _"...A thread cannot use `DestroyWindow` to destroy a window created by a different thread..."_ this also implies that cross-process will not work as the code calling `DestroyWindow` will always be in a different thread from the thread that created the window - source - https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-destroywindow – Richard Critten Oct 19 '21 at 12:47

1 Answers1

0

Looked for different solution after @RichardCritten comment and used PostMessage instead of DestroyWindow and it works!

var b = PInvoker.PostMessage(item.window, (uint)PInvoker.WM.CLOSE, 0, 0);
Boppity Bop
  • 9,613
  • 13
  • 72
  • 151