Я пытаюсь передать массив с плавающей запятой из С# в фортран, и фортран меняет свою ссылку на внутренний (в коде фортрана) массив. Я просто получаю мусор, когда делаю это, хотя все работает нормально. Вот что я делаю:
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