none
WindowsFormsHost no longer works following windows update RRS feed

  • Question

  • I have been using the following xaml to host a windows forms control inside of my WPF dialog:

    <Grid x:Name="ConnectionGrid" DockPanel.Dock="Bottom"  RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Stretch" Margin="0,3,0,15" MinWidth="730">
        <Grid.RenderTransform>
            <TransformGroup>
                <SkewTransform/>
                <RotateTransform/>
                <TranslateTransform/>
            </TransformGroup>
        </Grid.RenderTransform>
        <WindowsFormsHost Name="WinFormsConnectionHost" Visibility="Visible"></WindowsFormsHost>
    </Grid>


    This gets populated at run time like this:

    var host = WinFormsConnectionHost;
    host.HorizontalAlignment = HorizontalAlignment.Center;
    host.VerticalAlignment = VerticalAlignment.Center;
    host.Child = winformsControl;
    StyleHostedWinFormsControl(winformsControl);


    Following a recent windows update this is throwing the following exception for some of our users.

      Hosted HWND must be a child window of the specified parent. (InvalidOperationException)

    AutoTag version: 16.5.0.14
    Office: 16 (32-bit), WORD
    Windows: Microsoft Windows NT 10.0.17763.0 (64-bit), GC memory: 26,954,252

    Stack trace:
      Exception: System.InvalidOperationException
      Message: Hosted HWND must be a child window of the specified parent.
        Stack: at System.Windows.Interop.HwndHost.BuildWindow(HandleRef hwndParent)
    at System.Windows.Interop.HwndHost.BuildOrReparentWindow()
    at System.Windows.Interop.HwndHost.OnSourceChanged(Object sender, SourceChangedEventArgs e)
    at System.Windows.SourceChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
    at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
    at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
    at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
    at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
    at System.Windows.PresentationSource.UpdateSourceOfElement(DependencyObject doTarget, DependencyObject doAncestor, DependencyObject doOldParent)
    at System.Windows.PresentationSource.OnVisualAncestorChanged(DependencyObject uie, AncestorChangedEventArgs e)
    at System.Windows.UIElement.OnVisualAncestorChanged(Object sender, AncestorChangedEventArgs e)
    at System.Windows.Media.Visual.ProcessAncestorChangedNotificationRecursive(DependencyObject e, AncestorChangedEventArgs args)
    at System.Windows.Media.Visual.ProcessAncestorChangedNotificationRecursive(DependencyObject e, AncestorChangedEventArgs args)
    at System.Windows.Media.Visual.ProcessAncestorChangedNotificationRecursive(DependencyObject e, AncestorChangedEventArgs args)
    at System.Windows.Media.Visual.ProcessAncestorChangedNotificationRecursive(DependencyObject e, AncestorChangedEventArgs args)
    at System.Windows.Media.Visual.ProcessAncestorChangedNotificationRecursive(DependencyObject e, AncestorChangedEventArgs args)
    at System.Windows.Media.Visual.ProcessAncestorChangedNotificationRecursive(DependencyObject e, AncestorChangedEventArgs args)
    at System.Windows.Media.Visual.FireOnVisualParentChanged(DependencyObject oldParent)
    at System.Windows.Media.Visual.AddVisualChild(Visual child)
    at System.Windows.FrameworkElement.set_TemplateChild(UIElement value)
    at System.Windows.Controls.ContentPresenter.UseContentTemplate.BuildVisualTree(FrameworkElement container)
    at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
    at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
    at System.Windows.FrameworkElement.ApplyTemplate()
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
    at System.Windows.UIElement.Measure(Size availableSize)
    at System.Windows.Controls.DockPanel.MeasureOverride(Size constraint)
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
    at System.Windows.UIElement.Measure(Size availableSize)
    at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
    at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
    at System.Windows.UIElement.Measure(Size availableSize)
    at System.Windows.Controls.Border.MeasureOverride(Size constraint)
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
    at System.Windows.UIElement.Measure(Size availableSize)
    at System.Windows.Controls.Control.MeasureOverride(Size constraint)
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
    at System.Windows.UIElement.Measure(Size availableSize)
    at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
    at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
    at System.Windows.UIElement.Measure(Size availableSize)
    at System.Windows.Documents.AdornerDecorator.MeasureOverride(Size constraint)
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
    at System.Windows.UIElement.Measure(Size availableSize)
    at System.Windows.Controls.Border.MeasureOverride(Size constraint)
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
    at System.Windows.UIElement.Measure(Size availableSize)
    at System.Windows.Window.MeasureOverrideHelper(Size constraint)
    at System.Windows.Window.MeasureOverride(Size availableSize)
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
    at System.Windows.UIElement.Measure(Size availableSize)
    at System.Windows.Interop.HwndSource.SetLayoutSize()
    at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
    at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
    at System.Windows.Window.SetRootVisual()
    at System.Windows.Window.SetRootVisualAndUpdateSTC()
    at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
    at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
    at System.Windows.Window.CreateSourceWindowDuringShow()
    at System.Windows.Window.SafeCreateWindowDuringShow()
    at System.Windows.Window.ShowHelper(Object booleanBox)
    at System.Windows.Window.Show()
    at AutoTag.net.windward.autotag.Framework.DataSourceOptions_Click(OfficeDocument doc)
    at AutoTag.net.windward.autotag.menu.AutoTagMenu.DataSourceOptions(OfficeDocument doc)

    Our product is part of an office COM addin and I was able to reproduce this by setting up the following environment:

    Windows 10 1809 (build 17763.557)

    Microsoft Office 2019 Professional Plus 1906 (Build 11727.20230)

    I've also tried to add the WindowsFormsHost programatically like below, but I received the same exception apon adding it to the grid.

    ConnectionGrid.Children.Clear();
    var host = new WindowsFormsHost();
    host.Child = winformsControl;
    host.HorizontalAlignment = HorizontalAlignment.Center;
    host.VerticalAlignment = VerticalAlignment.Center;
    StyleHostedWinFormsControl(winformsControl);
    ConnectionGrid.Children.Add(host);

    What can I change to make this work with the latest versions of windows? Is there a setting I can instruct my customers to change to make the version of my addin that they have work?

    Wednesday, July 3, 2019 3:43 PM

All replies

  • This may actually be occurring after an office update not a windows update.
    Wednesday, July 3, 2019 4:25 PM
  • Hi  Matt Binsfeld,

    >>This may actually be occurring after an office update not a windows update.

    >>Our product is part of an office COM addin and I was able to reproduce this by setting up the following environment:

    Does your add-in is an ActiveX control that loads a WPF application, which besides the usual WPF controls has a WindowsFormHost with a  windows forms control?

    Or you Create VSTO Add-ins?

    Create VSTO Add-ins for Office by using Visual Studio

    If yes, please go to the Microsoft Office for Developers forum for getting suitable help.

    Besides, If you run your WPF program and show this error (not on office), you can refer the following link.

    InvalidOperationException when reparenting WindowsFormsHost


    Best regards

    Yong Lu





    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 4, 2019 2:08 AM
  • Hi Young Lu,

    It's an office COM addin developed with .NET framework. The clicking buttons on our ribbon tab opens various dialogs.

    This only seems to occur when running from inside office. Specifically using Office 1906. It seems to work fine when running from Office 1905.


    Friday, July 5, 2019 8:30 PM
  • I also tried using Dispatch.Invoke() to pass adding the winformshost to my dialog off to the UI wtih no luck.
    Friday, July 5, 2019 9:17 PM

  • Hi   Matt Binsfeld,  

    >>It's an office COM addin developed with .NET framework. The clicking buttons on our ribbon tab opens various dialogs.

    This only seems to occur when running from inside office. Specifically using Office 1906. It seems to work fine when running from Office 1905.
     
    I suggest you can go to the   Microsoft Office for Developers forum for getting suitable help.


    Thank you for your understanding.


    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 8, 2019 2:27 AM