Cant destroy arbitrary window using p/invoke in C#

  .net-core, c++, pinvoke

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.

Source: Windows Questions C++

LEAVE A COMMENT