Passing address of a C++ function to Fortran - syntax??

From: Adrian (acfer_at_hotmail.com)
Date: 11/23/04


Date: 23 Nov 2004 13:43:57 -0800

I am trying to pass the address of a C++ function into a Fortran
routine to enable the Fortran routine to call this C++ function. I
have to do it this way as our build process does not allow circular
dependencies of DLL's.

Does anyone know how to do this, I have tried everything in my book.

I have a C++ function GetP:

DllExport void GetP()
{
    ...code...
    return;
}

I am trying to pass the address of this function into a Fortran
routine called BIND_FTN. The call is made from another area of the
C++ code as follows:

void (GetP)();
DllImport void __stdcall BIND_FTN(int* GetP);

void Initialize()
{
    ...code...
    BIND_FTN(GetP); // inform Fortran of address of callback
function
    return;
}

The C++ will not compile, I get:
error C2664: 'BIND_FTN' : cannot convert parameter 1 from 'void
(__cdecl *)(void)' to 'int *'

Can anyone see the problem here?

Here is the Fortran code for BIND_FTN:

      subroutine BIND_FTN_EXTERNALS[DLLEXPORT](ext)
      external ext

      interface
        subroutine cb_GetP()
        !DEC$ ATTRIBUTES STDCALL :: cb_GetP
        end subroutine cb_GetP
      end interface

      pointer (p_GetP, cb_GetP)
      p_GetP = loc(ext)

      call cb_GetP ! call the C++ function from Fortran

      return
      end

(I haven't got to the Fortran yet - there may be syntax errors in
there)

Adrian



Relevant Pages

  • Re: Passing address of a C++ function to Fortran - syntax??
    ... | I am trying to pass the address of a C++ function into a Fortran ... | routine to enable the Fortran routine to call this C++ function. ... | DllExport void GetP() ... typedef void __stdcall GETP; ...
    (comp.lang.fortran)
  • Re: Passing address of a C++ function to Fortran - syntax??
    ... | I am trying to pass the address of a C++ function into a Fortran ... | routine to enable the Fortran routine to call this C++ function. ... | DllExport void GetP() ... typedef void __stdcall GETP; ...
    (comp.lang.cpp)
  • Re: calling a function once works, calling it twice fails
    ... I have linked a C++-program with a fortran routine, ... end subroutine READ_DIMENSIONS ... Does your Fortran compiler have an option to check array subscripts? ...
    (comp.lang.fortran)
  • Re: 2d Gaussian Quadrature
    ... > complex-valued function of two real variables. ... > Fritsch, Kahaner, and Lyness, Double Integration Using One Dimensional ... > of an adaptive routine over a single application of a gaussian ... > More recently I converted this routine to Fortran 90 and also created ...
    (comp.lang.fortran)
  • Re: Reported any bugs in C-LAPACK routine DSPEVX?
    ... >>and call the latest Fortran LAPACK routines directly. ... For the dstevx routine I do it like this. ... the declaration can be done also in a header.h file which is ... There exists other ways of including lapack routines into C. ...
    (sci.math.num-analysis)