- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

I've been working on the stdcall example I found on the web at: http://www.neurophys.wisc.edu/comp/docs/notes/not017.htmland I was able to get the C++ to call the Fortran subroutine, but the values from C++ aren't passing to Fortran and vice versa. I've made the code a bit simpler and here's what I've got.

// This illustrates how a Fortran routine and function may be

// called from a main program in C++#include

{

FR1(&n,&nSq);

}

and:

**FR1(N,M)**

SUBROUTINE

!DEC$ ATTRIBUTES STDCALL, ALIAS:'_FR1@8'::FR1

! COMPUTES THE SQUARE OF N, RETURNS IN M

**M,N**

REAL

M=N*N

**RETURN**

**END**

The value of N when I get into the Fortran subroutine is a random number(i.e. 6.06...E-39). The values of N and M when I get back into the C++ are 10 and 0, respectively. Any help would be appreciated.

Thanks

Link Copied

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

The "STDCALL" attribute changes the default passing mechanism for numeric argument from "by reference" to "by value". Your C program is passing by reference, so the Fortran program is interpreting the address of these values as the actual values.

Fix is easy; change the attributes to:

!DEC$ ATTRIBUTES STDCALL, REFERENCE, ALIAS:'_FR1@8'::FR1

and it should work for you.

- Lorri

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

!DEC$ ATTRIBUTES STDCALL,REFERENCE,DECORATE,ALIAS:"FR1" :: FR1

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

*Or my preference:*

!DEC$ ATTRIBUTES STDCALL,REFERENCE,DECORATE,ALIAS:"FR1" :: FR1

!DEC$ ATTRIBUTES STDCALL,REFERENCE,DECORATE,ALIAS:"FR1" :: FR1

Is there any advantage of one over the other, or is it just a preference?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

FR1(N,M)

**SUBROUTINE**

FR1(N,M)

**!DEC$ ATTRIBUTES STDCALL,REFERENCE,DECORATE,ALIAS:'FR1'::FR1**

! COMPUTES THE SQUARE OF N, RETURNS IN M

M,N

M=N*N

Still not passing value of N.

**REAL**

M,N

M=N*N

**RETURN****END**

Still not passing value of N.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

! COMPUTES THE SQUARE OF N, RETURNS IN M

...

Hope that helps.

Jrg Kuthe

www.qtsoftware.de

**SUBROUTINE**FR1(N,M)**!DEC$ ATTRIBUTES STDCALL, ALIAS:'_FR1@8'::FR1****!DEC$ ATTRIBUTES REFERENCE :: N,M**! COMPUTES THE SQUARE OF N, RETURNS IN M

**REAL**M,N...

Hope that helps.

Jrg Kuthe

www.qtsoftware.de

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

*Since you pass pointers and because you also declared FR1 to be STDCALL conform, don't you have to specify explicitely that you pass N and M by reference?*

No, the REFERENCE attribute for the FR1 means that all the arguments are passed by reference by default (unless explicitly overriden).

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page