简体   繁体   中英

How can I use a Fortran file in a C# application?

I have Intel® Parallel Studio XE, which offer a Fortran compiler for Microsoft Visual Studio (I use the 2013 Ultimate version). It is possible to execute a Fortran file in a C# application or it must be a C/C++ application? How can I do it?

None of them can use fortran, you must create a fortran project, you can't mix languages. A possible solution is to create a DLL and interface it with DLLImport, this may help you:

https://sukhbinder.wordpress.com/2011/04/14/how-to-create-fortran-dll-in-visual-studio-with-intel-fortran-compiler/

You have two options to call Fortran from C#.

1) Create a Fortran console application (EXE). Invoke from C# using Process.Start and pass input and outputs using files. I would recommend starting with this approach.

var startInfo = new ProcessStartInfo();
startInfo.FileName = "MyFortranApp.exe";
startInfo.Arguments = @"C:\temp\input_file.txt C:\temp\output_file.txt";
Process.Start(startInfo);

2) A more advanced approach is to create a Fortran DLL and call from C# using P/Invoke (DllImport). With a DLL all inputs and outputs are passed in memory. You can also use callbacks to report progress back to the C# calling code.

public static class FortranLib
{
    private const string _dllName = "FortranLib.dll";

    [DllImport(_dllName, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
    public static extern void DoStuff([In] double[] vector, ref int n, [In, Out] double[,] matrix);
}

http://www.luckingtechnotes.com/calling-fortran-dll-from-csharp/ http://www.luckingtechnotes.com/calling-fortran-from-c-monitoring-progress-using-callbacks/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM