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

Mixed Programming Language: C# and Fortran

fdc_dhi
Beginner
262 Views
Mixed Programming Language: C# and Fortran

I need to call an initialize subroutine in Fortran (Compaq Visual Fortran 6.6) from a C# program.( Microsoft Visual C# .NET ver. 7??) I have code access to both codes.

I have to transfer to C# string variables (the names of file-path and input-file) to the Fortran program.
I have done the following:

In the C# code I have transfers the stringvaribles into a int array containing the ASCII codes:

int [] intSetupPath;
int [] intSetupFileBin;
char[] charSetupPath;
char[] charSetupFileBin;
int pathLength;
int fileLength;
.
.
.

/*SetupPath: */
SetupPath.Trim(); /* the file path*/
charSetupPath=SetupPath.ToCharArray();
intSetupPath = new int[charSetupPath.Length];
for (i=0;i {
intSetupPath=(int)charSetupPath;
}
/* Setupfile */
SetupFileBin.Trim();/* the file name*/
charSetupFileBin=SetupFileBin.ToCharArray();
intSetupFileBin = new int[charSetupFileBin.Length];
for (i=0;i {
intSetupFileBin=(int)charSetupFileBin;
}
pathLength=intSetupFileBin.Length-1;
fileLength=intSetupPath.Length-1;
StatusFromEngine=Fortran_Init(ref intSetupPath,
ref pathLength,
ref intSetupFileBin,
ref fileLength);
.
.
.

In the Fortran code, I reverse the ASCII code to a Character variable:
LOGICAL FUNCTION MShe_Init
(ASCIIInputFifFile,InputFifLength,
ASCIIInputFullSubDir,SubDirLength)

!dec$attributes dllexport, alias: "Fortran_Init":: Fortran _Init

integer,intent(in):: InputFifLength,SubDirLength
integer,dimension(0:InputFifLength),intent(in)::
ASCIIInputFifFile
integer,dimension(0:SubDirLength),
intent(in)::ASCIIInputFullSubDir
.
.
.
do i=0,InputFiflength
FifFileName(i+1:i+1) =CHAR(ASCIIInputFifFile(i))
enddo
do i=0,SubDirLength
FullSubDirFilePreName(i+1:i+1) =
CHAR(ASCIIInputFullSubDir(i))
enddo



However the to integer arrays in FORTRAN do not contain the correct ASCII code, that were in the C# part.
/b]

Where is the error ?????
0 Kudos
1 Reply
hweisberg
Beginner
262 Views
using System;
using System.Runtime.InteropServices;

class TestClass {
[DllImport("Hello.dll")]
public static extern void Hello([MarshalAs(UnmanagedType.AnsiBStr)] string Text);

[STAThread]
static void Main() {
Hello("Hello World.");
}
}

subroutine Hello(Text)
!DEC$ ATTRIBUTES DLLEXPORT :: Hello
!DEC$ ATTRIBUTES C, ALIAS:'Hello' :: Hello
integer*1 Text(12)
print 10, Text
10 format(12A1)
end

Be sure to copy Hello.dll and Hello.pdb to the bindebug folder of your C# project. Also in Project | Properties |Configuration Properties | Debugging set Enable Unmanaged Debugging to True.
0 Kudos
Reply