Question

I have to create a dynamic DLL library in C++, to substitute old DLL library written in Fortran, without changing the host application (so functions and parameters must stay the same).

I have full specification of all Fortran functions in that library, but what tools (compiler) I need to use, and what is the way of coding DLL in this situation (stdcall, cdecl, dllexport etc. - that clues doesn't say much to me, I never created a DLL before).

This is sample Fortran function declaration in legacy DLL:

SUBROUTINE SetBoundaries(MaxFlow, MinFlow)
cDEC$ ATTRIBUTES DLLEXPORT :: SetBoundaries
cDEC$ ATTRIBUTES ALIAS: "SetBoundaries" :: SetBoundaries

REAL MaxFlow
REAL MinFlow

I tried to compile VC2008 DLL but got error:

Unhandled error in 'InitAllPublicVars' Run-time error 453: Can't find DLL entry point DLLVersion in SomeLib.DLL

Source of this DLL (defining function named DLLVersion doesn't help):

void __stdcall SetBoundaries( float *min , float *max ) {
}

Is DLLVersion some special DLL procedure or just lack in my documentation and I should create such function?

I know no details about original Fortran DLL source/compilation process, but there are some extracted informations:

General Some options Imports

Was it helpful?

Solution 2

I found a solution. DLL functions should be declared like this:

extern "C" void __declspec(dllexport) SetBoundaries( int min , int max ) {

}

Note that parameters are not pointers. I logged function calls and DLL gets valid numbers from the host application when put instead of original Fortran DLL. Compiled with Visual Studio 2008.

OTHER TIPS

The runtime library makes the Fortran DLL look like it was built with the Compaq or Digital Fortran compilers. By default those compilers used the stdcall calling convention. For your specific example the two arguments (parameters on the C side) are equivalent to float *, subroutines are equivalent to void functions. You would need to use a C++ compiler that supports stdcall. How you nominate that the function is exported in the DLL depends on your C++ compiler or personal preference, but some sort of dllexport keyword is typical.

From the point of view of change management, it would be far simpler to write the replacement DLL in Fortran and use something like the Intel Fortran compiler, which, being a descendent of the CVF compiler, has appropriate legacy support. Changes to the DLL source code can then be made in a progressive fashion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top