【发布时间】:2014-11-26 10:58:38
【问题描述】:
我最近遇到的一个问题如下。
我有一个用 Delphi 编写的 .DLL 这个 DLL 有一个 Divide 函数(它接受两个整数作为参数) 并按应有的方式返回其值。
function Divide( aFirstValue, aSecondValue : Integer ) : Double; stdcall;
begin
result := aFirstValue / aSecondValue;
end;
现在如果我使用以下参数 '5, 0' 那么它会抛出 DivideByZeroException(这是正确的 :))
但是当我从 C# 调用相同的 .DLL 时,它根本不会捕获任何异常。
[DllImport("DelphiDLL.DLL", EntryPoint = "Divide", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern float Divide(Int32 a, Int32 b);
private void Button_Click_2(object sender, System.EventArgs e)
{
try
{
TB.Text += "Divide(a,b) = ";
float temp;
temp = Divide(Convert.ToInt32(aTB.Text), Convert.ToInt32(bTB.Text));
Console.WriteLine(Marshal.GetLastWin32Error());
TB.Text += Convert.ToString(temp) + "\r\n";
}
catch (DivideByZeroException eMsg)
{
}
}
【问题讨论】:
-
有什么退回来的吗?
-
是的,在 C# 中它显示 Infinite 所以返回一个值,但它也可能是 0.0....nth
-
divisor 为零时不调用divide 方法如何?无论如何,您知道输入无效吗?
-
@SriramSakthivel - 这将处理 this 问题,但不能解决为什么没有将异常返回到调用 c# 方法的问题。
-
@SriramSakthivel 不要因为 OP 使用一个简单的例子来证明他的问题而抨击他。他的问题说得很清楚。至少他努力提供简单清晰的代码示例。这比大量寻求帮助而无需努力提炼问题的问题要好得多。 (我已经编辑了标题以删除对特定异常的引用,因为根据问题的主体,这显然并不重要。)
标签: c# delphi dll pinvoke dllimport