Calling NAG Fortran Library (DLL) from C#

The NAG Fortran Library consists of over 1500 routines. Potentially these routines can all be accessed in the .NET environment using the P/Invoke facilities provided by Microsoft. The aim of this paper is to illustrate the mechanism of calling the NAG Fortran Library routines from C# (and thereby from .NET generally) by providing a representative set of Fortran example programs translated into C# .

The Fortran Library uses the following data types as parameters. These are:

  • scalars of type DOUBLE PRECISON, INTEGER and COMPLEX*16.

  • arrays of type DOUBLE PRECISION, INTEGER and COMPLEX*16.

  • character strings and arrays of character strings

  • function parameters, also know as callbacks, that are pointers to functions with particular signatures.

Our principal task here is to map these types to C#.

In C# we distinguish between parameters as being passed either by reference or by value. In Fortran all parameters are passed by reference only. Hence every scalar parameter to a Fortran routine has to be qualified by the keyword ref. This does not apply to arrays as arrays are passed by reference in C#. The example programs below demonstrate this requirement.

There is one further consideration that we have to bear in mind as far as arrays are concerned. In Fortran two dimensional arrays are stored contiguously in memory by columns while in C# two dimensional arrays of the type [,] are stored contiguously by rows. It follows therefore that before a two dimensional C# array is passed to a Fortran routine as a parameter, it has to be transposed. See f07abfe.cs

We note that the complex type as such does not exist in C#. A complex number Z may be represented as an ordered pair of real numbers consisting of the real part of Z and the imaginary part of Z. In Fortran this is exactly how a complex type is stored and so we may utilise this observation to pass information from and to C#. Specifically we may use a C# double array to hold the real and imaginary parts (in that order) of complex numbers and pass this into the NAG DLL to represent a complex array or complex number. It might seem more elegant to define a structure with two double elements to represent the complex type, but we have found in practice that this works only for input data. We may pass complex data into the Fortran DLL in this manner, but cannot obtain information back from the DLL via this structure. See f07apfe.cs

Character strings can be declared as the string type in C#. The strings have to have a fixed length in C# and the length of the string has to be passed as an additional parameter. Character arrays in Fortran have contiguous storage and so cannot be represented as string arrays. We have found that we can mimic the Fortran character array using byte arrays. See g02eafe.cs

Function parameters in Fortran map to the delegate type in C#. Array parameters in the delegate type require the use of the C# IntPtr type and the Marshall class. See e04ucae.cs

We have provided C# classes based on NAG Fortran examples as shown in the Fortran Library manual to illustrate various parameter mappings. For example a sample C# class calling e04uca can be found in e04ucae.cs. If there are particular routines whose signatures cannot be determined from the information we have provided please contact us. We will try our best to help.

Example 1
Parameter features: double scalars, simple callback
Example 2
Parameter features: double arrays, more involved callback
Example 3
Parameter features: double arrays, involved callback with 2-D double arrays
Example 4
Parameter features: 2-D double arrays
Example 5
Parameter features: 2-D double arrays, character string
Example 6
Parameter features: 2-D complex arrays, character string
Example 7
Parameter features: 2-D double arrays,
Example 8
Parameter features: string array,
Example 9
Parameter features: double scalar,

In addition to the above we have provided the following examples:

Website Feedback


(If you're a human, don't change the following field)
Your first name.
Enter the characters shown in the image.