Calling a Win32 DLL from unmanaged Visual C++
Exporting routines from a Win32 Fortran DLL
Debugging a FTN95 Win32 DLL within Visual Studio .NET
<Documents Folder>\FTN95 Examples\Win32\VisualCInteroperability
A set of shortcuts has also been provided in the start menu.
This example demonstrates the use of an FTN95 Win32 DLL called from unmanaged Visual C++. This examples requires the use of the Microsoft Visual C compiler, cl.exe. This is installed by Visual Studio .NET. To build this example the solution file can be loaded (VisualCInteroperability.sln) or the project can be built from the command line by using the supplied batch files present in the solution folder. To compile this application from the command line you should ensure that the Visual C++ compiler is available for use. This document refers to the creation of this project using Microsoft Visual Studio .NET, for command line usage much of this document applies, but please see the batch files for command syntax details. The Visual Studio .NET environment batch file vsvars32.bat should have been run for command line usage.
This sample assumes the
__cdecl calling convention is used.
For an example of calling a Visual C++ Win32 DLL from Silverfrost Fortran see the VisualCInteroperability2 example.
This solution makes use of two language projects, an FTN95 Application Extension and a Visual C++ Win32 Console Application.
To create this solution two projects need to be created. First create the FTN95 Application Extension solution by selecting New Project from the File menu and choose a FTN95 Application Extension project. Name the project as desired or accept the default. When the project has loaded the configuration should be changed from .NET to Win32 if this is not already set. This can be done by selecting the project type combo box in the toolbar within Visual Studio .NET.
Enter the Fortran code as shown in the Code Features section below.
Select Properties from the Project menu. When creating a Fortran Application Extension project the default file type produced by a build is a DLL. As this project is to be used with Microsoft Visual C++ a .lib file has to be produced. In the project properties dialog, navigate to Compiler Options->Miscellaneous. In the Output Options section, select Output filetype and change the extension, using the dropdown available, to LIB as shown in the following image.
Select Linker Options in the same dialog and set Export all to Yes. This ensures that any routines that are included in the library produced by the project will be exported and callable from Visual C++. If only select routines need to be exported then a custom link script can be passed to the linker by creating a script and passing the script to the linker by specifying the script file in Extra linker options. A short description of how to do this for this project is shown below.
Using a Custom Link Script
If more options are required to be passed to the linker than are provided in the project configuration dialog then a custom link script can be provided. A blank text file should be created in the project directory and named as appropriate. For this example the file will be called extras.slink. To export specific routines from a DLL instead of all routines by use of Export all then specific Export commands can be passed to the linker. To export four specific routines from the code below the following linker script could be used:
These exported routines can be accessed from the C program in the same way as with the use of Export all.
It is possible to provide alternate export names for the routines by the use of the following syntax within the script file:
This routine is then exported as
_SubSimple and should be treated
as such within the C program.
To specify the script file to the linker, the name of the script file should be prefixed with '$' and added to Linker Options->Extra linker options as shown in the following image. If specific exports are used as shown then Export all should also be set to No. Note that if the linker script is not resident within the project directory then a full path or a path relative to the project directory should be specified.
For more information about custom link scripts and the commands that can be used with them please see the following topics.
Example Creation (continued)
When the Fortran project has been created, select Build from the Build menu. In the project output directory two files, a .lib and a .dll should have been created. The .lib file will be used by the Visual C++ project at build time, and the DLL will be used at runtime.
To create the Visual C++ project, select New Project from the File menu and select a Win32 Console Project from Visual C++ Projects. Name the project as appropriate and create the project. You should add the new project to the existing solution.
Enter the C code as shown in the Code Features section below.
Two changes need to be made to the Visual C++ project settings. Select Properties from the Project menu for the C++ project. Navigate to Linker->Input and select Additional Dependencies. The .lib file created by the Fortran project should be specified here. A path should be specified, using either a full or relative path. For this example the Fortran Application Extension and Visual C++ Project are in seperate parallel directories and therefore (in Debug/Win32 configuration) the path to the .lib file would be:
Now we have ensured that the Visual C++ program has enough information to compile and link with the Fortran Application Extension, we need to ensure that the Fortran DLL is available for the Visual C++ program at runtime. This is accomplished by use of a Post-Build Event. When the Visual C++ program runs the Fortran Application Extension DLL needs to be available either in a local directory or on the system path. To make the DLL available locally a custom event can be used to copy the DLL from the Fortran output directory to the Visual C++ output directory. Still within the project properties navigate to Build Events->Post-Build Event and select Command Line. Enter the following command:
Ensure that the solution build order is configured so that the Fortran Application Extension is built before the Visual C++ project. The entire solution can now be built. The solution should also be runnable from the Debug menu. Ensure that the Visual C++ project is set as the startup project.
This example demonstrates calling an FTN95 from Visual C++. A variety of routines are called, with different arguments and return types, array passing and function pointers are demonstrated. Explanations are provided after each code snippet below.
The above are three simple Fortran subroutines that accept different arguments. When passing the strings to the Fortran routines, as demonstrated by the last example, the lengths of the two strings are passed as extra arguments to the Fortran, see the C code for full details of this.
The first of these two routines accepts an array from the C code. The size of the array is passed, by value from the C as an extra argument. The second subroutine illustrates the passing of a function pointer as an argument. func is a function pointer passed from C and then called from within the Fortran code.
The code above shows two Fortran functions returning different types.
For details of type differences between Fortran and C see the following topic:
Visual C++ Code
These declarations should be provided to specify the Fortran routines that are to be called. Two cirumstances should be considered. If Export all was specified from the Fortran then the names should match those in the Fortran code but should be all upper-case, regardless of the case within the Fortran code. If explicit export statements have been provided by a link script then the names should match those provided in the link script. extern "C" should be specified to prevent Visual C++ from modifying the names of the routines in any way.
The first routines above demonstrate calling FTN95 DLL subroutines with no arguments and with simple arguments. Note that arguments are always passed by reference.
The second two routines demonstrate calling subroutines that require extra arguments to be passed along with the actual arguments. These two subroutines accept assumed length character arguments and an assumed size array. The lengths/sizes of these arguments should be passed from the C as extra arguments on the end of the argument list, by value and in the order in which the arguments requiring extra arguments are passed.
The above demonstrates calling functions in the Fortran DLL.
The above routines are used to pass a function pointer to the Fortran. Within the Fortran the argument is specified as EXTERNAL.
Debugging an FTN95 DLL within Visual Studio .NET
Normally, to run this example the Visual C++ Project should be set as the startup project. To debug the Fortran DLL we need to go through extra steps as mixed language debugging with FTN95 and Visual C++ is only available in .NET configuration.
In order to debug the Fortran DLL the following steps need to be taken. Firstly, set the Fortran Application Extension as the startup project by right clicking the project in the Solution Explorer and selecting Set as StartUp Project. Enter the properties dialog for the Fortran Application Extension and select the Miscelleneoustab. Enter the full path to the Visual C++ Exe. Any time a debug target other than the main project output for a project is required then the full path should be entered. See the following image.
Click OK. Breakpoints can now be set inside the Fortran DLL and the debugger will break when execution hits these points. Alternatively Debug->Step Into can be used to break at the first executable statement that is reached in the Fortran DLL.