- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 ?????
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 ?????
Link Copied
1 Reply
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
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