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

call function in c++ dll from Fortran program

cfann61
Beginner
1,127 Views
I have a c++ .dll I didn't write, and I want to call the functions in the .dll from my Fortran program. I've seen a few different methods, but can't seem to get anything to work. Code would look something like:

!DEC$ ATTRIBUTES DLLIMPORT, STDCALL, ALIAS : '_AirPTBW' :: AIRPTBW !AirPTBW being the c++ function

REAL*8 a, b, c, d
INTEGER n

d = AirPTBW(a, b, c, n)

I ran dumpbin.exe on the .dll, and the name listed for this specific function is AirPTBW.

There is also a library associtated with the .dll, 'AIRPRP32.lib'. I'm a little confused as to whether I need to include the .lib file somewhere and/or add: !DEC$ DLLIMPORT :: AIRPRP32

I keep getting an error, unresolved external _AirPTBW if I include the .lib file in Confi->Linker->Input->Additional Dependencies. Could it be looking for _AirPTBW, while the .dll outputs it as AirPTBW? If so, how can I fix this?
0 Kudos
4 Replies
Steven_L_Intel1
Employee
1,127 Views

Do you know for sure that it uses a STDCALL conventiion?

If the problem is really the underscore, just remove it from the ALIAS part of the ATTRIBUTES directive, Keep in mind that saying STDCALL changes the default to pass-by-value. You really need to know the calling interface of the routine in order to be successful - guessing may get you into trouble.
0 Kudos
cfann61
Beginner
1,127 Views
I'm not sure if it uses STDCALL convention or not. The .dll came bundled with a package that included an .exe, and add-in for excel. The excel add-in has been working fine, and now I want to use the .dll to perform those same functions in my program. The whole thing is older, from '97.

I've added the .lib file to additional dependencies, put the .lib file in a location where it will be found, and changed the compiler directive to:

!DEC$ ATTRIBUTES ALIAS : 'AirPTBW' :: AIRPTBW

Getting error: "unresolved external symbol AirPTBW"

Whole thing looks like this:

program Console1

!DEC$ ATTRIBUTES ALIAS : 'AirPTBW' :: AIRPTBW

implicit real*8 (A-Z)

integer n

a = 14.629D0
b = 83.262D0
c = 72.821D0

d = AirPTBW(a, b, c, n)

end

0 Kudos
Steven_L_Intel1
Employee
1,127 Views
What does dumpbin -exports on the .lib say?
0 Kudos
cfann61
Beginner
1,127 Views
COMPILED! Thanks! I didn't know the .lib file would have something different. Now just need to make sure the .dll is working properly. Thanks again.

0 Kudos
Reply