我想将一个FORTRAN DLL导入到Visual C#中。虽然我已经用函数做到了这一点,但当我想要导入具有多个输出的子程序时,问题就出现了。以下是一个简单的示例:
FORTRAN DLL:
Subroutine MySub(a,b,x,y)
!DEC$ ATTRIBUTES DLLEXPORT, STDCALL, ALIAS:'MySub' :: MySub
Implicit None
Integer, INTENT(IN) :: a,b
Integer, INTENT(OUT) :: x,y
y=a+b
x=2*a+3*b
End Subroutine MySub
C# 控制台应用程序:
using System;
using System.Runtime.InteropServices;
namespace AlReTest
{
class Program
{
[DllImport(@"D:\...\AltRetTest.dll", CallingConvention=CallingConvention.StdCall)]
public static extern int MySub(int a, int b, [Out] int x, [Out] int y);
static void Main(string[] args)
{
int a = 4;
int b = 3;
int x = 0;
int y = 0;
MySub(a, b, x, y);
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine(MySub(a, b, x, y));
}
}
}
以下是我得到的答案: x=0,y=0,MySub(a, b, x, y)=17
我在Visual C#编程方面还有点新手,但我认为上面的结果意味着两个语句即'a+b'和'2*a+3*b'已经被计算出来,但没有分配给x和y,而后者(2*a+3*b)已经被分配给了函数MySub本身!我在C#中使用了OutAttributes,在FORTRAN中使用了Intent[In]、Intent[Out],但都没有改变结果。我会感激任何帮助或建议。