calling C# dll from FORTRAN

calling C# dll from FORTRAN

Hi,

How can we call C# dll from Fortran? I googled a bit and found some examples where we can call Fortran dll from C#. However, I hardly find any example in the converse way. I would appreciate any suggestions.

Thanks.
Krishna

9 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

I think the most robust way is to create a wrapper dll in managed C++ (compiled with /CLR) switch. Managed C++ is the simplest bridge between managed and unmanaged code (actually, that is the very reason that Microsoft created it).

Here are few code snippets from an actual application. Fortran code uses C interoperability to call managed C++ function, which calls C# implementation. C++ methods are static, i.e. they do not require passing of a class object.

//C# implementation
public class DMSLogger
{
...
  public static void Log(LogLevel level, string formatString)
  {
      //body of log
  }
}
//in .h file:
class DMSLogger {
 ...
 static void DLLEXPORT Log(LogLevel level, char* message, char* filename, int line);
}

//in .cpp file:
void DMSLogger::Log(LogLevel level, char* message, char* filename, int line)
{
  msg = gcnew System::String(message);
  ...
  //Call the C# method
  ManagedLogger::DMSLogger::Log(level ,msg);
}

//Fortran-callable C wrapper:
extern "C"
{
  void DLLEXPORT DMSLogger_Log(DMSLogger::LogLevel level, char* message)
  {
    DMSLogger::Log(level, message, "", 0);
  }
}

Finally, the Fortran interface and call:

interface
  subroutine DMSLogger_Log(level, message) bind(C, name="DMSLogger_Log")
      import
      integer(LogLevel), value:: level
      character:: message(*)
   end subroutine
end interface
...
call DMSLogger_Log(Error, "LoadFlow_SetCustomData has failed. Aborting...")

This has one wrapper more than you probably need (long story...), but you should be able to call the
managed method directly from an extern "C" C++ CLR wrapper (DMSLogger_Log in my example).

Jugoslav
www.xeffort.com

Thank you for your nice suggestion. I'll go through it.

Krishna

I and my colleage tried to link the Fortran with managed C++ DLL, butwe got the linking problem at the first place. We got the following error:

>FortranCA.obj : error LNK2019: unresolved external symbol _maxx referenced in function _MAIN__

>Debug\FortranCA.exe : fatal error LNK1120: 1 unresolved externals

I thinkwe are missing something. The example code is attached for more details. This example is a slightly modified version of the example from
http://software.intel.com/en-us/forums/showthread.php?t=66501

Any help would be greatly appreciated.

Attachments: 

AttachmentSize
Download FortranCallCS.zip8.7 KB
Best Reply

See the attachment. You had several errors:

1) The DEC$ATTRIBUTES ALIAS was misspelled (two colons instead of one). (now commented out)
2) In general, do not mix BIND(C) and !DEC$ATTRIBUTES; they don't go along well. I commented out the later, and added NAME= to the former
3) There is no real need for STDCALL (and since it doesn't go along with BIND(C), I removed it)
4) The solution setup was messy (and still is, partly). I reconfigured both projects to produce output in $(SolutionDir)$(ConfigurationName).

For some reason, Project/Dependencies does not make linker recognize location of CPPLib. I had to add it through Linker/Input/Additional dependencies.

Attachments: 

AttachmentSize
Download FortranCallCS.zip8.7 KB
Jugoslav
www.xeffort.com

Dear Jugoslav,

Thank you very much for your valuable comments. However, I am unable to download the correction version. It says "Page not found". Can I give you a troubleto upload it again?

Krishna

It works for me, even when I'm logged out. Try refreshing the page, or perhaps another browser?

Jugoslav
www.xeffort.com

Download worked for me too.
Les

Many thanks. Today I can download after clearing Cookies, etc. Actually this is my first experience of mixed language programming. We (I and my colleague) tried several possibilities and were struggling over the past few days. We had also tried the way you have corrected, but somehow we were unable to make it work. We greatly appreciate your help and correction.

Leave a Comment

Please sign in to add a comment. Not a member? Join today