returning real values from fortran77 dll to c#

半世苍凉 提交于 2019-12-22 17:53:31

问题


Can somebody please point out what I'm doing wrong here?

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 values 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. much thanks in advance.


回答1:


Thanks everyone for suggestions

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)"" and it is working now

Thanks again




回答2:


Well, you are calling the Fortran-function with value-parameters. You should use reference-parameters (pointers) to get a value back from it.

Alternatively you can return a strcuture from the fortran code that contains your two values. You have to marshal it in C# to the corresponding types and read the results from it.

But I fear that I don't have any idea about fortran and if even one of my suggestions can work.



来源:https://stackoverflow.com/questions/1365958/returning-real-values-from-fortran77-dll-to-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!