|
The following is an example of mixed-language programming with a Microsoft Visual C/C++ main program calling a Visual Fortran routine in a DLL. You can also do it without the DLL by adding the Fortran source to the same project as the C/C++ source (and omitting the DLL-related directives). The instructions below assume you have Microsoft Visual C/C++ 5.0 or 6.0 installed and sharing the Developer Studio environment with Visual Fortran (requires the same major release of Visual Fortran and Visual C/++, for example V5 of both or V6 of both.)
- Create a "Fortran Console Application" (V6) or "Win32 Console Application" (V5) project for the main program, call it VC-Example. In the V6 project wizard, indicate that an empty project should be created.
- In this project, create a C/C++ source file named "cmain" (the file type will default to .cpp) with the following code:
/* Main program written in C++ that calls a Fortran DLL */
#include <stdio.h>
#include <string.h>
/* Declare the Fortran routine. The following items are of note:
- The "C" attribute prevents C++ name mangling Remove it
if the file type is .c
- The dllimport specification is required
- Fortran routines use the _stdcall interface by default
- Fortran character arguments have a hidden length
argument following the address
- Routine name must be in uppercase to match Fortran.
*/
extern "C" __declspec(dllimport) void _stdcall DLL_ROUT (
int *INT_ARG,
char *STR_IN,
int STR_IN_LEN,
char *STR_OUT,
int STR_OUT_LEN);
void main (int argc, char *argv[])
{
char instring[40];
char outstring[40];
int intarg;
strcpy(instring,"Testing...");
intarg = 123;
/* Call Fortran routine - pass intarg by reference,
pass length of outstring explicitly */
DLL_ROUT(&intarg,instring,strlen(instring),outstring,40);
printf("%s\n",outstring);
}
- Create a new "Fortran Dynamic Link Library" project, check "Add to current workspace" and "Dependency of". Call it VF-DLL.
- In this project, create a Fortran Free-form source file named "dll_rout" (the file type will default to .f90) as follows:
! Fortran part of a C-Fortran DLL example. This
! routine DLL_ROUT is called from a C executable program.
SUBROUTINE DLL_ROUT (INT_ARG, STR_IN, STR_OUT)
IMPLICIT NONE
! Specify that DLL_ROUT is exported to a DLL
!DEC$ ATTRIBUTES DLLEXPORT :: DLL_ROUT
INTEGER INT_ARG
CHARACTER*(*) STR_IN, STR_OUT
! This routine converts INT_ARG to a decimal string.
! appends the string value to STR_IN and stores it
! in STR_OUT. A trailing NUL is added to keep C
! happy.
!
! Note that there are implicit length arguments following
! the addresses of each CHARACTER argument.
CHARACTER*5 INT_STR
WRITE (INT_STR,'(I5.5)')INT_ARG
STR_OUT = STR_IN // INT_STR // CHAR(0)
RETURN
END
- Select Build..Set Active Configuration and set your executable project (VC-Example) as active. Click on the Build button. It should build the Fortran code first, then the C code and link it. (Note that because the DLL project is a subproject, it is built first AND its .LIB is automatically included with the main program.)
- Manually copy the VF-DLL.DLL file from the \Debug (or \Release) subdirectory of the DLL project to the directory containing the .EXE file.
- Run the program. It should display "Testing...00123" in the console window.
» Back to Examples
|