最近在开发一个.Net程序,其中涉及到对com组件的调用,在调用完以后如果使用一些小的测试程序继续运行,一切正常,但是在使用带有GUI的form程序继续执行时,总是出现以下异常
Exception: System.ArithmeticException
Message: 算术运算中发生溢出或下溢。
Source: System.Drawing
at System.Drawing.Font.Initialize(FontFamily family, Single emSize, FontStyle style, GraphicsUnit unit, Byte gdiCharSet, Boolean gdiVerticalFont)
at System.Drawing.Font..ctor(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte gdiCharSet)
at Payment.Phone.ErrorForm.InitializeComponent() in E:\cvsroot\develope\payment\Phone\ErrorForm.cs:line 108
at Payment.Phone.ErrorForm..ctor(Util util, String msg) in E:\cvsroot\develope\payment\Phone\ErrorForm.cs:line 139
at Payment.Phone.ConfirmForm.lblNext_Click(Object sender, EventArgs e) in E:\cvsroot\develope\payment\Phone\ConfirmForm.cs:line 212
后来到处找资料,花了我一天的时间才给解决,以下是我的解决办法:
1.声明一个DLL掉用
[DllImport("msvcr70.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern int _controlfp(int n, int mask);
2.在代码中添加以下代码方法
internal void ResetFPCR()
{
const int _EM_OVERFLOW = 0x0009001F;
const int _MCW_EM = 0x000FFFFF;
_controlfp(_EM_OVERFLOW, _MCW_EM);
}
3.在调用完com组件后,调用以上方法,如:
//这里调用com组件
//然后执行Reset方法
ResetFPCR();
注:具体详细请看http://support.microsoft.com/default.aspx?scid=KB;EN-US;q326219&