CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2001
    Location
    Wyoming
    Posts
    9

    Calling C++ from fortran

    I need to call a new C++ dll from an old fortran program. Does anyone where I can find an example of how to do this? I would like to keep the C++ portion as independent as possible, so when the C++ code is updated by my coworker, I just have to replace the old .dll with the new one. Thanks.


  2. #2
    Join Date
    Apr 2013
    Posts
    2

    Re: Calling C++ from fortran

    Quote Originally Posted by Mark Jablin View Post
    I need to call a new C++ dll from an old fortran program. Does anyone where I can find an example of how to do this? I would like to keep the C++ portion as independent as possible, so when the C++ code is updated by my coworker, I just have to replace the old .dll with the new one. Thanks.
    I also need to do this and have worked on and off with no success so far in figuring it out. I think you are going to be horrified at what a dog's breakfast Microsoft and Intel have made of PC programming with.DLLs and C.

    Here is what I think I understand about the issue so far:

    Unlike FORTRAN and Assembler, whose object files you can mix and link easily (give or take a little name-mangling!), the problem with .DLLs is that your FORTRAN or Assembler program cannot know before run time the address in memory to which a .DLL will be loaded - that's the nature of "Dynamic Link Libraries". They are actually not "Dynamic Link Libraries", because there is no dynamic linker, but rather "Dynamic Load Libraries".

    Even if you could figure out where in memory a .DLL got loaded - you might find (like I did) that that address is no use to you, because there is a difference between REAL memory addresses and Virtual memory addresses, and for security reasons, you cannot get at the former.

    So the only way you can get to routines contained in a .DLL is by calling Windows APIs.

    It is first necessary to call a Windows API to get the virtual memory address of where the .DLL is loaded - if it's loaded. Actually you first have to check if it is loaded (using another Windows API), and if it is not, load it (using yet another windows API)! Then you have to find out the offset, from the .DLL start address, to the routine in the .DLL that you want to call (using another windows API). And each Windows API may have different, big data structures in its argument list, which you have to declare in FORTRAN. Hopefully your FORTRAN is not so old that it lacks "derived types"!
    Hopefully also you have documentation of what the arguments of each routine would be if called from C. You may then have to do something to the arguments when calling from FORTRAN. For example, strings are stored differently, and 2-D arrays are stored transposed.

    If anybody can show it is simpler, I would be grateful.

  3. #3
    Join Date
    Apr 2013
    Posts
    2

    Re: Calling C++ from fortran

    I also forgot to mention this: I actually got as far as getting into a FORTRAN variable the virtual memory address of the entry point of a routine I wanted to call.

    The next problem is that there was no way to call a subroutine in FORTRAN, given its address in a variable name, i.e. if "SUBNAM" is a 32-bit integer containg a 32-bit address, you cannot write CALL SUBNAM(...)

    Well I fixed that by writing a little assembler routine called "CALLDLL" that took SUBNAM as an argument along with the arguments to SUBNAM, then tried to do a call in assembler to the address passed in SUBNAM.

    Still crashed, and don't know why!

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Calling C++ from fortran

    So the only way you can get to routines contained in a .DLL is by calling Windows APIs.
    Come on man. Every DLL builds along with its import library. And only things you have to do to be able to use DLL functions are:
    • link your FORTRAN program with the import library
    • make your C++ DLL exports be declared as extern "C"

    Of course no C++ specific things like classes, overridden functions, templates, etc. are not allowed to be exported from such DLL. Internally in the DLL you may use any C++ features you want, but in exports you follow the rule of extern "C".

    Hopefully also you have documentation of what the arguments of each routine would be if called from C. You may then have to do something to the arguments when calling from FORTRAN. For example, strings are stored differently, and 2-D arrays are stored transposed.
    Of course you have to take care of data types conversion between the language domains. But this point definitely has nothing to do with DLLs, Microsoft, Intel, dog's food and other horrible things.
    Last edited by Igor Vartanov; April 22nd, 2013 at 11:52 PM.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured