3D Ohio Logo

FORTRAN 77 and C# Combined

Required:
Microsoft Visual C# 2005 Express Edition or VS 2005
Open Watcom

So you have a need to take legacy unmanaged code and link it with managed code. Doing this is actually more common than one may expect. Although it does break many of the nice things with .NET and anchors the platform to Windows on x86 – but it sure beats rewriting code

1. Make a new FORTRAN project in Watcom – I used “csharp”
2. Create a new source file (.FOR) – I used “csharp.for”
3. Enter a FORTRAN function

*$pragma aux returnDecay "returnDecay"

      real*8 function returnDecay()
C Computes the decay of a radioactive substance and returns
C the value to C#

C Declarations
      REAL C
      PARAMETER (C = 0.693 / 28.0)
      REAL SUBNEW, SUBOLD, TIME

C Read initial population and time.
      PRINT *, 'Enter the initial amount of Strontium 90'
      READ *, SUBOLD
      PRINT *, 'Enter the time in years'
      READ *, TIME

C Computer new population amount.
      SUBNEW = SUBOLD * EXP(-C * Time)

C Set Result and Send back to C#
      returnDecay = SUBNEW
      RETURN
      END

4. We’ll need to change some linker settings. Linker settings on page 2. Import, Export and Library Switches – we’ll have to add the function(s) that we’re exporting.
Watcom Linker page 2
5. Next we’ll need to change the linker settings on page 4. Special Switches – if you don’t do this, you’ll get a “Warning! W1058: manyautodata option not valid for a Windows NT dynamic link library”
Linker setting
6. Make the DLL file. A successful message will look something like this:

cd C:\Documents and Settings\danand\My Documents\Fortran\csharp
wmake -f “C:\Documents and Settings\danand\My Documents\Fortran\csharp\csharp.mk” -h -e “csharp.dll”
wlink name csharp d all SYS nt_dll op m exp retrunDecay op maxe=25 op q op symf @csharp.lk1
wlib -q -n -b csharp.lib +csharp.dll
Execution complete

7. We’re done with Watcom, the rest is in C#. Enter the following code:

using System;
using System.Collections.Generic;
using System.Text;
// Need InteropServices for DLL integration
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        // Set Import DLL
        [DllImport("csharp.dll")]
        static extern double returnDecay();

        static void Main(string[] args)
        {
            Console.WriteLine("The amount remaining is " + returnDecay());
            Console.Write("Press any key to continue");
            Console.ReadKey(false);
        }
    }
}

Note that you’ll need the InteropServices to link the DLL and all functions need to be declared as “static extern”. You’ll also have to add the DLL to the project – or at least copy it to the debug directory for the project.

Success will leave you with a screen like this:

Enter the initial amount of Strontium 90
10.0
Enter the time in years
28.0
The amount remaining is 5.00073575973511
Press any key to continue

You can download the Fortran and the C# code / projects here: SOURCE

3 Responses to “FORTRAN 77 and C# Combined”

Yang, Seung-Ie Says:

Dear Sir:

I really appreciate your web page. I got a lot of things I need to do.

I have a question.

Is there more examples which connect the C# to FORTRAN.

If you have, please send it to me by e-mail.

Best regards,

Yang, Seung-Ie

Yang, Seung-Ie Says:

ww

Shailendra Singh Says:

Great information on this page …
It would be of great help .. if you could suggest similar example for subroutine and its global variable
Thanks

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image

Wordpress Feedburner Valid XHTML 1.0 Transitional Valid CSS!
copyright 2007 3D Ohio