我最近遇到的问题如下。
我有一个用Delphi编写的.DLL文件,这个DLL文件有一个函数Divide(它接受两个整数作为参数),并返回相除后的值,一切正常。
我有一个用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)
{
}
}