Calling a FORTRAN subroutine from C#

user3275090

I want to import a FORTRAN DLL into visual C#. While I have done that 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 App:

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 that I get: x=0, y=0, MySub(a, b, x, y)=17

I'm a bit new in Visual C# programming, but I think above results mean that the two statements i.e. 'a+b' and '2*a+3*b' have been calculated but not assigned to x and y, and the latter (2*a+3*b) has been assigned to the function MySub itself! I have used OutAttributes in C#, also Intent[In], Intent[Out] in FORTRAN, but none have changed the results. I would appreciate any help or advice.

David Heffernan

Like you I assumed that INTENT(OUT) would imply pass by reference. But it seems that is not the case. So you need to add the following to the FORTRAN to force pass by reference:

!DEC$ ATTRIBUTES REFERENCE :: x,y

The correct declaration of the function on the C# side is:

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

Call it like this:

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

Note that you need to use the out keyword so that a pointer to the output variables is passed to the native code. You do need to use out or ref to make sure that the calling code sees the modified values.

Also note that the native function does not return a value. It's just a coincidence that the return value is passed through a register which by chance happened to contain the result of the last calculation that was performed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling FORTRAN subroutine from c++ yields illegal parameter value

From Dev

segmentation fault when calling fortran subroutine in C++

From Dev

Issues passing string parameters from C to a Fortran subroutine

From Dev

R calling Fortran subroutine with character argument

From Dev

Calling Fortran subroutine from Julia. Arrays work, but integers don't

From Dev

Calling a Fortran function from Qt C++

From Dev

Calling C from fortran (ifort, gfortran)

From Dev

Calling C function with **int parameter from Fortran

From Dev

Calling C function from Fortran with pointers

From Dev

Calling Access Subroutine from Python

From Dev

Call parallel fortran MPI subroutine from R

From Dev

trying to call fortran subroutine in c++

From Dev

Error linking fortran subroutine into c++ program

From Dev

How to use fortran module subroutine in c

From Dev

Subroutine C++ called from Fortran : How to deal with Dynamic memory allocation?

From Dev

When calling an internal subroutine in a parallel Fortran do loop, correct value of iteration variable is not accessible to the subroutine

From Dev

Fortran and C interoperability: receive return value from c (calling from Fortran)

From Dev

Perl threads - calling subroutine from module (pm)

From Dev

Pointer is being being masked when calling a C function from Fortran

From Dev

Calling Fortran subroutines with optional arguments from C++

From Dev

Calling Fortran from C# in VS2019 with iFort

From Dev

Clean way to separate functions/subroutine declaration from definition in Fortran 90

From Dev

C Wrapper Calling Fortran Functions

From Dev

Renaming a subroutine in a Fortran module when using iso_c_binding

From Dev

Fortran subroutine overloading with submodules

From Dev

Fortran syntax (declaration of subroutine)

From Dev

How to compile a Fortran subroutine

From Dev

Optimizing a Fortran subroutine

From Dev

Allocating arrays in a Fortran Subroutine

Related Related

  1. 1

    Calling FORTRAN subroutine from c++ yields illegal parameter value

  2. 2

    segmentation fault when calling fortran subroutine in C++

  3. 3

    Issues passing string parameters from C to a Fortran subroutine

  4. 4

    R calling Fortran subroutine with character argument

  5. 5

    Calling Fortran subroutine from Julia. Arrays work, but integers don't

  6. 6

    Calling a Fortran function from Qt C++

  7. 7

    Calling C from fortran (ifort, gfortran)

  8. 8

    Calling C function with **int parameter from Fortran

  9. 9

    Calling C function from Fortran with pointers

  10. 10

    Calling Access Subroutine from Python

  11. 11

    Call parallel fortran MPI subroutine from R

  12. 12

    trying to call fortran subroutine in c++

  13. 13

    Error linking fortran subroutine into c++ program

  14. 14

    How to use fortran module subroutine in c

  15. 15

    Subroutine C++ called from Fortran : How to deal with Dynamic memory allocation?

  16. 16

    When calling an internal subroutine in a parallel Fortran do loop, correct value of iteration variable is not accessible to the subroutine

  17. 17

    Fortran and C interoperability: receive return value from c (calling from Fortran)

  18. 18

    Perl threads - calling subroutine from module (pm)

  19. 19

    Pointer is being being masked when calling a C function from Fortran

  20. 20

    Calling Fortran subroutines with optional arguments from C++

  21. 21

    Calling Fortran from C# in VS2019 with iFort

  22. 22

    Clean way to separate functions/subroutine declaration from definition in Fortran 90

  23. 23

    C Wrapper Calling Fortran Functions

  24. 24

    Renaming a subroutine in a Fortran module when using iso_c_binding

  25. 25

    Fortran subroutine overloading with submodules

  26. 26

    Fortran syntax (declaration of subroutine)

  27. 27

    How to compile a Fortran subroutine

  28. 28

    Optimizing a Fortran subroutine

  29. 29

    Allocating arrays in a Fortran Subroutine

HotTag

Archive