Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

ISO_C_BINDING

babe59
Beginner
470 Views
Hello,
I work under VS2005 with Intel Fortran Compiler. i must call method on DLL fortran in C#. this method is actually call by a C++ program (under linux).

Could i call this method without any change ?

here the beging of the method


[fxfortran]SUBROUTINE    CAL_INIT_OUTPUT ( DMPOutput1,DMPOutput2,DMPOutput3)
	use, intrinsic ::ISO_C_BINDING
	IMPLICIT NONE

	INCLUDE 	'xxx.def'

	INTEGER*4	I, J, K
	INTEGER*4	INDICE

	type(T_DMPOutput1), intent(out) :: DMPOutput1(DIM_BRA_DEF)
	type(T_DMPOutput2), intent(out) :: DMPOutput2(DIM_OUTILS)
	type(T_DMPOutput3), intent(inout) :: DMPOutput3
[/fxfortran]
0 Kudos
4 Replies
Wendy_Doerner__Intel
Valued Contributor I
470 Views

The Fortran code should be the same, but the setup in Visual Studio will differ than Linux. For instance you will need to use the /dll switch to make the DLL.

------

Wendy

Attaching or including files in a post


0 Kudos
babe59
Beginner
470 Views
ok it works but I have a problem when struct have array .
I couldn't get back correct array values in fortran DLL. In this example, "tabi" and "tabr" haven't good value in fortran module...

here my fortran structure:
[bash]	type, bind(C) :: T_Struct
	    INTEGER*4	aaa
	    REAL*4		bbb
	    integer*4	tabi(3)
	    real*4		tabr(4)
	end type T_Struct
[/bash]

Here my fortran method (in dll)
[bash]      SUBROUTINE	MODB (TRIGGER_NUMBER, 
     2                      V_Struct_in,
     3                      V_Struct_inout,
     4                      V_Struct_out) BIND(C,NAME="ModB")

      !DEC$ ATTRIBUTES DLLEXPORT :: ModB 

	use, intrinsic ::ISO_C_BINDING

 	IMPLICIT	NONE

      INCLUDE 	'ModB.def'

	INTEGER*4	TRIGGER_NUMBER

      integer*4 a
      real*4    b
      	
	type(T_Struct), intent(in) :: V_Struct_in
	type(T_Struct), intent(inout) :: V_Struct_inout
	type(T_Struct), intent(out) :: V_Struct_out
	

      a = trigger_number
      
      a = V_Struct_in%aaa+ 5
      b = V_Struct_in%bbb+ 1.5

      V_Struct_inout%aaa = V_Struct_inout%aaa+ 5
      V_Struct_inout%bbb = V_Struct_inout%bbb+ 1.5

      V_Struct_out%aaa = 9
      V_Struct_out%bbb = 11.11


      end subroutine[/bash]

Here my C# programm

[bash]    internal struct T_Struct
        {
            internal int entier;
            internal float reel;
            internal int[] tabi;
            internal float[] tabr;

            internal T_Struct(Boolean empty)
            {
                entier = 0;
                reel = 0.0f;
                tabi = new int[3];
                tabr = new float[4];

            }
        }
[/bash]



[bash]        [DllImport("Test_DLL_C.dll")]
        private static extern void ModB(ref int TRIGGER_NUMBER,         
                                        ref T_Struct v_Struct_in,       
                                        ref T_Struct v_Struct_inout,    
                                        ref T_Struct v_Struct_out       
                                                                    );  

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                T_Struct v_Struct_in = new T_Struct(true);
                v_Struct_in.entier = 10;
                v_Struct_in.reel = 10.1f;
                v_Struct_in.tabi = new int[] { 1, 2, 3 };
                v_Struct_in.tabr = new float[] { 1.1f, 2.2f, 3.3f };

                T_Struct v_Struct_inout = new T_Struct(true);
                v_Struct_inout.entier = 10;
                v_Struct_inout.reel = 10.1f;
                v_Struct_inout.tabi = new int[] { 1, 2, 3 };
                v_Struct_inout.tabr = new float[] { 1.1f, 2.2f, 3.3f };

                T_Struct v_Struct_out = new T_Struct(true);

                ModB(ref i, ref v_Struct_in, ref v_Struct_inout, ref v_Struct_out);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "n" + ex.InnerException);
            }
        }
[/bash]



















0 Kudos
TimP
Honored Contributor III
470 Views
Maybe only a minor contributor to the problem (if that), but I don't see why you would depend on undefined interoperability of the non-standard types integer*4, real*4 in place of integer(c_int), real(c_float).
Are you certain that your array references don't correspond to c_ptr ?
0 Kudos
babe59
Beginner
470 Views

I'v got response on other forum then for the futur search: "Fortran probably uses embedded arrays. Mark the arrays as ByValArray:"
Then I must use "[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]" for my tabi for example

0 Kudos
Reply