C# calling Fortran DLL with various types of dummy argument

C# calling Fortran DLL with various types of dummy argument

Just downloaded evaluation copy of Intel Fortran 7.0. Has some success in calling Fortran DLL from C#. Hope this might be helpful for someone searching for mixed language example.

Regards,
Long

C# main program

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace cs
{
///
/// Summary description for Class1.
///
public struct complex
{
public float x, y;

public complex(float p1, float p2)
{
x = p1;
y = p2;
}
}
class Class1
{
///
/// The main entry point for the application.
///
[STAThread]
// DLLIMPORT ATTRIBUTE FOR .NET PLATFORM INVOKE SERVICES
[DllImport("IFdll.dll")]
// PROTOTYPE FOR THE NATIVE LF95 DLL
public static extern void SUB1(ref double a1,ref double a2,ref float ary1,ref float ary2,ref int m, ref int n,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder fname,ref complex cmplx,int len);
// string fname,int len);
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
complex v = new complex(3.4f,5.6f);

double a1 = 23.74;
double a2 = 45.67;
float[] aa1;
float[,] aa2;
int m,n;
// string fname = "my file name is c:\driver\my directory\intel fortran";
StringBuilder fname = new StringBuilder("my file name is c:\driver\my directory\intel fortran");

m = 7;
n = 5;

aa1 = new float [m];
for(int i=0; i aa1 = i;
aa2 = new float [m,n];
int k = 1;
for(int i=0; i for(int j=0; j {
aa2[i,j] = k;
k++;
}

Console.WriteLine("");
Console.WriteLine(fname);
Console.WriteLine("C# Calling Native IF Fortran 90 DLL (ML WINAPI) Example");
// LF95 DLL FUNCTION CALL
// SUB1(ref a1,ref a2,ref aa1[0], ref aa2[0,0], ref m, ref n,fname, fname.Length);
SUB1(ref a1,ref a2,ref aa1[0], ref aa2[0,0], ref m, ref n, fname,ref v ,fname.Length);
Console.WriteLine("");
Console.WriteLine("C# array2(3,4)= {0}",aa2[3,4]);
Console.WriteLine("C# array2(6,2)= {0}",aa2[6,2]);
Console.WriteLine("C# dummy1 = {0}", a1);
Console.WriteLine("C# dummy2 = {0}", a2);
Console.Write(" {0}",a1);
Console.WriteLine(" {0}",a2);
Console.WriteLine(fname);
Console.WriteLine(" {0} {1}",v.x,v.y);
Console.WriteLine();
}
}
}

now here is the Fortran DLL portion of code.

! IFdll.f90
!
! FUNCTIONS/SUBROUTINES exported from IFdll.dll:
! IFdll - subroutine
!
subroutine sub1(dummy1,dummy2,array1,array2,m,n,fname,c)
!DEC$ ATTRIBUTES DLLEXPORT::sub1
INTEGER,INTENT(IN) :: m,n
REAL(KIND=8) :: dummy1,dummy2
REAL(KIND=4),DIMENSION(m) :: array1
REAL(KIND=4),DIMENSION(m,n) :: array2
CHARACTER(LEN=*) :: fname
complex(kind=4) :: c
REAL(KIND=16) quad1,quad2
INTEGER :: i,j
quad1 = dummy1**20
quad2 = dummy2**20
PRINT *, "dummy1 = ",dummy1,", dummy2 = ",dummy2
PRINT *, dummy1**20,dummy2**20
dummy1 = dummy1**20; dummy2 = dummy2**20
PRINT *,"inside fortran dll", dummy1,dummy2
print *, "now changing dummy1 = 123.123 and dummy2 = 456.456"
dummy1 = 123.123
dummy2 = 456.456
PRINT *, "quad1 = ",quad1,", quad2 = ",quad2
PRINT '(8f10.4)',array1
PRINT *,"printing array 2"
PRINT '(10f10.4)',((array2(i,j),i=1,m),j=1,n)
!PRINT *, array2
PRINT *, "Fortran 95 array2(3,4) = ", array2(3,4)
PRINT *, "Fortran 95 array2(6,2) = ", array2(6,2)
PRINT *, fname, LEN(fname)
print *, "passing in from C#"
print *, c
print *, "now changing it in Fortran to c/(1.23,45.3)"
print *, c/(1.23,45.3)
c = c/(1.23,45.3)
fname = fname(:40) // " back from fortran 95"

end subroutine sub1

1 post / 0 new
For more complete information about compiler optimizations, see our Optimization Notice.