LF Fortran 95  

Visual Basic Information

Visual Basic calling Fortran

To create a DLL that will work with Microsoft Visual Basic, take 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 msvb

Running the Visual Basic Demo

  1. Compile the VBDEMO.F90 file, located in the Win32\Examples\Mix_Lang\MSVB subdirectory, using the -dll -win -ml msvb options.
  2. Ensure that the resulting VBDEMO.DLL resides in a directory that is on your path. Failure to do this will generally result in an "Error loading DLL" message from the operating system.
  3. Start Visual Basic and open the VBDEMO.VBP project in LF95's Win32\Examples\Mix_Lang\MSVB directory.
  4. Run the demo application (F5).

Declaring your Procedure in Visual Basic

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

Private Declare Function my_func Lib "my_dll" (ByRef my_arg As Long) As Long

Private Declare Sub my_sub Lib "my_dll" (ByRef my_arg As Long)

(see the relevant section below if an item on the argument list is either an array or is character datatype). Note that in the example above, "my_dll" must specify a complete path in order to operate within the Visual Basic Environment.

Passing Character Data in Visual Basic

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

Character (string) arguments and hidden length arguments must be passed by value, i.e., declare the procedure's arguments (actual and hidden) with the ByVal keyword. Refer to the example VBDEMO program. The following restrictions apply:

Passing Arrays in Visual Basic

When passing an array from Microsoft Visual Basic you will need to declare the argument as a scalar value in the Basic declaration, and pass the first element of the array as the actual argument. Declare the array dummy argument normally in the Fortran procedure. Note that the default lower bound for arrays in Visual Basic is 0, so you may find it helpful to explicitly declare your Fortran arrays with a lower bound of 0 for each dimension, or explicitly declare your Basic arrays to have a lower bound of 1 (this can be done at the module or procedure level via the Option Base statement). Note also that arrays of strings cannot be passed from Visual Basic to LF95.