Topic: dll callable from VBA for EXCEL 2013

I am a new user of Simply Fortran. I have been programming with Fortran on and off for 40 years. I recently purchased Simply Fortran so I could convert a library of VBA functions and subroutines for EXCEL 2013 to a Fortran dll of callable functions and subroutines. These routines are part of a highly nonlinear thermodynamic model which sometimes takes up to 20 hours to execute. My hope is to dramatically reduce the execution time.

I am running a 32 bit version of EXCEL 2013 using 64 bit Windows 7 Professional running on a Dell Precision 6800 with an Intel Core i7-4800MQ CPU @ 2.70 MHz with 16 GB of ram.

So far, I have had no significant problems converting the VBA code to stand alone Fortran functions and subroutines. I have NOT had any luck in getting the generated dll to work with EXCEL. I have found very little on this topic for GNU Fortran. I have a lot of material on this subject for MS / Compaq / Digital / Intel Fortran. The examples for these versions of Fortran do not work for GNU Fortran. I suspect the problem is mostly related to not having the right compiler switches and or meta-commands in the GNU Fortran and VBA modules.

Any examples or guidance on how to properly call a GNU Fortran function or subroutine form VBA for EXCEL 2013 would be greatly appreciated.

Thanks, Charles

Re: dll callable from VBA for EXCEL 2013

Charles,

The problem with calling a 32-bit Simply-Fortran-generated DLL from VBA normally centers around two issues:

  1. Calling convention

  2. Function decorations

For VBA to use a Fortran DLL (and make sure you've built a 32-bit DLL, of course), the Fortran functions/subroutines must use the "STDCALL" calling convention.  To mark a routine as using this standard, you'll need to add the preprocessor directive to your Fortran code within your function/subroutine body:

!GCC$ ATTRIBUTES STDCALL :: MyFunc

Using the ISO_C_BINDING module is also recommended.  When binding Fortran functions/subroutines as "C" callable, it can automatically configure how arguments are handled.

The decoration problem gets more complicated.  Basically the STDCALL calling convention in Windows for DLLs expect functions within the DLL to appear as something like MyFunc@4.  The Simply Fortran compiler will not generate this style of decoration, though.  I usually suggest explicit use of DECLARE SUB statements in VBA to handle the mismatch of what VBA expects and what Simply Fortran DLLs provide.

If you want to see how your function is actually named in a DLL, you can inspect the symbols present in a DLL by opening it with Simply Fortran (using "Open" in the File menu).  You'll receive a list of all symbols in the DLL in a relatively readable form.  You can find your functions and subroutines by searching by name.  By default, it might appear as "myfunc_" without any special compiler flags.

This thread covers a non-trivial example of how to call a Fortran DLL from VBA.  It should at least get you started in the right direction.

Jeff Armstrong
Approximatrix, LLC