- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi
Please let me know how to handle data type sending and receiving from C# to Fortran dll. I have come across some samples from VB and Fortran communication but not really specific to C#. I want to pass specific data from C# as inputarray to routine inFortran dll and receive some resultsas array from same Fortran dll after processing it.
Any help is appreciated.
Pradeep
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Attached is a C# sample. It may be of some help. Arrays can be tricky, if you don't want to get into added complexity, you can pass arrayname[0] (or whatever the first element is and Fortran should be able to pick it up. Remember that Fortran and C/C# index arrays differently.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Many thanks steve,
I have built the applivation with CVF as we will receive IVF in 2-3 days.
I am surprised to see following error which I guess is related to array type mismatch. Please correct me as I am not sure about the error.
I have created another sample application but there also no success. I will be grateful ifyou send me your email id so that I send the attachment as I am not able to attachthe file as per security policy here.
Thanks Pradeep
This is the error in application you sent:
489.7500
a
233.7500
a
233.7500
Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
at ConsoleApplication4.Class1.visualfortranDll1(Single r1, Single& num, Strin
gBuilder nnn, Int32[] arr, Int32 aLen, StringBuilder outstr, Int32 len, Int32 ou
tlen)
at ConsoleApplication4.Class1.Main(String[] args) in c:documents and setting
s135482desktopcsharpsamplecsharpsampleconsoleapplication4class1.cs:line 36
an instance of an object.
at ConsoleApplication4.Class1.visualfortranDll1(Single r1, Single& num, Strin
gBuilder nnn, Int32[] arr, Int32 aLen, StringBuilder outstr, Int32 len, Int32 ou
tlen)
at ConsoleApplication4.Class1.Main(String[] args) in c:documents and setting
s135482desktopcsharpsamplecsharpsampleconsoleapplication4class1.cs:line 36
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The sample I sent you was for Intel Visual Fortran. I don't know what if anything would need to change when using CVF. If it still fails with Intel Fortran, let me know.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I will check that code once we have IVF. I tried one simple code given below which really works without handling unmanaged types. I don't know whether to proceed with this one or one which you have written. Please let me know the difference between two.
Fortran code to build DllTest.dll:
SUBROUTINE SUB1(A,N)
!DEC$ ATTRIBUTES DLLEXPORT :: SUB1
!DEC$ ATTRIBUTES C, ALIAS :'SUB1':: SUB1
REAL A(N),B(N)
!DEC$ ATTRIBUTES C, ALIAS :'SUB1':: SUB1
REAL A(N),B(N)
DO 100 I=1,N
WRITE (*,*) A(I)
B(I) = A(I)*2
100 CONTINUE
END SUBROUTINE SUB1
WRITE (*,*) A(I)
B(I) = A(I)*2
100 CONTINUE
END SUBROUTINE SUB1
C# code to call the dll :
using System;
using System.Runtime.InteropServices;
namespace CallF77Dll
{
///
/// Summary description for Class1.
///
class Class1
{
[DllImport("DllTest.dll")]
public static extern void SUB1(float[] A,int N);
static void Main(string[] args)
{
float[] A = {1.1F,2.2F,3.3F,4.4F,5.5F};
float[] B;
int N = A.Length;
SUB1(A,N);
}
}
}
Thanks & Regards,
Pradeep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
To be honest, I don't know. If what you have works, that's great. Maybe the additional types used in the sample I provided (which is based on something from a customer) requires the MarshallAs stuff. Clearly I have some learning to do about C#.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am also in learning phase so not aware of managed/unmanaged handling. I will keep a post if I succeed in handling with managed thing only.
Thanks & Regards
Pradeep

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page