Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HwndHost temporarily reparents hosted window to SystemResourceNotifyWindow (SystemResources.Hwnd window) for no reason #10153

Open
aquinn39 opened this issue Dec 8, 2024 · 0 comments
Labels
Investigate Requires further investigation by the WPF team.

Comments

@aquinn39
Copy link

aquinn39 commented Dec 8, 2024

Description

In response to certain events, such as toggling high contrast mode or RDPing into a machine, HwndHost will temporarily change the parent of the hosted child window to a special invisible 'SystemResourceNotifyWindow' window created by .NET. This behaviour is supposed to only happen if the child window is taken out of the visual tree (so no longer has a parent window). This causes an unfortunate side effect when using WinUI 3's Xaml Islands (DesktopWindowXamlSource) that the child window is destroyed, causing all sorts of issues. See microsoft/microsoft-ui-xaml#10231. There is obviously also the question of if WinUI 3 should have this behaviour of destroying DesktopWindowXamlSource windows if they have their parent changed, so that is why I raised an issue for that as well. Still, HwndHost shouldn't be reparenting child windows for no reason.

This seems to be in response to the PresentationSource changing in reponse to these events for some reason. You can see exactly where it reparents the child window to the SystemResourceNotifyWindow (SystemResources.Hwnd window) here:

UnsafeNativeMethods.SetParent(_hwnd, new HandleRef(null, hwnd.Handle));
. The method where that happens is called by the method which is added as a SourceChanged handler here:
PresentationSource.AddSourceChangedHandler(this, new SourceChangedEventHandler(OnSourceChanged));
.

Reproduction Steps

  1. Debug the attached project in Visual Studio
  2. Toggle high contrast mode on or off in Windows Settings or RDP into the box
  3. The app will crash due to BuildWindowCore running again since the window was destroyed as a result of the parent changing
  4. Do not stop debugging
  5. Check the immediate window for a message that looks like the following:
    onecoreuap\windows\moderncore\inputv2\inputhost\outside\lifted\windowparentchain.cpp(326)\Microsoft.UI.Input.dll!00007FF8A4985FCC: (caller: 00007FF8A4986AB1) ReturnHr(2) tid(15c88) 8007139F The group or resource is not in the correct state to perform the requested operation. Msg:[HWND 000000000088348A changed parent from 00000000017F11E4 to 000000000042392C. Unsupported scenario.] Exception thrown at 0x00007FFA13D5FA4C (KernelBase.dll) in QPad.exe: WinRT originate error - 0x8007139F : 'WindowParentChain invalid state.'.
  6. Using a tool like Spy++, look up the window handle which the above message says is the new parent
  7. You will notice that the new parent of the Xaml Islands child window is 'SystemResourceNotifyWindow'
    Image

Expected behavior

The parent of the hosted child window in the HwndHost should never change unless the HwndHost is moved to a different window, especially not in response to trivial events such as toggling high contrast.

Actual behavior

Certain events like toggling high contrast and RDPing into the box cause the hosted child window to be reparented to the special invisible 'SystemResourceNotifyWindow' window temporarily.

Regression?

Not sure, but I tested in .NET 8 and the issue is present there as well.

Known Workarounds

None at the moment. I have tried adding my own SourceChangedHandler and setting e.handled to true to stop it going through to the HwndHost's internal one, but the internal one always seems to run first.

Impact

Unable to use WinUI 3 XamlIslands properly using a HwndHost in WPF. Could also cause issue with other types of child windows when hosted in a HwndHost.

Configuration

  • .NET 9
  • Windows 11 23H2 (22631.4460
  • x64 (OS and app)
  • Not sure

Other information

My guess is that for some reason, certain events cause a new PresentationSource to be created, and when it is first created, it isn't associated with a window handle, which causes the HwndHost to reparent the child window to the special invisible 'SystemResourceNotifyWindow'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Investigate Requires further investigation by the WPF team.
Projects
None yet
Development

No branches or pull requests

2 participants