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.

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#?

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.

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

Community
  • 1
  • 1
user2256085
  • 483
  • 4
  • 15
  • You may want to wrap your Fortran code with C glue. Then your `C#` code will call *C* code which will call your *Fortran* – Basile Starynkevitch Apr 08 '13 at 05:27
  • Hi user2256085, I'm asking this exact same question and, like you, am having trouble finding much content about it. When I do find something, like the answer below, it often seems to be in the wrong direction, that is Fortran to C# and not Vice-Versa. Have you found out anything new in the 2 years since you posted this? – u8it Sep 28 '15 at 14:47
  • P57, I was able to get an example working, eventually. If there is a way to continue this conversation directly (via email), let's do that, though it might require one of us to post direct contact information here, which you may not want to do (I don't). – user2256085 Sep 29 '15 at 21:31

1 Answers1

2

Fortran code:

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# code:

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]);
}

Also refer this link:

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

You can also refer some theory about it:

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

Freelancer
  • 9,008
  • 7
  • 42
  • 81
  • Hello Freelancer, Gosh, I had to go back and re-read my original post because the example you provided had me thinking I asked the very opposite of what I intended. Unless I've missed the obvious, which is entirely possible, I was soliciting for an example in which C# was called from Fortran. That is, do you (or anyone else for that matter), have an example of some fortran code that makes use of a function written in C#, and not the other way around as show above? – user2256085 Apr 09 '13 at 04:01