Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

returning real values from fortran77 dll to c#

rajxabc
Beginner
1,508 Views

Please advice me ..what i m dooing wrong here
the following codes are Dummy code but similar to my large application
i am using using watcomf77 compilor to make win32 dll
my appologies .. if this is not the right place to ask this question




FORTRAN 77 dll code
________________________________________________________


*$pragma aux DON "DON" export parm(value*8,value*8)


SUBROUTINE DON(DAA,DBB,DCC)
REAL*8, DAA,DBB,DCC
DBB=DAA+1
DCC=DBB+1
RETURN
END
________________________________________________________


C# code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.InteropServices;

using System.Diagnostics;

namespace pDON
{
class Program
{

[DllImport("DON.dll",
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
public static extern void DON(
[MarshalAs(UnmanagedType.R8)] double DAA,
[MarshalAs(UnmanagedType.R8)] double DBB,
[MarshalAs(UnmanagedType.R8)] double DCC
);

static void Main(string[] args)
{
//double TIME = 100.0;
double DAA = 5.5;
double DBB = 7;
double DCC = 9;
//START( ENERIN, VAL1);
DON(DAA, DBB, DCC);

Console.Write("val1 = " + DBB);
Console.Write("val2 = " + DCC);
Debug.WriteLine("VAR = " + DBB.ToString());
Console.Write("Press any key to exit");
Console.ReadKey(false);
}

}
}


________________________________________________________

I want to get the valuse of DBB DCC back to C# main prog ..after they are processed thru FORTRAN 77 subroutine
P.S. : i can not use INTENT(out) as i m using fortran 77
expected values are DBB=6.5, DCC=7.5
values i m getting DBB=7, DCC=9

Or if there is another way to get the values ..like using COMMON BLOCK .
plz show me small example
much thanks in advance

0 Kudos
8 Replies
ZlamalJakub
New Contributor III
1,508 Views
I think that problem is that You did not set arguments You need to change in routine as pointers to value (I assume that C# passes by default variables by value as C). I do not understand C# but in C You should use something like

void routine(double *a,double *b)

Or may be problem in routine You used to print values, are correct values visible in debugger?

Jakub
0 Kudos
rajxabc
Beginner
1,508 Views
Quoting - Quba
I think that problem is that You did not set arguments You need to change in routine as pointers to value (I assume that C# passes by default variables by value as C). I do not understand C# but in C You should use something like

void routine(double *a,double *b)

Or may be problem in routine You used to print values, are correct values visible in debugger?

Jakub

Yes PRINT*,var is giving good result
its just ...they are not comming back to C#
i evan tryed , reference type for out put ,still same result
0 Kudos
ZlamalJakub
New Contributor III
1,508 Views
What is meaning of first line of Your fortran code

*$pragma aux DON "DON" export parm(value*8,value*8)

Is not problem there?

0 Kudos
rajxabc
Beginner
1,508 Views
Quoting - Quba
What is meaning of first line of Your fortran code

*$pragma aux DON "DON" export parm(value*8,value*8)

Is not problem there?


"*$pragma aux DON "DON" export parm(value*8,value*8)"
is Auxilary pragma[compilor directive]used in fortran 77 to export symbol(function or subroutine)
there is no problem in that as i m able to pass value of DAA from C# as input
and while debuggung with PRINT*,var in DLL i m getting desired values
0 Kudos
rajxabc
Beginner
1,508 Views

Just for information :
i am using these for compiling and linking

wmake -f C:WATCOMbinwbinDON.mk -h -e
wlink name DON d all SYS nt_dll op m op maxe=25 op q op symf @DON.lk1
wlib -q -n -b DON.lib +DON.dll
0 Kudos
GVautier
New Contributor III
1,508 Views
Hello

The call from the C code must be as follow if you want to get back the changed values of DBB and DCC :

DON(DAA, &DBB, &DCC);

So, in the fortran side, you must declare that the second and the third argument of the function are pointers not values. Look the Watcom f77 documentation to change your pragma settings.

Dont forget to change the C side prototype of the function.



0 Kudos
rajxabc
Beginner
1,508 Views
Quoting - gvautier
Hello

The call from the C code must be as follow if you want to get back the changed values of DBB and DCC :

DON(DAA, &DBB, &DCC);

So, in the fortran side, you must declare that the second and the third argument of the function are pointers not values. Look the Watcom f77 documentation to change your pragma settings.

Dont forget to change the C side prototype of the function.




By Default F77 usescall-by-reference for arguments and i have alread tried call-by-value and call-by-reference at C# side
0 Kudos
rajxabc
Beginner
1,508 Views

Thanks a lot to everyone specially QUBA and GVAUTIER for pointing me to right direction

the input argument should be passed by value and out put arguments ...pass by refrence
so i changed my Auxilary pragma to
"*$pragma aux DON "DON" export parm(value*8, reference, reference)"
Now it works like a charm :-)
Thanks again
0 Kudos
Reply