|
Visual
Basic DLLs
This page describes
how to use the Absoft Fortran compilers and linker to create DLLs
that are callable from Microsoft Visual Basic. A DLL, Dynamic Link
Library, is a library of routines that are callable at runtime from
any application that conforms to the Windows API.
You must be using a 32-bit version of Visual Basic, such as Visual
Basic V4.0, to call Absoft Fortran DLLs.
Creating The Fortran DLL
Write your FORTRAN source code in the usual manner, declaring the
program unit as either a subroutine or a function. Insert the additional
keyword STDCALL before each SUBROUTINE or FUNCTION keyword. Adhere
to the normal FORTRAN CALL/RETURN sequences and argument passing
rules. For example, consider the following subroutine
stdcall subroutine DegreeSin(input, output)
implicit none
double precision input
double precision output
output = dsind(input)
return
end
This subroutine simply computes the double precision sin in degrees
of the input argument and returns the result in the output argument.
Compile the file normally, using the -c option which instructs the
compiler to produce an object file only.
Next, create a new linker control file containing the names of the
program units that will be placed in the DLL. This file should contain
only those names in the library that will be externally visible.
If the DLL contains any programs units that are used only by the
routines within the DLL, they should not be placed in this file.
In the case of the above example, we would create the file, "DegreeSin.xps",
containing the single line:
DegreeSin
You must also create a second new linker control file containing
the aliases for the internal names that the compiler generated for
your subprogram names. The compiler creates internal names to avoid
conflicts with other global symbolic names. It does this by prepending
an underscore to the name and, in the case of STDCALL subprograms,
appending the size of the call stack to the name. The size of the
call stack will be equal to 4 times the argument list count. In
the case of the DegreeSin subroutine, this value will be 8. This
value is separated from the subprogram name with a commercial at
sign (@) since the subprogram name may legally end with a number.
In the case of the above example, we would create the file, "DegreeSin.als",
containing the single line:
_DegreeSin@8 DegreeSin
which says that the internal name, _DegreeSin@8, will be known externally
as DegreeSin.
To create the DLL, we would enter the following line on the MS-DOS
Prompt command line:
lnk /dll DegreeSin.obj /exports:DegreeSin.xps frt0.lib ntf77dll.lib
kernel32.lib /aliases:DegreeSin.als
The three .lib file references allow the linker to resolve any runtime
library routines that may needed. That is, your DLL may itself reference
other DLLs.
Creating the Visual Basic code
Complete documentation about calling Fortran DLLs from Microsoft
Visual Basic can be found in the Microsoft Visual Basic Programmer's
Guide. Refer to Chapter 26, Calling Procedures in DLLs. This section
will describe the basics of referencing a Fortran Subroutine or
Function.
The first step is to declare the Fortran subprogram in your Basic
program. The declaration for the subroutine discussed in the previous
section would be:
Declare Sub DegreeSin Lib "c:\...\DegreeSin.dll" (inval As Double, _
outval As Double)
The string following the Lib keyword should be the path to the DLL.
The actual reference to the Fortran subroutine is simply:
Dim inval As Double
Dim outval As Double
.
.
.
Call DegreeSin(inval, outval)
|