Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Excel VBA and Fortran DLL

trembl27
Beginner
532 Views
Hi!

I've been reading a lot of post but cannot find the solution to the problem I'm facing.

I have a Fortran DLL that I've been using for a few years in Excel. It was compiled iwth the Lahey-Fortran compiler.

Now that we started integrating fortran DLL in Matlab, I switched to Intel Visual Fortran as it's the only compatible compiler for this software.

I thought it would be a good idea to get rid of the other compiler and "standardize" every fortran DLL on IVF. However, I now face some challenge with Excel / VBA.

Here is a small example :

VBA :

Private Declare Sub CALLBIEFAV Lib "mcph.dll" (IC As Long, ByRef Debit As Single, ByRef VAL As Single, ByRef ELAV As Single)

Private Function BIEFAV_Generic(inIC, inDebit, inVal)
Dim IC As Long
Dim Debit As Single, VAL As Single, ELAV As Single
IC = inIC
Debit = inDebit
VAL = inVal
Call CALLBIEFAV(IC, Debit, VAL, ELAV)
BIEFAV_Generic = ELAV
End Function


Fortran :

SUBROUTINE CALLBIEFAV(IC,DEBIT,VAL,ELAV)
!DEC$ ATTRIBUTES STDCALL, DLLEXPORT :: CALLBIEFAV
!DEC$ ATTRIBUTES ALIAS : "CALLBIEFAV" :: CALLBIEFAV

CALL BIEFAV(IC,DEBIT,VAL,ELAV)

RETURN
END



When I attach Excel.exe to the VS2008 debugger, I can see what's happening.

The values that IC, DEBIT, VAL and ELAV hold are not right.

IC = 1294032
DEbit = 1.8133194E-39
Same for the others.

Looks to me like I'm not sending to right "address" or type of variable. However, everything is casted as LONG/SINGLE in VBA and INTEGER/REAL in Fortran. In the compiler option, I see that the default real kind is 4.

And to add to the insult, I pass my arrays from VBA to Fortran perfectly.

Here is another example :

Private Declare Sub CALLQMW Lib "mcph.dll" (IC As Long, QTOT As Single, ELAM As Single,
V_SUP As Single, AVAL As Single, NUD As Long, NOROUE As Long, U As Long,
GEN As Single, QTURB As Single, QTU_U As Single, PGEN As Single)

Some of these parameters are arrays as you can see when I call it from my code :

...
Dim NOROUE(16) As Long
Dim U(16) As Long
...
Call CALLQMW(IC, QTOT, ELAM, V_SUP, AVAL, NUD, NOROUE(1), U(1), GEN, QTURB, QTU_U(1), PGEN(1))
...

When I check in the debugger, all my arrays values are perfect on the DLL side.

I don't feel like creating one-element arrays for everything =)

If there is a link for simple example, that would be great!

Thanks a lot!

Francois





0 Kudos
1 Reply
Steven_L_Intel1
Employee
532 Views
Change:

!DEC$ ATTRIBUTES STDCALL, DLLEXPORT :: CALLBIEFAV

to:

!DEC$ ATTRIBUTES STDCALL, REFERENCE, DLLEXPORT :: CALLBIEFAV
0 Kudos
Reply