如何在C#应用程序中使用Fortran文件?

时间:2016-02-17 14:43:33

标签: c# c++ visual-studio-2013 fortran intel-fortran

我有英特尔®ParallelStudio XE,它为Microsoft Visual Studio提供Fortran编译器(我使用2013 Ultimate版本)。 可以在C#应用程序中执行Fortran文件,还是必须是C / C ++应用程序?我该怎么办?

2 个答案:

答案 0 :(得分:2)

他们都不能使用fortran,你必须创建一个fortran项目,你不能混合语言。一个可能的解决方案是创建一个DLL并将其与DLLImport接口,这可以帮助您:

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

答案 1 :(得分:2)

您可以通过两种方式从C#调用Fortran。

1)创建Fortran控制台应用程序(EXE)。使用Process.Start从C#调用,并使用文件传递输入和输出。我建议从这种方法开始。

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

2)更高级的方法是创建Fortran DLL并使用P / Invoke(DllImport)从C#调用。使用DLL,所有输入和输出都在内存中传递。您还可以使用回调将进度报告回C#调用代码。

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/