calling C++ function from Fortran: parameter passing 
Author Message
 calling C++ function from Fortran: parameter passing

Hi,

I noticed that when I'm calling a C++ function from my fortran code,
the parameters lose their values.

The C++ function is declared as follows:

extern "C" void __cdecl
GerkCallback(double &inCurrentTime
double *fortranStateProbabilities,
double *fortranStateProbabilitiesDelta )
{
   ...

Quote:
}

The Fortran code uses the function like this (I combined all the
relevant code together):
   REAL*8 T [REFERENCE]  
   EXTERNAL F       //F is passed to Fortran as a pointer
   A = T
   CALL F(A, YG, YGP)
As I debug this code, I see that A equals 0 before calling F, but when
I get to the C++ function, the corresponding parameter inCurrentTime
takes some random value, instead of 0. The other params become bad as
well.

Both C++ and Fortran code is compiled with the _cdecl calling
convention. Environment: VC++ .Net, Intel Fortran 7.1.

What am I doing wrong?

Thanks,
Pavel



Tue, 25 Oct 2005 04:38:54 GMT  
 calling C++ function from Fortran: parameter passing

Quote:

> Hi,

> I noticed that when I'm calling a C++ function from my Fortran code,
> the parameters lose their values.

> The C++ function is declared as follows:

> extern "C" void __cdecl
> GerkCallback(double &inCurrentTime
> double *fortranStateProbabilities,
> double *fortranStateProbabilitiesDelta )
> {
>    ...
> }

> The Fortran code uses the function like this (I combined all the
> relevant code together):
>    REAL*8 T [REFERENCE]
>    EXTERNAL F       //F is passed to Fortran as a pointer
>    A = T
>    CALL F(A, YG, YGP)
> As I debug this code, I see that A equals 0 before calling F, but when
> I get to the C++ function, the corresponding parameter inCurrentTime
> takes some random value, instead of 0. The other params become bad as
> well.

> Both C++ and Fortran code is compiled with the _cdecl calling
> convention. Environment: VC++ .Net, Intel Fortran 7.1.

> What am I doing wrong?

> Thanks,
> Pavel

I do not know the details of your environment, but why not change the
reference &inCurrentTime to a pointer - I suspect this is the
problem.

Regards,

Arjen



Tue, 25 Oct 2005 14:39:43 GMT  
 calling C++ function from Fortran: parameter passing
I found what the problem was. The variable A was typed implicitly, and
apparently its type was not "REAL*8" (which is equivalent to "double"
in C++). When I specified the type of A as REAL*8, everything became
fine.

Pavel

Quote:

> Hi,

> I noticed that when I'm calling a C++ function from my Fortran code,
> the parameters lose their values.

> The C++ function is declared as follows:

> extern "C" void __cdecl
> GerkCallback(double &inCurrentTime
> double *fortranStateProbabilities,
> double *fortranStateProbabilitiesDelta )
> {
>    ...
> }

> The Fortran code uses the function like this (I combined all the
> relevant code together):
>    REAL*8 T [REFERENCE]  
>    EXTERNAL F       //F is passed to Fortran as a pointer
>    A = T
>    CALL F(A, YG, YGP)
> As I debug this code, I see that A equals 0 before calling F, but when
> I get to the C++ function, the corresponding parameter inCurrentTime
> takes some random value, instead of 0. The other params become bad as
> well.

> Both C++ and Fortran code is compiled with the _cdecl calling
> convention. Environment: VC++ .Net, Intel Fortran 7.1.

> What am I doing wrong?

> Thanks,
> Pavel



Tue, 25 Oct 2005 22:51:36 GMT  
 calling C++ function from Fortran: parameter passing

Quote:

> I found what the problem was. The variable A was typed implicitly, and
> apparently its type was not "REAL*8" (which is equivalent to "double"
> in C++). When I specified the type of A as REAL*8, everything became
> fine.

I recommend IMPLICIT NONE.

--
Craig Powers
MVP - Visual C++



Tue, 25 Oct 2005 23:07:18 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. calling C++ function from Fortran: parameter passing

2. Passing C++ Class Member Function to as a C Callback Function Parameter

3. Calling a member function passed as parameter

4. function parameters to pass a VB string array to a C++ DLL

5. passing Fortran characters to C (was: VAX - passing strings between C and FORTRAN)

6. Calling Managed C++ function from C# with array parameters

7. Calling Managed C++ function from C# with array parameters

8. Call a function in a function with name of a function as parameter

9. how do you pass in a call back function to a member fnc in C++

10. Passing and retrieving a buffer calling a c++ function

11. Variable Numbers of Parameters in Fortran Calls

12. C# client crashs when calling into Managed C++ which calls unmanaged c++ function

 

 
Powered by phpBB® Forum Software