T.10 - (OLD) How to call the library from a Win32 application ?¶
Answer¶
The library has been successfully compiled using Microsoft compiler For more information on load-time and/or run-time dynamic linking, please consult the manual of your favorite compiler and linker. The sample C code included in the distribution and discussed below has been test using Microsoft Visual C++.
Illustration¶
Before accessing the Unilib subroutines, their synopsis has to be defined in an include file. For example, the Fortran sypnopsis of the subroutine
um510()
is
INTEGER*4 kint, kunit, ifail
REAL*8 year
CHARACTER*32 lbint
CALL UM510 (kint, year, lbint, kunit, ifail)
In C, it can be translated as
void CALLBACK UM510 (long*, double*, char*, unsigned long, long*, long*);
For strings, note that Win32 Fortran passes strings as a descriptor, which includes a hidden length argument, that has to be provided by the C programme. The other arguments are always passed by reference. When defining a TYPE structure, e.g TYPE(zgeo), the order of the variable list has to be the same as in the header file, i.e.
typedef struct UNILIB_TYPE_ZGEO
{ double RADIUS;
double COLAT;
double ELONG; } TYPE_ZGEO;
Some subroutines, such as
um510()
, generate text output through Fortran WRITE statements. The Win32 application has to define a handle for the standard output in order to intercept the messages sent to file unit 6. This can be done by allocating a console by a call to the Win32 function AllocConsole().Inside the Win32 application, a typical call to UM510 reads
long kunit=6, ifail=0, kint=0;
double year=1985.;
char label[33]; // Fortran strings are not null-terminated:
label[32] = '\0'; // reserved 33 bytes and set the null character.
/* ... */
UM510(&kint, &year, label, 32, &kunit, &ifail);
if ( ifail < 0 ) exit(-1);
Be aware to reserve and pass the correct length when string arguments are used. Fortran strings are not null-terminated, and are padded with blank spaces.
See Also¶
UNILIB/tags/v3.02