Fortran 入门——C#调用Fortran DLL
首先建立一个Fortran动态连接库项目,并写一个计算两数之和的函数,代码如下
1 function MySum(x,y)
2 implicit none
3 !DEC$ ATTRIBUTES DLLEXPORT :: MySum
4 !DEC$ ATTRIBUTES ALIAS:'MySum'::Mysum
5 integer x,y,MySum
6 MySum=x+y
7 end function
Realse编译后,复制生成的DLL.
接着新建一个C#控制台项目,将刚才复制的DLL粘贴到DEBUG目录下.然后添加代码
1 class Program
2 {
3 [DllImport("Dll1.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
4 public static extern int MySum(ref int x,ref int y);
5 static void Main(string[] args)
6 {
7 int x = 3;
8 int y = 4;
9 int result = MySum(ref x, ref y);
10 Console.WriteLine(result);
11 Console.ReadKey();
12 }
13 }
需要添加命名空间:System.Runtime.InteropServices;