Silverfrost Forums

Welcome to our forums

Passing INTEGER/REAL Arrays from FTN95 to C#

2 Sep 2005 10:16 #318

Perhaps I'm missing something obvious, but I'm merely trying to pass an array from a FTN95 routine (in a DLL) to a C# static routine (in another DLL) and the following test code causes a runtime error:

[u]FTN95[/u] SUBROUTINE SUB1 LIBRARY 'CSlib.dll' ! C# DLL ASSEMBLY_EXTERNAL(NAME='CSlib.TestClass.test') TEST INTEGER IRET,ITEST(2)

IRET = TEST(ITEST)

END SUBROUTINE

[u]C#[/u] public class TestClass { public static int test(ref int[] itest) { itest[0] = 1; itest[1] = 2; return(0); } }

I'm using .NET in Visual Studio 2003. At runtime the debugger raises the error: 'Method not found: Int32 CSlib.TestClass.test(Int32 ByRef)'. Works fine if I pass a scalar (i.e. 'INTEGER ITEST' and 'ref int itest'), but not an array.

Am I using the correct syntax for FTN95/C# for passing an array by reference? Any suggestions?

Thanks in advance,

Andy.

7 Sep 2005 12:08 #321

The simple answer to your question is to use

public static unsafe int test(int* itest)

The alternative is to create a .NET array in the Fortran and to copy the Fortran array to the .NET array.

The main thing to note is that .NET arrays are 'safe arrays' with a header part containing the array properties. A Fortran array is just a contiguous block of data for the array elements. If you go into more that one dimension then you need to be aware that Fortran arrays use 'column major' ordering i.e. the opposite to C#.

Sometimes you can resolve this kind of question by looking at a disassembly of the exe/dll. To do this you can use the C# utility called ILDASM.exe.

Please login to reply.