Using FORTRAN in your DLL

It is possible to wrap compiled FORTRAN code in C or C++, allowing Mathcad to run it. In this way, legacy FORTRAN code can be reused as a Mathcad function. We have had success compiling with Compaq Visual Fortran (formerly MS Fortran) and MS Visual C++ 6 or 7.

First, follow the directions for Compiling and Linking your DLL to set up the Visual Studio environment with all the appropriate settings for creating a UserDLL. Next, create a static library from the FORTRAN subroutine. Link the C code to the FORTRAN code, and compile a DLL in the usual manner for Mathcad use.

Create the Fortran Library

  1. Create a new "FORTRAN Static Library" project in Compaq Visual Fortran 6.6B, and add your files to the project.

  2. Under the Build menu, select "Set Active Configuration" and choose "Release."

  3. Compile and build. The Release subfolder of your FORTRAN project now contains [myfortranlib].lib.

Modify the C code for the UserDLL

Once you have the FORTRAN library, you need to add in your C code:

extern void __stdcall FORTRANFUNCTION(const double *array1, const double *scalar1 [, etc.]);
// Since FORTRAN typically expects arguments by reference, arguments are passed
// as pointers. To pass values, the FORTRAN code must contain a compiler directive
// telling the function to expect a value rather than an address.

LRESULT mcadfunction(LPCOMPLEXARRAY array1, LPCCOMPLEXSCALAR scalar1, etc.)

// this defines the function before FUNCTIONINFO, using the same variable names
// called by the external FORTRAN function.

{

// some error checking goes here, followed by the
// actual call to the FORTRAN function. For example,

FORTRANFUNCTION(&array1->hReal[0][0], &scalar1->real [, etc.]);

// Either the function call must be in UPPERCASE, or you will have to set
// Settings->FORTRAN->External procedures->External name implementation
// to "Upper case" in your FORTRAN compiler. Any other C functionality follows...

return 0;

}

Link the Library

  1. In an existing C project in MS Visual C++ or Visual Studio, select the Link tab and then Input from the Category dropdown menu. Enter "[myfortranlib].lib" in the "Object/library modules" textbox at the end of the list.
  2. Enter "libc.lib" in the Ignore libraries text box.
  3. Enter the path of the [myfortranlib].lib file in the Additional Library Path text box, for example, "C:\temp."
  4. Compile and build the DLL.

Notes: