How to link Visual Studio C to Fortran DLL?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • David Hsu
    New Member
    • Dec 2010
    • 2

    How to link Visual Studio C to Fortran DLL?

    here is my code:
    Visual C:
    Code:
    **********************************
    #include <stdio.h>
    #include <stdlib.h>
    extern __declspec(dllimport) void _stdcall abcd(float *aa , float *bb , float *cc , float *dd); 
    void main (void)
    { 
    	float aa=1.0;
    	float bb=2.0;
    	float cc=3.0;
    	float dd;
    	abcd(&aa,&bb,&cc,&dd);
    	printf("dd=%f",dd);
    	return;
    }
    *******************************
    fortran :
    Code:
    *******************************
          SUBROUTINE abcd(aa,bb,cc,dd)
          !DEC$ ATTRIBUTES DLLEXPORT::abcd
          !DEC$ ATTRIBUTES STDCALL,DECORATE, ALIAS:'abcd'::abcd 
           REAL aa                
           REAL bb                
           REAL cc               
           REAL dd               
           dd=( aa+bb)*cc
           RETURN
           END  
    
    ************************
    It`s ok to compile these codes together.
    but I can not get the right answer after debug, the data seems no connection with fortran dll.
    Here are the outputs:
    dd -1.0737418e+008 float
    bb 2.0000000 float
    cc 3.0000000 float
    aa 1.0000000 float
    can somebody tell me what`s wrong with it?
    thank you very much.
    Last edited by Niheel; Dec 2 '10, 10:39 PM. Reason: Please use code tags when showing code
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Have you made a library containing your abcd function?

    You add that library to your FORTRAN link.

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      David Hsu,

      I've done things like this before, and you have to be very careful about providing the correct argument list to FORTRAN.

      My first question is if your program is running at all, and it seemst to be, thus I expect that you got the mechanics of the linkage correct.

      Since that's working, you may need to look in your FORTRAN compiler's manuals to see what the calling conventions are. You might be surprised at just how convoluted things can get. Especially with

      The next thing I'd try is to return a constant value from your FORTRAN subroutine. It's an easy test, and it will let you triage the problem rapidly.

      Also, in the same pass, you might try using FORTRAN I/O to print out the values you recieved from C. If they aren't passed in correctly, then the return value is pointless. This, of course, depends on the ability to link the FORTRAN I/O libraries into your DLL - which may be problematic.

      Of course, if the second test fails, you will have no choice but to go to the manual. Make sure the manual matches your FORTRAN implementation. When I had to deal with this problem, Microsoft's FORTRAN and Borland's FORTRAN had different calling conventions.

      This sounds like a fun problem.

      Good Luck!
      Oralloy

      Comment

      • David Hsu
        New Member
        • Dec 2010
        • 2

        #4
        thanks a lot, it`s ok to write message on the screen for Fortran DLL. But the output still wrong for this code,now I consider about the using of :void main (void)
        is this right for sneding input and output, or I should edit it?
        thank you very muck

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          David Hsu,

          In C++, the main function should always return an int.

          Often this is managed by simply returning zero (success) at the end of function, with the use of exit calls having actuall error return codes.

          Cheers!
          Oralloy

          Comment

          Working...