gpt4 book ai didi

c# - "Overflow or underflow in the arithmetic operation"WPF 特定问题

转载 作者:太空狗 更新时间:2023-10-29 17:39:46 24 4
gpt4 key购买 nike

我的 WPF 测试应用程序(非常简单,只有一个窗口)正在使用第 3 方托管的 dll(比如 X.dll)。此托管 dll 使用一些非托管 dll。因此,假设我编写了一个仅引用 X.dll 的小型 wpf 应用程序。在窗口的构造函数中,我访问了 X.dll 中的某些内容(即在 X.dll 中的某个 namespace 中)。在这样做时,我没有发现任何异常,而且事情似乎按预期进行。但是在将控件返回到 .NET 运行时时,我在 Application 类的“DispatcherUnhandledException”处理程序中遇到异常:

“算术运算上溢或下溢。”System.ArithmeticException 未处理Message="运算上溢或下溢。"
来源="PresentationFramework"
堆栈跟踪:

System.Windows.Window.ValidateTopLeft(Double length)<br/>
System.Windows.Window.CoerceTop(DependencyObject d, Object value)
System.Windows.DependencyObject.ProcessCoerceValue(DependencyProperty dp, PropertyMetadata metadata, EntryIndex& entryIndex, Int32& targetIndex, EffectiveValueEntry& newEntry, EffectiveValueEntry& oldEntry, Object& oldValue, Object baseValue, CoerceValueCallback coerceValueCallback, Boolean coerceWithDeferredReference, Boolean skipBaseValueChecks)<br/>
System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, OperationType operationType)<br/>
System.Windows.DependencyObject.CoerceValue(DependencyProperty dp) at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)<br/>
System.Windows.Window.CreateSourceWindowImpl() at System.Windows.Window.SafeCreateWindow() at System.Windows.Window.ShowHelper(Object booleanBox)<br/>
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)<br/>
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

几点:

  • 这只发生在 WPF 应用程序中,不会发生在 winforms 应用程序中。
  • 这不会被 try-catch 捕获。仅在应用程序的 DispatcherUnhandledException 中
  • 如果我在 Window 的“加载”事件中访问 X.dll 的代码,则不会发生这种情况,只会发生在构造函数中。

有人能猜出问题吗?

谢谢,米沙尔

最佳答案

按照@Mishhl 的链接,修复是

public class FloatingPointReset
{
[DllImport("msvcr110.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int _fpreset();


public static void Action()
{
// Reset the Floating Point (When called from External Application there was an Overflow exception)
_fpreset();
}
}

这是由包含的 DLL 中的某些内容将 FP 重置为与 WPF/C#/Microsoft DLL 不兼容的状态引起的。 Delphi/CPPB 默认执行此操作。

因此在窗口构造函数或 App() 构造函数中只需执行

FloatingPointReset.Action();

您可能需要更改以下内容以引用任何版本的 msvcr###.dll

[DllImport("msvcr110.dll", CallingConvention = CallingConvention.Cdecl)]

例如

[DllImport("msvcr70.dll", CallingConvention = CallingConvention.Cdecl)]

关于c# - "Overflow or underflow in the arithmetic operation"WPF 特定问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2407040/

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