LF Fortran 95  

Borland Delphi Information

Delphi Calling Fortran

To create a DLL that will work with Borland Delphi, take the Fortran source (without a main program) and indicate the procedures that you want available in the DLL with the DLL_EXPORT statement, then invoke the LF95 driver like this:

LF95 source.f90 -win -dll -ml bd

Running the Delphi Calling Fortran Demo

  1. Compile the BDDEMO2.F90 file located in the Win32\Examples\Mix_Lang\BD subdirectory using the -dll, -win , and -ml bd options.
  2. Ensure that the resulting BDDEMO2.DLL resides either in the current working directory, or in a directory that is on your path. Failure to do this will generally result in an "Debugger Kernel Error" message from the operating system.
  3. Start Delphi and open the BDDEMO2.DPR project in the Win32\Examples\Mix_Lang\BD subdirectory.
  4. Run the demo application (F9).

Fortran Calling Delphi DLLs

Before building the Fortran main program with LF95, you must have a DLL and import library available. Refer to your Delphi documentation for the specifics on creating a DLL. Because Delphi does not build a .LIB file for the DLL, and does not create compatible object files, the stub method must be used to create a Microsoft-compatible import library. See Building import libraries when no object file is available. An example of linking a Fortran program to a Delphi DLL appears in the Win32\Examples\Mix_Lang\BD subdirectory.

When you create a Fortran procedure that references a Delphi DLL procedure you declare the Delphi procedure name with the DLL_IMPORT attribute in your Fortran code. The procedure may be a subroutine or function. Delphi DLL functions may only return the 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
  

Build the Fortran program using the -ml bd option:

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

Where dll_src.lib is the name of the Microsoft compatible import library created by the stub method.

Running the Fortran Calling Delphi Demo

  1. From Delphi, open F95CALLBD.DPR in LF95's Win32\Examples\Mix_Lang\BD subdirectory.
  2. Build the DLL by pressing Ctrl-F9.
  3. Copy F95CALLBD.DLL to LF95's Win32\Examples\Mix_Lang\BD subdirectory.
  4. Change to LF95's EXAMPLES\MIX_LANG\BD directory.
  5. Run the batch file RUNF95CALLBD.BAT. This batch file compiles the Fortran stub code, generates an import library, and compiles the Fortran main program using the newly created import library.
  6. The resulting executable, F95CALLBD.EXE is automatically run by the batch file.

Declaring your Procedure in Delphi

In your Delphi code, a procedure's declaration will be like one of the following examples:

function my_LF95_function(var my_arg: LongInt) : LongInt; 
  stdcall; external `my_dll.dll';
  
procedure my_LF95_subroutine( var my_arg: Single); 
  stdcall;
  external `my_dll.dll';
  

(see the relevant section below if an item on the argument list is either an array or is character datatype).

Passing Character Data in Delphi

Character arguments are passed as strings with the length of each string appended at the end of the argument list.

Delphi has two kinds of strings: long strings and short strings, where a long string can contain a very large number of characters and its length varies dynamically as needed, and a short string has a specified length and may contain up to 255 characters. If your character argument is a short string you should use the var keyword in your procedure's declaration; omit the var keyword if your argument is a long string. Refer to the BDDEMO and BDDEMO2 programs to see examples for both of these cases.

As of this writing, the following conditions apply:

Passing Arrays in Delphi

Because Delphi processes multi-dimensional arrays as an array of arrays (like C and C++) and Fortran processes arrays as multi-dimensional arrays, there are some special considerations in processing a Fortran array. Refer to Passing Arrays in C or C++ for more information.