Subject: question about C++/Fortran

From: Yiping Wang <[email-masked]>
Date: Thu, 28 Oct 1999 10:38:42 -0400

Dear CCL:

I am working on programming using Visual C++. However I need a
subroutine written by Fortran. So I wrote the following code in VC++ and
Microsoft Fortran, respectively:

test.cpp

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <fstream.h>

//------------------interface things-------------------------------

_declspec (dllimport) void MYADD(int*,double*,double*);

typedef void (*addPTR) (int*,double*,double*);

addPTR add_;

//------------------------------------------------------------------

int main()
{ ////load dll library and set function pointers////
 HINSTANCE hinst;
 hinst = LoadLibrary("..\\testFortran\\Debug\\testFortran.dll");
 add_ = (addPTR)GetProcAddress((HMODULE)hinst,"MYADD");

 double zz;
 double *x,*y;
 x=new double[2];
 y=new double[2];
 double a, b;
 x[0]=10.0;
 x[1]=50.0;
 int n;

 a=100.0;
 n=2;
 zz=100.0;
 add_(&n,&a,&b);

 cout<<"\nb="<<b;
 cout<<"\nz="<<z;
 cout<<"\nn="<<n;
 cout<<"\nx[0]="<<x[0];
 cout<<"\nx[1]= "<<x[1];
 cout<<"\ny[0]="<<y[0];
 cout<<"\ny[1]="<<y[1];
 cout<<"\nz="<<zz;
 return 0;

}
test.f file

 SUBROUTINE MYADD(N,X,Y)

     !MS$ATTRIBUTES DLLEXPORT :: MYADD
     !MS$ATTRIBUTES ALIAS : 'MYADD' :: MYADD

      INTEGER N
      REAL X,Y

     Y=-X

 RETURN
 END

After calling function add_, the program gave me the correct answer for
b. However right after call add_, something is messed up. For example,
the value of n and z are changed. I create a testFortran.dll
inMircrosoft Power Station and a test.dsw in Visual C++, which is type
of Window32 Console application.

I will appreciate if anyone can give me suggestion about how to do
multi-language programming especially beteween C++ and Frotran in Visual
C++ and Microsoft Fortran environments.
Sincerely,

Yiping Wang
[email-masked]