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
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.
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.
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).
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:
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.