PSS ID Number: 133104
Article Last Modified on 2/2/2002
Part Contents
---- --------
1 Building C projects
2 Building FORTRAN projects
3 Using DLLs with Mixed Languages
CC FSTR_F.FOR FORTRAN MAIN PROGRAM
CC
INTERFACE TO SUBROUTINE Ucase1 [C,ALIAS:'_Ucase1'] (text)
CHARACTER*(*) text
END
INTERFACE TO SUBROUTINE Ucase2 [ALIAS:'_Ucase2@8'] (text)
CHARACTER*(*) text
END
CHARACTER*40 string1,string2
DATA string1 /'This is a null-terminated string.'C/
DATA string2 /'This is a standard Fortran string.'/
WRITE (*, *) string1
CALL Ucase1 (string1)
WRITE (*, *) string1
WRITE (*, *) string2
CALL Ucase2 (string2)
WRITE (*, *) string2
END
// FSTR_C.C C Subprograms Make strings uppercase
//
#include <ctype.h>
void Ucase1( char *string )
{
char *ptr;
for (ptr = string; *ptr; ptr++)
*ptr = toupper( *ptr );
}
void __stdcall Ucase2( char *string, int length )
{
char *ptr;
for (ptr = string; *ptr; ptr++)
*ptr = toupper( *ptr );
}
CC FORTRAN MAIN PROGRAM
CC
INTERFACE TO SUBROUTINE csub [C,ALIAS:'_csub']()
END
INTEGER i
REAL x
CHARACTER*4 s
COMMON /BRIDGE[dllimport]/ i, x, s
i = 1
x = 2.5
s = 'fail'
WRITE(*,'(5X,A)') 'before calling dll '
WRITE(*,'(5X,A,I5)') 'i = ', i
WRITE(*,'(5X,A,F4.2)') 'x = ', x
WRITE(*,'(5X,A,A)') 's = ', s
CALL csub()
WRITE(*,'(5X,A)') 'after calling dll '
WRITE(*,'(5X,A,I5)') 'i = ', i
WRITE(*,'(5X,A,F4.2)') 'x = ', x
WRITE(*,'(5X,A,A)') 's = ', s
END
// C DLL Subprogram
//
#include <stdio.h>
#include <string.h>
#define DLLexport _declspec(dllexport)
DLLexport void csub();
DLLexport struct {
int i;
float x;
char s[4];
} BRIDGE;
void csub()
{
BRIDGE.i = BRIDGE.i + 1;
BRIDGE.x = BRIDGE.x + 1.5F;
strcpy(BRIDGE.s, "pass");
printf("\n\n%s\n%s%d\n%s%f\n%s%s\n",
"in the dll",
"i = ", BRIDGE.i,
"x = ", BRIDGE.x,
"s = ", BRIDGE.s);
}
Output redirected to a file:
Before calling dll:
i = 1
x = 2.50
s = fail
After calling dll:
i = 2
x = 4.00
s = pass
Output in the dll:
i = 2
x = 4.000000
s = pass
// C Main Program
//
#include <stdio.h>
#include <string.h>
#define DLLimport _declspec(dllimport)
extern void _stdcall FSUB ();
extern DLLimport struct {
int i;
float x;
char s[4];
} BRIDGE;
void main(){
BRIDGE.i = 1;
BRIDGE.x = 2.5F;
strcpy(BRIDGE.s, "fail");
printf("\n\n%s\n%s%d\n%s%f\n%s%s\n",
"before calling dll", "i = ", BRIDGE.i,
"x = ", BRIDGE.x,
"s = ", BRIDGE.s);
FSUB();
printf("\n\n%s\n%s%d\n%s%f\n%s%s\n",
"before calling dll", "i = ", BRIDGE.i,
"x = ", BRIDGE.x,
"s = ", BRIDGE.s);
}
CC FORTRAN DLL SUBROUTINE
CC
SUBROUTINE FSUB[dllexport]()
COMMON /BRIDGE[dllexport]/ i, x, s
INTEGER i
REAL x
CHARACTER*4 s
i = i + 1
x = x + 1.5
s = 'pass'
WRITE(*,*)
WRITE(*,'(5X,A)') 'in the dll '
WRITE(*,'(5X,A,I5)') 'i = ', i
WRITE(*,'(5X,A,F4.2)') 'x = ', x
WRITE(*,'(5X,A,A)') 's = ', s
END
Output redirected to a file:
In the dll:
i = 2
x = 4.00
s = pass
Before calling dll:
i = 1
x = 2.500000
s = fail
After calling dll:
i = 2
x = 4.000000
s = pass
Additional query words: 1.00
Keywords: kbLangC kbLangFortran KB133104
Technology: kbAudDeveloper kbFORTRANPower32100NT kbFortranSearch kbZNotKeyword2