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

How to export a Module procedure in a DLL

kalia
Beginner
689 Views
I want to export the module procedure so that I can call it from C#. I can access the routine if it is not part of a module, i.e., access to subroutine. But if I place the subroutine in a module, I could not make it working. How to do that? Any help would be greatly appreciated. Example code to demonstrate the requirement is as follows:

1. C# of caller class (sample, taken from other Intel article)

public class FortranDllWrap

{

[DllImport("datdatart.dll", CallingConvention = CallingConvention.Cdecl)]

public static extern void balt();

// how to code here??

}





module datdatart

! how can get the Interface access so that I do not need to define interface's routine in the caller C# class?

interface balt
module procedure baltut, baltutr

end interface

contains


subroutine baltut(a)
integer a
!DEC$ ATTRIBUTES DLLEXPORT :: BALTUT
a = 2;
end subroutine baltut

subroutine baltutr(a)
real a
!DEC$ ATTRIBUTES DLLEXPORT :: BALTUTR
a = 4.0;
end subroutine baltutr

end module datdatart


Thank you very much
0 Kudos
2 Replies
Jugoslav_Dujic
Valued Contributor II
689 Views
Your problem is two-fold:

  1. The standard solution to achieve a C#-callable routine is to declare your Fortran routines with BIND(C) attribute (meaning, callable from C or "compatible" language), and optionally give them a binding name:
    [fortran]public class FortranDllWrap
    {
      [DllImport("datdatart.dll", CallingConvention = CallingConvention.Cdecl)]
    
      public static extern void baltut();
    
    }    
    
    ...
    module datdatart
    contains
    
    subroutine baltut(a), bind(C, name="baltut")
    !DEC$ ATTRIBUTES DLLEXPORT :: BALTUT
    ...
    end subroutine baltut[/fortran]
  2. However, you cannot call a generic interface from other language, as you attempted. Generics in general (i.e. not limited to Fortran) are resolvable only within one language: in Fortran, the compiler decides which version (specific procedure) of balt will be called. C# compiler cannot do that, because it cannot see the generic interface. -- there is no bindable entity called "balt" to link with.
0 Kudos
kalia
Beginner
689 Views

Thank you Jugoslav. However, how to avoid module procedure in Fortran? I mean, how can I get the variable type (interger or real) and its kind (KIND(variable) will do, right?)? I could not find how to get whether the variable (especially if the variable is an array) is real, integer or character?

thank you very much

0 Kudos
Reply