Skip to content

Process.MainWindowHandle should prefer non-popup windows #112409

@miloush

Description

@miloush

Description

The Process.MainWindowHandle currently grabs the first window that 1) does not have owner 2) is visible:

private static bool IsMainWindow(IntPtr handle)
{
return (Interop.User32.GetWindow(handle, GW_OWNER) == IntPtr.Zero) && Interop.User32.IsWindowVisible(handle) != Interop.BOOL.FALSE;
}

Arguably if the process has multiple windows meeting this criteria with one of them having WS_POPUP, the popup is probably not the main window.

Reproduction Steps

Create a process with normal and a popup window. The repro relies on EnumWindow returning the popup first, but the order is not guaranteed.

Expected behavior

Popup windows are returned as main window even when non-popup windows exist.

Actual behavior

Prefer non-popup windows.

Regression?

No, same in .NET Framework.

Known Workarounds

Do not use Process.MainWindowHandle but do your own interop.

Configuration

.NET: not specific
OS: not specific (27793 x64)

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions