gpt4 book ai didi

c# - 托管窗口时出现 BuildWindowCore 错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:55:05 25 4
gpt4 key购买 nike

我正在尝试在我的进程中托管另一个进程的窗口。为此,我使用 HwndHost,如下所示:

public class MyHandle : HwndHost
{
#region User32.dll

private static Int32 WS_VISIBLE = 0x10000000;
private static Int32 WS_CHILD = 0x40000000;
private static Int32 WS_BORDER = 0x00800000;
private static Int32 GWL_STYLE = -16;

[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, IntPtr procid);

[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32")]
private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);
#endregion
private Action WindowCoreBuilt = null;

private IntPtr m_window = IntPtr.Zero;

public MyHandle(IntPtr window, Action windowCoreBuiltDelegate)
{
m_window = window;
WindowCoreBuilt = windowCoreBuiltDelegate;
}

protected override System.Runtime.InteropServices.HandleRef BuildWindowCore(System.Runtime.InteropServices.HandleRef hwndParent)
{
int guestStyle = SetWindowLong(m_window, GWL_STYLE, WS_CHILD | WS_BORDER | WS_VISIBLE);
SetParent(m_window, hwndParent.Handle);

HandleRef hwnd = new HandleRef(this, m_window);
InvokeHelper.InvokeDelegate(this.Dispatcher, () => WindowCoreBuilt());
return hwnd;
}

protected override void DestroyWindowCore(HandleRef hwnd)
{
}
}

它通常有效,但有时我会遇到这样的异常:

System.InvalidOperationException: BuildWindowCore failed to return the hosted child window handle.
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)

堆栈跟踪更深入,但我认为它不相关。
我的问题是:
1. 是什么原因导致的,我该如何解决?
2. 我怎样才能捕获这个异常以防止我的应用程序崩溃? (它发生在我无权访问的系统线程中......)

最佳答案

_hwnd = BuildWindowCore(hwndParent);

if(_hwnd.Handle == IntPtr.Zero || !UnsafeNativeMethods.IsWindow(_hwnd))
{
throw new InvalidOperationException(SR.Get(SRID.ChildWindowNotCreated));
}

这是 WPF source code 的特定部分抛出异常。 IsWindow 是直接调用 Win32 IsWindow功能。

我的猜测是您以某种方式从 BuildWindowCore 返回了 IntPtr.Zero 句柄。

关于c# - 托管窗口时出现 BuildWindowCore 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34179157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com