1

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
4

1 に答える 1