PDA

View Full Version : Using Fortran 77 in .NEt for fortran newbie


Kerad
04-21-2004, 02:16 PM
Hi,
I am currently working on a .Net Project. In this project I have to use some very old thing :D made with Fortran 77.
I have no problem with .Net, as it's my main technologi for more than 2 years, but I never used Fortran :cool:

Well, the problem is, I don't know how to do that :(
So I have the followig questions:
1. Is there a way to convert automaticaly the Fortran 77 code into Fortran.Net code
2. If not, can we encapsulate fortran 77 function into .NEt classes (something with an external definition or what)
3. Or is ther a way to make a DLL with Fortran77 that can be used by .Net
I tried this with the following:
INTEGER FUNCTION dlleas(n)
!MS$ATTRIBUTES DLLEXPORT :: dlleas
dlleas=n*9
END FUNCTION
I exported it to a dll
First thing: the entry generated in the DLL is _DLLEAS@4 :eek:
Never mind, i do the following with C#:
[DllImport(@"C:\developpement\noyeau\dlleas.dll", EntryPoint="_DLLEAS@4")]
[return:MarshalAs(UnmanagedType.I4)]
public static extern int dlleas([MarshalAs(UnmanagedType.I4)]int n);

And then i try to do:
dlleas(3) or dlleas(i) where I is an int. But in both cases I get a System.NullreferenceException at runtime :(

4. Maybe we can make a dll using the fortran77 with the Lahay Compiler... how to to that ?

Well, any help would be welcome ;)

Thanks a lot
Kerad

tzeis
04-21-2004, 06:30 PM
1. Is there a way to convert automaticaly the Fortran 77 code into Fortran.Net code

If you have our .NET compiler, you should be able to compile the Fortran code to .NET MSIL without making changes. This should work as long as the code doesn't have ENTRY statements or pass functions as arguments. When doing this you want to make sure to use the "-ncs" option to turn off the case sensitivity of the compiler.


2. If not, can we encapsulate fortran 77 function into .NEt classes (something with an external definition or what)

If you were able to do #1, then you have already done this. In order to call the Fortran procedure from another .NET language, add a reference to the Fortran assembly, be sure to refer to the procedure with all caps, and add a class name that is the same as the procedure name. For example, if you have a Fortran procedure called FOO(), refer to it from C# as FOO.FOO(). If you want, you can provide your own class implementation by encapsulating the Fortran code in a module, and providing a class definition that includes the Fortran code as static methods. Once agauin, assuming you have a Fortran procedure called FOO():

subroutine foo()
! implementation of foo
end subroutine

You would encapsulate it like so:

module bar
type class1
contains
procedure, nopass :: foo
end type
contains
subroutine foo()
! implementation of foo
end subroutine
end module

After doing this, you would add "using BAR" to the C# code and refer to the Fortran method from C# as CLASS1.FOO()

If you want to call Framework methods directly from Fortran, you will need to make all the Fortran variable names have a consistent case, and lose the "-ncs" option. You would then refer to the procedure with the same case as it has in the Fortran code (case matters).


3. Or is ther a way to make a DLL with Fortran77 that can be used by .Net

You should be able to do this, provided you get your calling conventions right.

4. Maybe we can make a dll using the fortran77 with the Lahay Compiler... how to to that ?

We provide examples of calling Win32 Fortran DLL's from C#, managed C++, and VB .NET along with our compiler.

Kerad
04-22-2004, 09:06 AM
Ok ! Yhank you a lot :D :D :D
Indeed I have the fortran.Net compiler

I tried to compile a .for code -> works well compiles the thing and so on
Then I looked in the assembly what has been generated (with class explorer)
and I have the following:
dll: another conversion
public sealed class INVERM
and its methods

Yeah! and then I can use it with C# .. good job guys !
Somtimes the easiest solution is the better lol ;)

The only thing that I would like to have is a namespace encapsulating those generated classes. Is that possible?

Thanks again,
Kerad

tzeis
04-26-2004, 06:51 PM
The only mechanism for encapsulation in Fortran is the module, so by placing the code in a module, you are able to define the namespace the code will reside in. To do this is less complicated than enfolding the code in a class. For example, in the previously defined class, all you have to do is remove the class definition, but keep the module:
module bar
contains
subroutine foo()
! implementation of foo
end subroutine
end module
This procedure still has a generated class name, but is now encapsulated in namespace "bar" (or "BAR" if the -ncs compile options is used)..