Call FORTRAN routine from C #

I want to import a FORTRAN DLL into Visual C #. Although I did this with functions, the problem arises when I want to import a subroutine with multiple outputs. Here is a simple example:


FORTRAN DLL:

Subroutine MySub(a,b,x,y)

!DEC$ ATTRIBUTES DLLEXPORT, STDCALL, ALIAS:'MySub' :: MySub

Implicit None
Integer, INTENT(IN) :: a,b
Integer, INTENT(OUT) :: x,y

 y=a+b
 x=2*a+3*b
End Subroutine MySub

C # Console Application:

using System;
using System.Runtime.InteropServices;

namespace AlReTest
{
    class Program
    {
    [DllImport(@"D:\...\AltRetTest.dll", CallingConvention=CallingConvention.StdCall)]
        public static extern int MySub(int a, int b, [Out] int x, [Out] int y);
        static void Main(string[] args)
        {
        int a = 4;
        int b = 3;
        int x = 0;
        int y = 0;
        MySub(a, b, x, y);

        Console.WriteLine(x);
        Console.WriteLine(y);
        Console.WriteLine(MySub(a, b, x, y));
        }
    }
}

Here are the answers I get: x = 0, y = 0, MySub (a, b, x, y) = 17

I am a little new to Visual C # programming, but I think the above results mean two statements, i.e. "a + b" and "2 * a + 3 * b" were calculated but not assigned to x and y, and the last (2 * a + 3 * b) was assigned to the MySub function itself! I used OutAttributes in C #, also Intent [In], Intent [Out] in FORTRAN, but none of them changed the results. I would appreciate any help or advice.

+4
1

, , INTENT(OUT) . , , . FORTRAN : :

!DEC$ ATTRIBUTES REFERENCE :: x,y

#:

[DllImport(...)]
public static extern void MySub(int a, int b, out int x, out int y);

:

MySub(a, b, out x, out y)

, out, . out ref, , .

, . , , .

+4

Source: https://habr.com/ru/post/1537180/


All Articles