制作Fortran的DLL和从C#调用它 [英] Making Fortran dll and calling it from C#
问题描述
我有一个Fortran可执行的功能,我需要使它成为一个dll文件,这样我就可以调用它的功能从C#程序
I have a function in a Fortran executable and I need to make it a dll file so I can call it's functions from a C# program
FUNCTION TSAT11(P)
C ** IGNORE IMPLEMENTATION **
TSAT11 = SX*TSAT2(X) + SXL1*TSAT3-273.15
RETURN
END
p是一个浮体和函数返回一个float
P is a float and the function returns a float
在这里,我不知道从C#在FORTRAN任何东西,也叫动态链接库的东西,所以请解释一下。
The thing here that I don't know anything in fortran nor calling dlls from C#, so please explain a little more.
我使用的是康柏的Visual Fortran和C#2008。
I'm using Compaq Visual Fortran and C# 2008.
感谢您的时间。
(如果你喜欢,你可以看到完整的代码< A HREF =http://www.mediafire.com/?r49g04w5qncqgr4>在这里 [这是一个程序来计算水和蒸汽的性质])
(If you like you can see the full code Here [It's a program to calculate water and steam properties])
推荐答案
下面是使用单精度浮点数的例子
Here is an example using single precision floats.
Fortran库包括:
Fortran library contains:
FUNCTION TSAT11(P)
!DEC$ ATTRIBUTES ALIAS:'TSAT11' :: TSAT11
!DEC$ ATTRIBUTES DLLEXPORT :: TSAT11
!DEC$ ATTRIBUTES VALUE :: P
REAL, INTENT(IN) :: P
REAL :: TSAT11
! Examle calculation
TSAT11 = P - 273.15
RETURN
END FUNCTION
通过调用函数
class Program
{
[DllImport("calc.dll")]
static extern float TSAT11(float P);
static void Main(string[] args)
{
float p = 300f;
float t = TSAT11(p);
// returns 26.8500061
}
}
对于同样阵列(必须声明的大小)
Similarly for an array (must declare the size)
FUNCTION TSAT12(P,N)
!DEC$ ATTRIBUTES ALIAS:'TSAT12' :: TSAT12
!DEC$ ATTRIBUTES DLLEXPORT :: TSAT12
!DEC$ ATTRIBUTES VALUE :: N
INTEGER, INTENT(IN) :: N
REAL, INTENT(IN) :: P(N)
REAL :: TSAT12
! Examle calculation
TSAT12 = SQRT( DOT_PRODUCT(P,P) )
RETURN
END FUNCTION
通过调用 C#
代码
class Program
{
[DllImport("calc.dll")]
static extern float TSAT12(float[] P, int N);
static void Main(string[] args)
{
float[] p2=new float[] { 0.5f, 1.5f, 3.5f };
float t2=TSAT12(p2, p2.Length);
//returns 3.84057283
}
}
这篇关于制作Fortran的DLL和从C#调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!