The documentation on this page is taken from the FTN95 User Guide. It is presented here to demonstrate the quality of documentation along with the ease in which .NET code can be used from Fortran.
Calling Fortran from other .NET languages
This example demonstrates many features of FTN95 for .NET and .NET language interoperability. A C# Windows Application makes use of a FTN95 for .NET assembly which makes calls to the .NET Framework to perform drawing operations.
This program requires the Microsoft .NET Framework to be installed in order to compile and run it. You can compile this example program either in Microsoft Visual Studio .NET or from the command line using the supplied batch files.
To compile from Microsoft Visual Studio .NET you just need to load the supplied solution file (Mandelbrot.sln), then choose Build Solution from the Build menu. You can now run the program under Debug mode by pressing F5.
To compile from the command line you can use either of the two batch files included in the Mandelbrot directory. Run builddbg.bat to create a debug build of the program in the Mandelbrot\bin\Debug directory, or run buildrel.bat to create a release build of the program in the Mandelbrot\bin\Release directory.
If compiling this application from the command line you should ensure that the .NET Framework directory is on the system PATH. Running the FTN95 for Microsoft .NET Command Prompt from the start menu will ensure that this is set up correctly.
This example makes use of two languages. A C# Windows Application and an FTN95 Fortran Application Extension project.
To create the Fortran Application Extension project, select New Project from the File menu. One file will be added to the project when it is created. This project uses functions that are available in Windows.System.Forms.dll, you should add this as a reference to the project using Add Reference from the References node. Enter the code below into the source file. This project can now be built using the Build command from the project menu.
To create the C# application, select New Project from the File menu, name the application if desired or accept the default. From the Toolbox add a PictureBox control and a Button control. Double-click the Button control to add an event handler for that button. Add the C# code as shown below. Because the C# application makes a call into the Fortran Application Extension you need to add a reference to the Fortran project. Do this by selecting Add Reference from the References node within the C# application and selecting the Fortran project.
This example demonstrates calling a Fortran assembly from C# and passing .NET objects from the C# application. Methods of these objects are then called by the Fortran code to perform actions on the C# form.
The Fortran source is examined in more detail below.
This module defines a TYPE that will be used by main routine and also declares ASSEMBLY_EXTERNAL aliases for several .NET methods that are used by the main routine. Details of another routine called to setup items for later use such as a color matrix can also be seen
.NET Objects are passed to the Fortran routine as bitmap and picturebox. Their full type can be seen in the OBJECT statements in the code above. An external alias is provided for the routine by use of an ASSEMBLY_INTERFACE statement.
The above is standard Fortran code.
The code above demonstrates the calling of .NET instance methods. The methods called do not return a value and so can be called as a SUBROUTINE. The aliases provided in the module are used and any arguments that are required are passed to the routine. Note that as these are instance methods the object that the method is being called on has to be passed as the first argument to the method.
This Fortran source should be compiled as a Fortran Application Extension and added to the following C# Application as a reference.