简体   繁体   English

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

[英]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). 我有英特尔®Parallel Studio XE,它为Microsoft Visual Studio提供了Fortran编译器(我使用2013 Ultimate版本)。 It is possible to execute a Fortran file in a C# application or it must be a C/C++ application? 是否可以在C#应用程序中执行Fortran文件,或者它必须是C / C ++应用程序? How can I do it? 我该怎么做?

None of them can use fortran, you must create a fortran project, you can't mix languages. 他们都不能使用fortran,您必须创建一个fortran项目,不能混合语言。 A possible solution is to create a DLL and interface it with DLLImport, this may help you: 一个可能的解决方案是创建一个DLL并将其与DLLImport接口,这可能对您有所帮助:

https://sukhbinder.wordpress.com/2011/04/14/how-to-create-fortran-dll-in-visual-studio-with-intel-fortran-compiler/ 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#. 您可以通过两种方式从C#调用Fortran。

1) Create a Fortran console application (EXE). 1)创建一个Fortran控制台应用程序(EXE)。 Invoke from C# using Process.Start and pass input and outputs using files. 使用Process.Start从C#调用,并使用文件传递输入和输出。 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). 2)一种更高级的方法是创建一个Fortran DLL,并使用P / Invoke(DllImport)从C#进行调用。 With a DLL all inputs and outputs are passed in memory. 使用DLL,所有输入和输出都在内存中传递。 You can also use callbacks to report progress back to the C# calling code. 您还可以使用回调将进度报告回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/ http://www.luckingtechnotes.com/calling-fortran-dll-from-csharp/ http://www.luckingtechnotes.com/calling-fortran-from-c-monitoring-progress-using-callbacks/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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