LF Fortran 95  

Fortran Calling C DLLs

When you create a Fortran procedure that references a C procedure you declare the C procedure name with the DLL_IMPORT attribute in your Fortran code. The procedure may be a subroutine or function. C functions may only return the Fortran equivalent of default INTEGER, REAL, or LOGICAL results.

Example code:

program main
  implicit none
  real, dll_import :: My_Dll_Routine ! case-sensitive
  real             :: x
  x = My_Dll_Routine()
  write (*,*) x
end program main

Before building the Fortran main program with LF95, you must have a DLL and import library available. Refer to your C manual for specifics on creating a DLL. If the C compiler does not create a compatible import library (.LIB file) for the DLL, proceed as described above, see Building Import Libraries.

If the DLL was created with Microsoft Visual C++, use the -ml msvc option:

LF95 source.f90 -win -ml msvc dll_src.lib

If the DLL was created with Borland C++, use the -ml bc option:

LF95 source.f90 -win -ml bc dll_src.lib

Where dll_src.lib is the name of the Microsoft compatible import library.

There are examples of calling C DLLs in the Win32\Examples\Mix_Lang directory below the Fortran installation directory.