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

Calling a Fortran created DLL Function from Visual Basic - Round 2

Bruno_Repetto
Beginner
1,420 Views
I have looked at the replies here: http://software.intel.com/en-us/forums/showthread.php?t=53420 I followed what was said therein, but I still get the error that was mentioned there.

I am using Windows 7 64-bit; Intel Visual Fortran, latest version (11.1.065) within Visual Studio 2008 Professional Edition, and EXCEL 2007.

This is what I did:

1. In Visual Studio, I created a new project for building a Dynamic-link library.
2. I inserted this code:

double precision function pppp(a,b)
!DEC$ ATTRIBUTES STDCALL,REFERENCE,ALIAS:"pppp" :: pppp
double precision a,b
pppp=a*b
return
end
(i.e., I followed most of the recommendations in the that thread.)

3. I then compiled the Release version of the DLL, which produced no errors or warnings. The full path name of the DLL file is "C:\\Work\\For\\pppp\\pppp\\Release\\pppp.dll", which I used in the next step.

4. I opened a new spreadsheet in EXCEL, and in VB I entered the following:

Declare Function pppp Lib "C:\\Work\\For\\pppp\\pppp\\Release\\pppp.dll" (a As Double, b As Double) As Double

Sub test()
a = pppp(2, 3)
End Sub

When I ran it, it produced the same error the original poster got, namely:

Runtime error '453'
Can't find DLL entry point pppp in C:\\Work\\For\\pppp\\pppp\\Release\\pppp.dll

I don't know how to get around this. Can anybody help??

Thanks in advance,

Bruno Repetto, PhD

Calling a Fortran created DLL Function from Visual Basic
0 Kudos
4 Replies
DavidWhite
Valued Contributor II
1,420 Views
I have always found it more reliable to use SUBROUTINE calls instead of FUNCTIONS in this situation.

My code includes:


VBA:

Private Declare Sub WaterDensity_F Lib "AWAProps.dll" _

(TempC As Double, Value As Double, ByVal Units As String)

Note the ByVal is required to return the character string.
The variable declares are:

Dim Units As String * 20
Dim Value As Double

and then for the actual call:
Call WaterDensity_F(T, Value, Units)

Fortran:

Subroutine WaterDensity_F(TempC, Value, Units)
!DEC$ ATTRIBUTES DLLEXPORT, STDCALL, ALIAS:'WaterDensity_F' :: WaterDensity_F
!DEC$ ATTRIBUTES REFERENCE :: Units
IMPLICIT NONE
Real(KIND=8), INTENT(IN) :: TempC(1)
Real(KIND=8), INTENT(OUT) :: Value(1)
Character(LEN=20), INTENT(INOUT) :: Units

Note the use of Arrays here. I think this helps because Fortran uses the location not the variable value. Also, note that Units has REFERENCE here, which in VBA it is ByVal.

The ALIAS ensures that the entry point is appropriately decorated and the case preserved for VBA.

the _F on the routine names is not required, but I use it because on the Fortran side, this is a wrapper function which is only used for the VBA entry. The "real" function names are used as entry points for other Fortran calls.

Regards.

David

0 Kudos
Steven_L_Intel1
Employee
1,420 Views
Are you building a 32-bit or 64-bit DLL? It has to be 32-bit for Excel.

Does the EXCEL sample provided in Samples\en_US\MixedLanguage.zip build and run for you?
0 Kudos
Bruno_Repetto
Beginner
1,420 Views
This worked!!!!

I tried your example (filling in for missing instructions with dummy stuff), and it worked!!

So now that I had something that actually worked, I went back to what I had done before and to my embarrassment I found that the DLLEXPORT was missing! In my confusion I didn't notice I had doen away with it. I put it back and everything fell into place quite neatly. I can call my "pppp" function in VB, as well as call it a user-defined function in an EXCEL worksheet.

Thank you very much David!!

Just the little push that I needed.
0 Kudos
Bruno_Repetto
Beginner
1,420 Views
Are you building a 32-bit or 64-bit DLL? It has to be 32-bit for Excel.

Does the EXCEL sample provided in Samples\en_US\MixedLanguage.zip build and run for you?

It is 32-bit. I didn't try the sample, as I have now found what was wrong.

Thanks, Steve!

0 Kudos
Reply