Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help calling fortran DLL with visual C

Status
Not open for further replies.

pecan204

Programmer
Jan 25, 2001
24
0
0
US
Hello,

Does anyone know how to call a fortran dll in MS VC?

I need some help calling one.I compiled the file below. Added the .lib file to the link tab. But I get this error.

Linking...
debug/DLL_ROUT.lib : fatal error LNK1106: invalid file or disk full: cannot seek to 0x3e218d3c
Error executing link.exe.

DLL_ROUT_EXE.exe - 1 error(s), 0 warning(s)

Fortram file is below.
Code:
! Fortran part of a C-Fortran DLL example. This
! routine DLL_ROUT is called from a C executable program.
SUBROUTINE DLL_ROUT (INT_ARG, STR_IN, STR_OUT)
IMPLICIT NONE
                        
! Specify that DLL_ROUT is exported to a DLL
!DEC$ ATTRIBUTES DLLEXPORT :: DLL_ROUT
                        
INTEGER INT_ARG
CHARACTER*(*) STR_IN, STR_OUT
                        
! This routine converts INT_ARG to a decimal string.
! appends the string value to STR_IN and stores it
! in STR_OUT. A trailing NUL is added to keep C
! happy.
!
! Note that there are implicit length arguments following
! the addresses of each CHARACTER argument.
                     
CHARACTER*5 INT_STR
                        
WRITE (INT_STR,'(I5.5)')INT_ARG
                        
STR_OUT = STR_IN // INT_STR // CHAR(0)
                        
RETURN
END

The main c file is below.
Code:
/* Main program written in C++ that calls a Fortran DLL */
#include <stdio.h>
#include <string.h>
                        
/* Declare the Fortran routine. The following items are of note:
                        
   - The &quot;C&quot; attribute prevents C++ name mangling  Remove it
     if the file type is .c
   - The dllimport specification is required                   
   - Fortran routines use the _stdcall interface by default
   - Fortran character arguments have a hidden length
     argument following the address
   - Routine name must be in uppercase to match Fortran.
*/
//extern &quot;C&quot; __declspec(dllimport) void _stdcall DLL_ROUT (                        

extern &quot;C&quot; void _stdcall DLL_ROUT (
    int *INT_ARG,
    char *STR_IN,
	int STR_IN_LEN,
	char *STR_OUT,
	int STR_OUT_LEN);
                        
void main (int argc, char *argv[])
    {
    char instring[40];
    char outstring[40];
    int intarg;
                        
    strcpy(instring,&quot;Testing...&quot;);
    intarg = 123;
    /* Call Fortran routine - pass intarg by reference,
       pass length of outstring explicitly */
    DLL_ROUT(&intarg,instring,strlen(instring),outstring,40);
    printf(&quot;%s\n&quot;,outstring);
    }


Thanks Ken

 
1) Is this a 16 or 32 bit DLL? If it is a 16 bit DLL, you will need to write a thunk for it.
2) On VC.net, DLL_ROUT should be declared as
Code:
extern void fortran
I don't know what it is on VC6: if you get a syntax error on fortran try pascal. Also, DLL_ROUT may have to be dll_rout. MS does some strange case switches between languages.
 
It is a 16 bit dll.

I am using the MS Visual C++ IDE version 5.0.

Any other thoughts? Anyone?

Maybe someone can try it?

Thanks
 
The problem is getting hold of the combination of tools that you have. I have MSVC6 and MSC3. On MSC3, it says, that Fortran routines should be preceded by __fortran (2 leading underscores).

Also, the Fortran program should have the directive $NOTRUNCATE (whatever that means).
 
xwb, Have you been able to get an example to work?

Thanks

Ken
 
I don't have access to a current MS fortran compiler. When I was using MSC3, I tried it with MS Fortran and it worked.

I haven't tried it with gcc and g77 yet but will do tonight.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top