"算术运算中的溢出或下溢"WPF特定问题

mis*_*153 9 c# wpf

我的WPF测试应用程序(非常简单,只有一个窗口)正在使用第三方托管的dll(比如X.dll).这个托管的dll使用了一些非托管的dll.所以我可以说我写了一个只引用X.dll的小型wpf应用程序.在窗口的构造函数中,我访问X.dll内部的东西(即在X.dll中的某些命名空间中).在这样做时,我没有发现任何异常,似乎事情按预期进行.但是在将控件返回给.NET运行时后,我在Application类的'DispatcherUnhandledException'处理程序中得到一个异常:

"算术运算中出现溢出或下溢."System.ArithmeticException未处理 Message ="算术运算中出现溢出或下溢".
Source ="PresentationFramework"
StackTrace:

System.Windows.Window.ValidateTopLeft(Double length)
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)
System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, OperationType operationType)
System.Windows.DependencyObject.CoerceValue(DependencyProperty dp) at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
System.Windows.Window.CreateSourceWindowImpl() at System.Windows.Window.SafeCreateWindow() at System.Windows.Window.ShowHelper(Object booleanBox)
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

一些要点:

  • 这只发生在WPF应用程序中,而不是在winforms应用程序中.
  • 这不会陷入尝试捕获.仅在Application的DispatcherUnhandledException中
  • 如果我在Window的'Loaded'事件中访问X.dll的代码,则不会发生这种情况,只会在构造函数中发生.

有谁能猜到这个问题?

谢谢你,米沙尔

Ric*_*son 10

根据@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

所以在窗口构造函数或App()构造函数中都可以

FloatingPointReset.Action();
Run Code Online (Sandbox Code Playgroud)

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

[DllImport("msvcr110.dll", CallingConvention = CallingConvention.Cdecl)]
Run Code Online (Sandbox Code Playgroud)

例如

[DllImport("msvcr70.dll", CallingConvention = CallingConvention.Cdecl)]
Run Code Online (Sandbox Code Playgroud)


mis*_*153 5

我不确定根本原因,但解决方案在这里:http:
//social.msdn.microsoft.com/forums/en-US/wpf/thread/a31f9c7a-0e15-4a09-a544-bec07f0f152c

似乎是一个流行的bug :)

谢谢,米沙尔