Silverfrost Forums

Welcome to our forums

calling a static c# method from f95

31 Mar 2008 10:09 #2962

How do you call a static C# method FROM Fortran in Visual Studio 2005?

I have tried various combinations of <namespace>.<class>.<method> in the ASSEMBLY_EXTERNAL statement and all fail with a compiler error: 'Method Not Found...'. The compiler apparently recognizes the .dll in the LIBRARY statement. Help. Please.

1 Apr 2008 7:04 #2963

You will find instructions in the FTN95 help file under 'Calling .NET methods from Fortran'.

The class names etc are case-sensitive and the intelisense works OK so you can build up the name from the dropdown lists that automatically appear.

1 Apr 2008 9:00 #2964

Static methods are the simplest to call, in that you dont have to instantiate an object first. For a simple static method called PrintMe, in a class called Class1 in a namespace called StaticMethods, the Fortran should look like:

ASSEMBLY_EXTERNAL('StaticMethods.Class1.PrintMe') :: PRINTME
CALL PRINTME()
PAUSE
END
1 Apr 2008 10:10 #2965

Thanks, gentlemen, but here is the exact, relevant code that dosen't compile:

C#, built as a class library, VMSLink.dll:

namespace VMSLinkGroup { public class VMSLink { ... public static int RA_Connect(string ipaddr, out string result) { ...

and the FTN95 main program:

 PROGRAM MAIN
 ...
 LIBRARY 'VMSLink.dll'
 ASSEMBLY_EXTERNAL(NAME='VMSLink.RA_Connect') :: RA_CONNECT
 ...
 STRING IPADDR, ERRMSG
 ...
 IOSTAT = RA_CONNECT(IPADDR, ERRMSG)
 ...

The exact Error message is: 'error 1117 The .NET Method VMSLink.RAConnect was not found. Have you forgotton...'

What am I doing wrong?

1 Apr 2008 11:11 #2966

The code below is the correct usage of your class.

ASSEMBLY_EXTERNAL('VMSLinkGroup.VMSLink.RA_Connect') :: RA_CONNECT 
STRING IPADDR, ERRMSG
INTEGER IOSTAT
IOSTAT = RA_CONNECT(IPADDR, ERRMSG) 
PRINT *, IOSTAT
PAUSE
END

You should specify the library to compile/link with on the command line, like:

FTN95.EXE File.f95 /CLR /CLR_VER 2 /REF VMSLink.dll /BINARY out.dbk

and

DBK_LINK2.EXE /REF:VMSLink.dll file.exe out.dbk

Note I have used /CLR_VER 2 and DBK_LINK2 for use with .NET version 2.0, but the same would work for 1.1 (without /CLR_VER 2 and with DBK_LINK)

1 Apr 2008 11:56 #2967

Thank you Andrew.

The 'trick' is, apparently, simply to prefix the class with the namespace. The documentation could be just a tad more clear on that point. Thanks again.

Please login to reply.