C# Class to Fortran TYPE question

C# Class to Fortran TYPE question

I have passed single arguments such as integer, float, or arraysfrom C# toFortran successfully
for some time now (thanks to this Forum) but have not been able to pass a simple class into a TYPE
I have found some examples on the forum which compile, but the values dont go through.

On the C# side I use the DLLIMPORT statement and pass the Class and object.

C#

public class StructureTester {
int a = 1;
float b = 2.0f;
}

[DllImport("K3F_Utils.dll", CharSet = CharSet.Auto)]
public static extern void PassClassToFortran(StructureTester st);


Fortran

! type defined at top of program file
TYPE :: SampleStruct
sequence
integer :: a
real :: b
END TYPE SampleStruct

subroutine PassClassToFortran ( InputDataStructure )
!DEC$ ATTRIBUTES DLLEXPORT, ALIAS: 'PassClassToFortran' ::PassClassToFortran
! !DEC$ ATTRIBUTES TYPE (SampleStruct) :: InputDataStructure

TYPE (SampleStruct) :: InputDataStructure

end subroutine

Notes: I've simplified the code to remove the WRITE statements which show the wrong values
Notice I commented out the DEC$ of the TYPE. THis is because it does not compile. I suspect that this
is why the structure values are not coming through, yet I can't find the proper format for DEC$ a structure.
Does anyone have an example of how to fornat this line for the "incoming" TYPE ?

I'm using MSVS 2010 for C# and MSVS 2008 for FORTRAN, both x64. Simple "intrinsic" data type work OK.
IVF is 11.1.038

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Best Reply

You cannot import or pass a C# class. C "structs" can be used. There is no directive to assign a type to an identifier.

Steve - Intel Developer Support

I tested the C to Fortran'struct' interface with thislittle C program.The C passes a struct.
It steps into the Fortran functionOK.
But the Fortran TYPE does not pick up the structvalues as I can see with the debug. Crashes if I try
to use the incoming 'object'.

When I use an intrinsic type, no problem
-------------- C --------

#include "stdafx.h"

struct StructureTester

{ int a ; float b ; } ;

void PassStructureToFortran(struct StructureTester st);

int _tmain(int argc, _TCHAR* argv[])

{

struct StructureTester st;

st.a = 1;
st.b = 2.0f;

PassStructureToFortran ( st );

return 0;

}
----------------------- Fortran ---------

subroutine PassStructureToFortran ( InputDataStructure )

!DEC$ ATTRIBUTES DLLEXPORT, ALIAS: 'PassStructureToFortran' ::PassStructureToFortran

TYPE (SampleStruct) :: InputDataStructure

end subroutine

--------------\
NOTES:
The debugger shows the following but not sure how to interpret, maybe it means the first value '1'
is considered the whole struct.

- INPUTDATASTRUCTURE 0x4000000000000001 {A=??? B=??? } SAMPLESTRUCT *

OK NOW PASSING THE STRUCT FROM CWORKS WITH THE FOLLOWING CHANGES IN BOLD:

Since I really need to do this from C#, I'll have to go C#->C->Fortran

------------------------------

#include "stdafx.h"

struct StructureTester

{

int a ;

float b ;

} ;

void PassStructureToFortran(struct StructureTester *st, int v);

int _tmain(int argc, _TCHAR* argv[])

{

struct StructureTester st;

int v ;

st.a = 1;

st.b = 2.0f;

v = 3;

PassStructureToFortran ( &st, v );

return 0;

}

Leave a Comment

Please sign in to add a comment. Not a member? Join today