How to create a dynamic C ++ DLL to replace the legacy Fortran DLL

I need to create a dynamic C ++ DLL to replace the old Fortran DLL, without changing the host application (so that the functions and parameters must remain unchanged).

I have a full specification of all the Fortran functions in this library, but what tools (compiler) I need to use, and what is the way to code the DLL in this situation (stdcall, cdecl, dllexport, etc.), I say a lot, I never created a DLL before).

This is an example of declaring a Fortran function in an obsolete DLL:

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

I tried to compile the VC2008 DLL but got an error:

Unhandled error in 'InitAllPublicVars' Runtime Error 453: Unable to find DLL entry point DLL in SomeLib.DLL

The source of this DLL (the defining function named DLLVersion does not help):

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

Is DLLVersion some special DLL routine or just missing from my documentation and should I create such a function?

I don't know the details about the source / compilation of the Fortran DLL file, but there is some extracted data:

GeneralSome optionsImports

+4
source share
2 answers

I have found a solution. DLL functions should be declared as follows:

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

Please note that parameters are not pointers. I registered function calls, and the DLL gets real numbers from the host application, if instead of the original Fortran library. Compiled with Visual Studio 2008.

+2
source

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

From a change management perspective, it would be a lot easier to write a Fortran DLL replacement and use something like the Intel Fortran compiler, which, as a descendant of the CVF compiler, has corresponding legacy support. Changes to the source code of a DLL can be made in a progressive way.

+4
source

All Articles