Exchange string array between C# and fortran DLL

Exchange string array between C# and fortran DLL

Hi,

I need a simple way to return an array of strings with unknown size to C#. Here there is a sample code of fortran subroutine:

subroutine testStringArray(strArr)
! directive
!DEC$ ATTRIBUTES DLLEXPORT, Alias:'testStringArray' :: testStringArray
! argument
character(len=32),allocatable,intent(out) :: strArr(:)
! local variable
integer :: size
! main
print*, 'Please enter the size of array:'
read(*,*) size
allocate(strArr(size))
strArr='Hello world'C
end subroutine testStringArray

Can anyone give me a C# code to invoke this subroutine?

There is no matter in changing the directives, interface, arguments and body of subroutine. my main goal is returing strings to C#.

Best regards, Arash.

5 post / 0 nuovi
Ultimo contenuto
Per informazioni complete sulle ottimizzazioni del compilatore, consultare l'Avviso sull'ottimizzazione

You can't use allocatable or deferred-shape arrays with non-Fortran languages - at least not today. (The Fortran standard has proposed further enhancements of C interoperability that would allow this in the future.)  If you make strArr a scalar of type C_PTR (from ISO_C_BINDING), create a local allocatable of the declaration you have now, then use C_LOC to assign to strArr the address of the local, it would return to the caller a pointer to the data. You'll need to separately return the size. Taking that data pointer and converting it to something usable in C# is an exercise left for the reader.

Steve - Intel Developer Support

Thank you Steve for your answer. I was looking for a way to return array by argument in order to reduce the chance of occurrence of unexpected situations. But it seem there is no way and I have to use memory address and pointer.

Best regards, Arash.

I almost forgot - if you do this with ALLOCATABLE, you also need to make the variable SAVE. Or you can use POINTER.

Steve - Intel Developer Support

Also, see my note re: using SafeArray on your other thread.

Lascia un commento

Eseguire l'accesso per aggiungere un commento. Non siete membri? Iscriviti oggi