Lahey/Fujitsu Fortran  

Calling Fortran DLLs from .NET Applications

Programs created using a .NET language dynamically load unmanaged DLL's at runtime, so the DLL name and characteristics must be specified in the managed code. When creating a native Fortran DLL that can be called by a .NET application, compile and link with one of the following -ml options: winapi, msvc, lf95, or fc. DLLs built with the -ml msvb option can be called from VB.NET applications. DLLs built with the -ml options lf90, bc, or bd cannot be called from .NET languages.

A DLL compiled and linked with -ml lf95, or with no -ml option uses the cdecl calling convention.

A DLL compiled and linked with -ml winapi, -ml msvc, or -ml fc, uses the stdcall2 calling convention.

Fortran function results and argument types must map to .NET variable types.

Example code demonstrating calling Fortran DLL's from .NET languages exist in subdirectories under the Win32\Examples\Mix_Lang directory. These directories all contain .NET in the directory name.

Calling LF95 DLLs from Microsoft C#

Syntax for procedure calls using the cdecl convention:

[DllImport("dll-name.dll", CallingConvention=CallingConvention.Cdecl)] public static extern return-type proc-name_ (arg-list);

Syntax for procedure calls using the stdcall2 convention:

[DllImport("dll-name.dll")] public static extern return-type proc-name (arg-list);

Where:

dll-name.dll is the pathname\file-name of the unmanaged (Win32) DLL return-type is "void" if calling a Fortran subroutine, otherwise the C# equivalent of the Fortran function return type proc-name is the case-sensitive procedure name. If the cdecl convention is used, a trailing underscore must be appended to the procedure name arg-list is a managed code variable list with types mapped to Fortran dummy argument types; precede pass-by-reference parameters with "ref"

Calling LF95 DLLs from Microsoft Visual Basic .NET

For DLL's using the cdecl convention, declare the Fortran procedure in the VB.NET code using the following syntax:

Calling a function:

Class ClassName <DllImport("dll-name.dll", CallingConvention:=CallingConvention.Cdecl)>_ Shared Function proc-name_ (arg-list) as return-type End Function End Class

Calling a subroutine:

Class ClassName <DllImport("dll-name.dll", CallingConvention:=CallingConvention.Cdecl)>_ Shared Sub proc-name_ (arg-list) End Sub End Class

For DLLs using the stdcall2 convention, declare the Fortran function in the VB.NET code using the following syntax:

Calling a function:

Class ClassName <DllImport("dll-name.dll", CallingConvention:=CallingConvention.StdCall)>_ Shared Function proc-name (arg-list) as return-type End Function End Class

Calling a subroutine:

Class ClassName <DllImport("dll-name.dll", CallingConvention:=CallingConvention.StdCall)>_ Shared Sub proc-name (arg-list) End Sub End Class

For DLLs compiled using the -ml msvb option, declare the Fortran function in the VB.NET code using the following syntax:

Calling a function:

Class ClassName Declare Auto Function proc-name Lib "dll-name.dll" (arg-list) as return-type End Function End Class

Calling a subroutine:

Class ClassName Declare Auto Function proc-name Lib "dll-name.dll" (arg-list) End Function End Class

Where:

dll-name.dll is the pathname\file-name of the unmanaged (Win32) DLL return-type is the VB.NET equivalent of the Fortran function return type proc-name is the case-sensitive procedure name. If the cdecl convention is used, a trailing underscore must be appended to the procedure name arg-list is a managed code variable list with types mapped to Fortran dummy argument types; precede pass-by-reference parameters with "ByRef"

Calling LF95 DLLs from Microsoft Visual C++ .NET

Syntax for procedure calls using the cdecl convention:

[DllImport("dll-name.dll", CallingConvention=CallingConvention::Cdecl)] extern "C" return-type proc-name_ (arg-list);

Syntax for procedure calls using the stdcall2 convention:

[DllImport("dll-file-name.dll"] extern "C" return-type proc-name (arg-list);

Where:

dll-name.dll is the pathname\file-name of the unmanaged (Win32) DLL return-type is "void" if calling a Fortran subroutine, otherwise the C++ equivalent of the Fortran function return type proc-name is the case-sensitive procedure name. If the cdecl convention is used, a trailing underscore must be appended to the procedure name arg-list is a managed code variable list with types mapped to Fortran dummy argument types; precede pass-by-reference parameters with "ref"