将数组指针从Fortran(被调用者)传递给C#(调用者)

时间:2014-04-04 18:53:55

标签: c# arrays pointers fortran fortran-iso-c-binding

我正在尝试将一个float数组从C#传递给fortran,并且fortran更改了对内部(在fortran代码中)数组的引用。当我这样做时,我只是变得垃圾,尽管它运行良好。以下是我的工作:

float[] test = new float[50];
testpointer_( test);

[DllImport("ArrayPointerTest.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void testpointer_([Out] float[] array);//Out keyword makes no difference



!DEC$ ATTRIBUTES DLLEXPORT::testpointer
subroutine testpointer(arrayout)
  implicit none
real, dimension(1:50), target :: arrayin
real, dimension(:), pointer :: arrayout
integer :: i


DO i=1,50
    arrayin(i)=i
end do
arrayout => arrayin
end subroutine

为什么呢?因为我正在将遗留代码转换为dll,并且不希望进行任何更改。有什么想法吗?

更新使用接受的答案加上一些更改
这段代码成功地使C#:“test”瞄准了fortran的值:“arrayin”。

[DllImport("ArrayPointerTest.dll", CallingConvention = CallingConvention.Cdecl)]
        static unsafe extern void testpointer(float* arrayPtr);

  private unsafe static void PointerTest()
        {
            float[] teste = new float[50];
            teste[49] = 100;

            fixed (float* testePtr = teste)
            {
                testpointer(testePtr);
            }
            for (int i = 0; i < 50; i++)
            {
                Console.WriteLine(teste[i]);
            }
            Console.Read();
        }


!DEC$ ATTRIBUTES DLLEXPORT::testpointer
subroutine testpointer(arrayout_) bind(c)
  use iso_c_binding
  implicit none



real(c_float), dimension(1:50), target :: arrayin
type(c_ptr), value ::arrayout_
real(c_float), dimension(:), pointer :: arrayout

integer :: i

call c_f_pointer(arrayout_, arrayout, [50])

do i=1,50
        arrayin(i) = i*2!you can also change arrayout here, it will be reflected
end do

arrayout = arrayin ! todo: is this causing a copy to be made, or is it changing the pointer's references?


end subroutine

1 个答案:

答案 0 :(得分:2)

使用iso_c_binding

!DEC$ ATTRIBUTES DLLEXPORT::testpointer
subroutine testpointer(arrayout) bind(c)
  use iso_c_binding
  implicit none

real(c_float), dimension(1:50), target :: arrayin
real(c_float), dimension(:), pointer :: arrayout
integer :: i


DO i=1,50
    arrayin(i)=i
end do
arrayout => arrayin
end subroutine

使用bind(c),您也不需要破坏程序名称:

static extern void testpointer([Out] float[] array);//Out keyword makes no difference

您还必须小心传递数组。我使用c_ptr

!DEC$ ATTRIBUTES DLLEXPORT::testpointer
subroutine testpointer(arrayout_) bind(c)
  use iso_c_binding
  implicit none

type(c_ptr),value::arrayout_
real(c_float), dimension(:), pointer :: arrayout

call c_f_pointer(arrayout_, arrayout, [50])