`Process.MainWindowHandle` should prefer non-popup windows

Open
#112,409 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp

Research direction

Start in src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Win32.cs at the linked window-selection code around lines 43-46. Reproduce a process with both normal and popup windows, then verify that MainWindowHandle prefers the non-popup window when both are visible and unowned.

Written by the indexing model from the issue text.

Description

area-System.Diagnostics.Process
Description

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

https://github.com/dotnet/runtime/blob/771539182c280c9933ad57422583f1380541eda1/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Win32.cs#L43-L46

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

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.