Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29255 Discussions

create a fortran dll to make a call from C# code

debolina123
Beginner
1,065 Views

I was thinking if there is a way to create fortran dll and make call to it from C# code.

An example will be of great help if possible.

Thanks

0 Kudos
9 Replies
Steven_L_Intel1
Employee
1,065 Views
I've attached an example that works though it hasn't been cleaned up yet. You may be able to get the general idea.

You asked in the other thread how to build DLL from the command line. You do it with:

ifort /dll sourcename.f90

You will want to add !DEC ATTRIBUTES DLLEXPORT lines as appropriate.
0 Kudos
jlea16
Beginner
1,065 Views

Steve - good to see that you are still Fortraning it!! I've been looking for exactly what you attached in this thread for quite some time now - thanks for the post. I did a quick review of the code and I have a couple of questions:

1 - since there are 8 parameters defined/passed in the C# and 6 parameters in the Fortran, I assume something is going on with either the characters (StringBuilder) data types, is this correct?

2 - Is there a "complete" list of how to marahall C# data types (including scalars, single and multidimensional arrays) down to fortran?

3 - Finally, do you see any problem building a C# wrapper around my good old fortran and using the resulting fortran DLL/C# wrapper .NET assembly in an ASP.NET v2.0 application? I want to start putting some of my useful engineering fortran into web applications and I see this a very viable solution.

Thanks.

Jon Lea

0 Kudos
Steven_L_Intel1
Employee
1,065 Views
1) Correct. The C# code passes the two character lengths at the end of the argument list, where Fortran expects to pick them up.

2) I have no such list. I will comment that multidimensional arrays are likely to be problematic - C/C++ at least does not represent these as anything close to Fortran, and that probably goes for C# as well. I don't know if C# can use "SafeArrays" that VB.NET does - you can manipulate these with Fortran (see the VB.NET-SafeArray sample).

Most of the mixed-language things, especially with the .NET languages, we pick up as we go along. This particular example is based on code from a customer - I did not write it from scratch.

3) You could do that., but also look at the reintroduced COM Server capability in IVF 10.
0 Kudos
jlea16
Beginner
1,065 Views
Thanks Steve.
0 Kudos
g_f_thomas
Beginner
1,065 Views

I've attached such a sample 'as is'. It works for me. YMMV.

C# has an Array class. What its relation is to a SafeArray I can't tell. It looks like its pretty close to a Fortran matrix. The demo illustrates the provision of a rank 2 real(8) matrix to C#by a VF Dll, the use of a Console in what is essentially a Windows Forms application, and the simulation of a lengthy operation in a (unsafe) thread. Enjoy.

Gerry

0 Kudos
Steven_L_Intel1
Employee
1,065 Views
Thanks, Gerry. So it seems C# has a real "matrix" type after all...
0 Kudos
g_f_thomas
Beginner
1,065 Views

Here's a repost. It has no known (to me) password.

Gerry

0 Kudos
g_f_thomas
Beginner
1,065 Views

When I click the reposted zip it opens and the archive is intact.

Gerry

0 Kudos
g_f_thomas
Beginner
1,065 Views

Third time lucky, maybe. I think the file was encrypted . I decrypted it and reattached. It has no password.

Gerry

0 Kudos
Reply