调用从Delphi应用程序.net4.0 COM服务器后错误异常 [英] Wrong exception after calling .net4.0 com server from delphi application

查看:184
本文介绍了调用从Delphi应用程序.net4.0 COM服务器后错误异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在迁移我们从BDS2006 codeBase类为RAD Studio企业版,我们发现了一些很奇怪的现象:如果我们把无效的浮点操作(即除以零。)创建从实现的COM服务器的一些对象之后。 Net4.0,我们没有得到正常的异常(即EDivisionByZero),但EStackOverflow。

We are migrating our codebase from BDS2006 to Rad Studio XE, and we found some very strange behavior: if we make invalid floating point operation (ie. division by zero) after creating some object from COM server implemented in .Net4.0, we don't get normal exception (ie. EDivisionByZero), but EStackOverflow.

我们设法prepare很简单的例子: ComErrorExample

We managed to prepare very simple example: ComErrorExample

有一个.NET 4.0组件,与COM接口(一个函数返回的字符串)和简单的Delphi应用程序:

There is an .net 4.0 assembly, with com interface (one function returning string) and simple delphi application:

var
  a, b: Double;
  Stored8087CW: Word;

begin
  CoInitialize(nil);

  try
    b := 0;
    a := 1 / b;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message, ' (Expected type of exception)');
  end;

  Stored8087CW := Get8087CW;
  Writeln('Code from .NET COM: ', CoExampleOfCOM.Create.DoSomething);
  Set8087CW(Stored8087CW); //it's only to show that 8087 control word doesn't change

  try
    b := 0;
    a := 1 / b;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message, ' (Unexpected type of exception! Why stack overflow?)');
  end;

  Readln;

  CoUninitialize;

end.

正如你所看到的,我们提出两个被零除 - 第一个,COM对象创建之前,抛出EDivisionByZero,第二次抛出EStackOverflow

As you can see, we make two divisions by zero - first one, before com object creation, throws EDivisionByZero, second one throws EStackOverflow.

我们测试了这种对Win7的X64,和WinXP X32 - 没有什么区别。但是,当我们切换COM服务器从.net4.0到.NET3.5 - 一切正常。

We tested this on win7 x64, and winXP x32 - no difference. But when we switched com server from .net4.0 to .net3.5 - everything works fine.

问:我们做错了什么?我们能做些什么来解决这个问题?

Question: are we doing something wrong? Can we do something to fix this problem?

切换到.NET3.5,或倾倒德尔福是不是我们的选择。

Switching to .net3.5, or dumping Delphi isn't an option for us.

更新: 我们之前,但没有任何的运气检查浮点配置(Set8087CW())。 UPDATE2:我已经与恢复浮点配置扩展的例子

UPDATE: We checked the floating point configuration ( Set8087CW() ) before but without any luck. UPDATE2: I've expanded example with restoring floating point configuration.

推荐答案

这看起来像是在COM DLL正在改变浮点处理器的配置。见Default8087CW和Set8087CW在Delphi的帮助。

It looks like something in the COM DLL is changing the floating point processor configuration. See Default8087CW and Set8087CW in the Delphi help.

您可以将它保存做任何与COM DLL之前,之后恢复它。

You can save it before doing anything with the COM DLL and restore it afterwards.

var
  Saved8087CW: Word;
begin
  Saved8087CW := Default8087CW;
  // If you want, disable all fpu exceptions 
  // with the next line.
  Set8087CW($133F);
  DoYourComOperationHere;
  Set8087CW(Saved8087CW);
end;

这篇关于调用从Delphi应用程序.net4.0 COM服务器后错误异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆