WPF SetParent抛出ArithmeticException

时间:2018-02-08 13:58:09

标签: c# wpf winapi user32 setparent

我一直在为应用程序开发一个带有C#和WPF的插件。

在Win7机器上启动之前,它一直运行良好。症状是在启动时为 System.ArithmeticException (算术运算中的溢出或下溢)弹出Microsoft .NET Framwork的“未处理异常”对话框,并给出指向System.Windows.Controls.Primitives.Track.ComputeScrollBarLengths (...)的堆栈跟踪更深层次。

所以,我开始调试应用程序:它显示在调用user32.dll的setParent时抛出了System.ArithmeticException。当应用程序调用显示加载项UI时,即可完成此操作。

public bool ShowUI(int Parent)
{
userControl = new MyUserControl(); // Extends System.Windows.Forms.UserControl
SetParent(userControl.Handle, new IntPtr(Parent)); // <- exception thrown here
...
}

可能导致此问题的原因是什么?

1 个答案:

答案 0 :(得分:0)

我在这里添加解决方案,万一有人发现它是有用的。

非托管代码中的某些东西 - 仅在64位版本中 - 导致浮点运算导致导致此错误的异常,如Hans Passant的评论中所述。解决方案与此答案类似:https://stackoverflow.com/a/19722292/8063451

在我的附加组件初始化时添加_fpreset()函数调用解决了这个问题。

    [DllImport("msvcr.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int _fpreset();

    public static void Run()
    {
       // Other code
        _fpreset(); // Reinitialises the floating-point package
    }