Re: Fwd: Problem calling C .dll library from Fortran using ISO_C_BINDING

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Pietro Ghillani wrote:
I am writing Fortran code on Windows 7 32-bit with MinGW.
I am using the Fortran 2003 feature that allows to access APIs
contained in a third party .dll library (probably) written in C with
some success as I have been able to call an initialization function
and a function that returns the library version.

With 32bit Windows, one has to be careful which calling convention is used. GCC defaults (for C, C++, Fortran, ...) to CDECL - or STDCALL, which is used by most of the Windows API functions.

Thus, if the DLL uses STDCALL instead of CDECL, you have to tell this the Fortran compiler. In gfortran, you do this using a directive as described http://gcc.gnu.org/onlinedocs/gfortran/GNU-Fortran-Compiler-Directives.html


The problems come when I try to call a function to create a new file
that requires three arguments: a file ID, the path to the file
including its name and the path to a folder to store the temporary
files. The documentation defines the syntax of the function as
follows:

long NewFile(long fID, char* Filename, char* Path)

In Fortran I have created an interface:

INTERFACE
FUNCTION NewFile(uID,FileName,Path) BIND(C, NAME='NewFile')
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_CHAR, C_LONG
    IMPLICIT NONE
    INTEGER(C_LONG) :: NewFile
    INTEGER(C_LONG), VALUE :: fID
    CHARACTER(C_CHAR) :: Filename(*)
    CHARACTER(C_CHAR) :: Path(*)

I think the interface is fine.


Then I call the function with:
iErr = NewFile(10,C_CHAR_'C:\\Windows\\Temp\\File.ext'// C_NULL_CHAR, &
C_CHAR_'C:\\Windows\\Temp'// C_NULL_CHAR)

Note: "\\" appears as two and not as one backslash. Thus, you should probably use "c:\Windows".

If you want that gfortran interprets the backslashes (e.g. \n = new line, \\ = *one* backslash etc.), you have to compile with -fbackslash.

to be honest, I don't know the meaning of those "char*" in the syntax
(I know that "char *Filename" would be a pointer, but "char* Filename"
is beyond my knowledge).

Those two are identical - fortunately, a space does not carry a meaning in this case!

Tobias




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux