View previous topic :: View next topic |
Author |
Message |
silverdan
Joined: 14 May 2009 Posts: 29
|
Posted: Fri May 15, 2009 5:57 pm Post subject: How to pass a large amount of data from Fortran to C#? |
|
|
I have a c# application that calls a legacy Fortran dll. Once the C# application calls the Fortran dll, the Fortran dll spits out some binary files. There has been a request to see if Fortran could pass the contents of the files directly to C#. Rather than having C# read the files.
Maybe in one of the following ways?
1. Could this be done by passing a Fortran object to C#?
2. Could this be done by creating a Fortran module with a bunch of variables populated, then have c# access those variables directly to create its own C# object?
3. Pass some file stream from Fortran to C#? Again, rather than creating the files, somehow pass the data to C#?
Are any of these possible or maybe something better to do? |
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8211 Location: Salford, UK
|
Posted: Sat May 16, 2009 7:00 am Post subject: |
|
|
First of all you will need to use the same Fortran compiler to read the binary as that was used to create it. Also you will need the code in order to replicate it in the read instructions. If it was FTN95 then...
Look up what the help file has to say about accessing Fortran arrays from C#. Start in .NET platform->Calling Fortran from other .NET languages. |
|
Back to top |
|
 |
Andrew
Joined: 09 Sep 2004 Posts: 232 Location: Frankfurt, Germany
|
Posted: Sat May 16, 2009 9:17 am Post subject: |
|
|
You can pass .NET objects around, from C# to Fortran and from Fortran to C#. Objects can be created in the Fortran, populated and passed to C# or vice versa, however you want. Delegates should be avoided however. The most appropriate solution would depend on what exactly you want to achieve and the amount of data you want to pass around.
Topics that will be of interest in the help file are the .NET extensions such as:
- new@
- assembly_interface
- assembly_external
You also free to use streams if you like. After all they are .NET objects too and can be passed around in same way. If you wanted to create a stream, write to it and then pass the stream on then that would also be possible. |
|
Back to top |
|
 |
silverdan
Joined: 14 May 2009 Posts: 29
|
Posted: Mon May 18, 2009 2:33 pm Post subject: |
|
|
Andrew,
Thanks for your reply. Can you point me to a sample of how to pass a stream from FTN95 to .NET?
I know how to use OPEN in Fortran and WRITE to a file. I also know how to use StreamReader in C#. However, how can I open a file and a stream in FTN95 in one Class and then read that stream in a C# Class?
--Dan |
|
Back to top |
|
 |
Andrew
Joined: 09 Sep 2004 Posts: 232 Location: Frankfurt, Germany
|
Posted: Mon May 18, 2009 2:52 pm Post subject: |
|
|
You can do this with use of the ASSEMBLY_EXTERNAL and OBJECT extensions pointed at earlier in the thread. But, and example of passing a StreamReader could be...
Code: | PROGRAM TestStream
OBJECT("System.IO.StreamReader") StreamReader
ASSEMBLY_EXTERNAL("System.IO.File.OpenText") FileOpen
ASSEMBLY_EXTERNAL("System.IO.StreamReader.Close") StreamReaderClose
OBJECT("ClassLibrary1.Class1") CSharpClass
ASSEMBLY_EXTERNAL("ClassLibrary1.Class1.ReadStream") ReadStream
CSharpClass = new@("ClassLibrary1.Class1")
! Dont need to pass the object as its a static method
StreamReader = FileOpen("c:\Documents and Settings\andy\Desktop\text.txt")
! Pass the object as its an instance method
CALL ReadStream(CSharpClass, StreamReader)
CALL StreamReaderClose(StreamReader)
PAUSE
END |
With C# code something like:
Code: | namespace ClassLibrary1
{
public class Class1
{
public void ReadStream(StreamReader streamReader)
{
string s = "";
while ((s = streamReader.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
} |
The stream could be closed in the C# of course, I just put the example into the Fortan to show how it could be done there.
This example could be extrapolated for other .NET types you may need. |
|
Back to top |
|
 |
|