Intel® Business Client Software Development
Support for Intel® vPro™ software development and technologies associated with Intel vPro platforms.
1380 Discussions

Passing array of structures from c# to Intel Fortran

Carlo59
Beginner
719 Views

I made an example with thfollowing struct in Fortran:

   type :: interop_struct_num

        sequence

        real*8 :: values(10)
        integer :: id

    end type
   
    type :: interop_struct_vet
        sequence
        type(interop_struct_num) :: items(3)
    end type

the

The function declaration in Fortran has the following look:

    function pass_structure_c_Array(NumData, dataArray, TextVet) result(sum)
   
        !DEC$ ATTRIBUTES DLLEXPORT, ALIAS: 'pass_structure_c_Array'::pass_structure_c_Array

        integer, intent(in) :: NumData
        type(interop_struct_vet), intent(in) :: dataArray
        character(len=16), intent(inout) :: TextVet(NumData)
       ....

end function

On the c# side the structure is the following:

    [StructLayout(LayoutKind.Sequential, Pack = 8)] 
    public struct FortranInteropStructOnlyNum
    {

        /// <summary>
        /// Fixed length array holding double values.
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
        public double[] Values;

        public Int32 Id;
    }  

   [StructLayout(LayoutKind.Sequential, Pack = 8)] 
   public struct ForStructVect
   {
       [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
       public FortranInteropStructOnlyNum[] Items;
   }

 Whereas the function declaration is the following one:

        [DllImport("Interop.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "pass_structure_c_Array", CharSet = CharSet.Ansi)]
        public static extern double PassStructArray(ref Int32 NumV, ref ForStructVect dataV, StringBuilder SV);

I compile the dll fortran and the c# calling executable perfectly, but when I run the program I can see that the elements belonging to the array dataV.Items[i] with i>1 are partially corrupted.

I don't understand the reason of this behaviour and I thank you in advance for any help you will can give me.

 

0 Replies
Reply