在下文中,我使用Robert Giesecke的模板来制作非托管dll。 我尝试使用以下代码将C#中的值传递给Fortran:
using System;
using System.Collections.Generic;
using System.Text;
using RGiesecke.DllExport;
namespace AddDll
{
class MyAddDll
{
[DllExport("Add", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static int Add(int a, int b)
{
return a + b;
}
}
}
在Fortran方面:
MODULE MYEXTERNALS
USE iso_c_binding
INTERFACE
FUNCTION Add(a,b) RESULT(ret) bind(c, name="Add")
USE, intrinsic :: iso_c_binding
INTEGER(c_int), VALUE, intent(in) :: a,b
INTEGER(c_int) :: ret
END FUNCTION
END INTERFACE
END MODULE MYEXTERNALS
PROGRAM CallAdd
USE MYEXTERNALS
IMPLICIT NONE
INTEGER a, b
a = 4
b = 3
PRINT*, 'a + b =', Add(a, b)
PAUSE
END PROGRAM CallAdd
工作正常! 现在我想传递一个包含以下代码的数组:
using System;
using System.Collections.Generic;
using System.Text;
using RGiesecke.DllExport;
namespace AddDll
{
class MyAddDll
{
[DllExport("Arr", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static void Arr(int [] a)
{
a[0] = 1;
a[1] = 2;
}
}
}
在Fortran方面:
MODULE MYEXTERNALS
USE iso_c_binding
INTERFACE
FUNCTION Arr(a) RESULT(ret) bind(c, name="Arr")
USE, intrinsic :: iso_c_binding
INTEGER(c_int), intent(in) :: a(2)
INTEGER(c_int) :: ret
END FUNCTION
END INTERFACE
END MODULE MYEXTERNALS
PROGRAM CallArray
USE MYEXTERNALS
IMPLICIT NONE
INTEGER a(2)
PRINT*, Arr(a)
PAUSE
END PROGRAM CallArray
给了我错误:
任何人都可以帮我,将数组从C#传递给Fortran吗?
答案 0 :(得分:2)
您的C#代码定义了void
函数,该函数对应于FORTRAN subroutine
。因此,它没有返回值,而你的意思是call
。
我认为以下应该有效:
MODULE MYEXTERNALS
USE iso_c_binding
INTERFACE
SUBROUTINE Arr(a) bind(c, name="Arr")
USE, intrinsic :: iso_c_binding
INTEGER(c_int), intent(in) :: a(2)
!INTEGER(c_int) :: ret
END SUBROUTINE
END INTERFACE
END MODULE MYEXTERNALS
PROGRAM CallArray
USE MYEXTERNALS
IMPLICIT NONE
INTEGER a(2)
call Arr(a)
print*,a
!PAUSE
END PROGRAM CallArray