从Fortran调用C# [英] calling C# from Fortran

查看:191
本文介绍了从Fortran调用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有在Stack Overflow上找到任何讨论如何从Fortran调用C#的帖子(我也使用Visual Studio 2010和英特尔Visual Fortran进行了安装).但是,帖子的数量(数量)非常有限 [1 2

I’ve not found any posts on Stack Overflow that discuss calling C# from Fortran (I’m using Visual Studio 2010 with Intel Visual Fortran installed as well). However, there is a (very) limited # of posts[1, 2, 3 ] that discuss calling C/C++ from fortran.

在对这些帖子的回复之一中,有人提出从Fortran调用C ++比调用C更为棘手,这使我怀疑C#可能还比较棘手?缺乏C/C ++/C#的基础,我想知道为C/C ++制定的过程是否适用于C#?

In one of the responses to these posts, it was suggested that calling C++ from Fortran is trickier than calling C, which raised my suspicions that C# may be trickier yet? Lacking a foundation in C/C++/C#, I’m wondering if the procedures laid out for C/C++ are applicable to C#?

我在这些文章中注意到的一个共同点是需要一个称为ISO_C_BINDING的内部模块.在此处了解更多信息之后,我不清楚ISO_C_BINDING是否允许我将几个2D数组有价值的信息传递给用C#编写的程序(编译为DLL),调用一些事件"(类似于函数?),最后从C#中获取2D数组信息,在继续介绍我在Fortran中的业务之前.

One commonality I noticed among these posts was that the intrinsic module called ISO_C_BINDING was needed. After reading a bit more about it here, it wasn’t clear to me that ISO_C_BINDING would allow me to pass a couple of 2D-arrays worth of information to a program (compiled as a DLL) written in C#, call some ‘events’ (analogous to functions?), and finally get back a 2D-array of information from C# , before moving on about my business in Fortran.

如果同时熟悉Fortran和C#,能否请您告诉我ISO_C_BINDING是否足以胜任该任务?从上面列出的信息中我并没有那种感觉.如果有人有一个可行的示例,包括在 C# 和Fortran之间传递数组,以及从中调用 C# 函数Fortran,我非常感谢有机会将其作为我进行工作的模板来进行研究.谢谢,埃里克

If familiar with both Fortran and C#, could you please tell me if ISO_C_BINDING is adequate to the task? I’ve not gotten that sense from the information I’ve listed above. If anyone has a working example that includes passing arrays between C# and Fortran, as well as calling C# functions from Fortran, I would very much appreciate the opportunity to look it over as a template for how I might proceed. Thanks, Eric

推荐答案

Fortran代码:

function TestPass (floatArray) result (iRes)
implicit none
dll_export :: TestPass ! export function name

integer :: Ires
real, intent (in out) :: floatArray

dimension floatArray(5)

iRes = 0 ! Assign function result

open (5,FILE='output.txt')
write (5, 100) floatArray(3)

floatArray(0) = 0.0
floatArray(1) = 1.1
floatArray(2) = 2.2
floatArray(3) = 3.3
floatArray(4) = 4.4

! correct values are written to file here...
open (5,FILE='output.txt')
write (5, 100) floatArray(3)
100 format(5X,'got here',5X,F3.3)
close (5)
end function

C#代码:

static extern int TestPass (
[MarshalAs(UnmanagedType.LPArray, SizeConst=5,
ArraySubType=UnmanagedType.R4)]
float [] yields);

private void BtnTestClick(object sender, System.EventArgs e)
{
float [] floatArray = new float[5] {9.9F, 9.9F, 9.9F, 9.9F, 9.9F};
TestPass(floatArray);

// floatArray.Length == 0 after the function call

for ( int i = 0; i < floatArray.Length; i++ )
Trace.WriteLine(floatArray[i]);
}

另请参见以下链接:

http://software.intel.com/zh-CN/articles/calling-fortran-function-or-subroutine-in-dll-from-c-code

您还可以参考一些有关它的理论:

http://www.ibiblio.org/pub/languages/fortran/ch2-4.html

这篇关于从Fortran调用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆