Need help , in passing passing C# string to FORTRAN 77 dll as argument

Need help , in passing passing C# string to FORTRAN 77 dll as argument

C# CODE

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace ConsoleApplication1
{
class Program
{
[DllImport("fdchar.dll",
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
static externvoid returnDecay([MarshalAs(UnmanagedType.LPStr)]string FCHAR,int tint);

static void Main(string[] args)
{
string ccc = "ABCD";
int tint=4;

CHARIN(ccc,tint);
Console.Write("Press any key to EXIT");
Console.ReadKey(false);
}
}
}

*******************************

FORTRAN 77 CODE

*$pragma aux CHARIN "CHARIN" export parm(value)

SUBROUTINE CHARIN(FCHAR)
C Declarations
CHARACTER*(*) FCHAR
C
PRINT*,FCHAR
C
RETURN
END

************************************

code is printing some garbage charater on screen NOT ABCD

please help!!!

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

It's probably inadvisable to attempt to link up C# with pure f77 CHARACTER strings, certainly inadvisable to do this without referring to documentation. The examples probably use ifort-specific reference or iso_c_interop bind(c) so as to suppress the implicit character string length argument.

You've passed the hidden length properly, so that isn't the issue. The example I have looks like this:

[c-sharp]sing System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace CsharpCallsFortran
{
    class Program
    {
        [DllImport(@"FortranDLL.dll")]
        public static extern void FortranDLL(
            [MarshalAs(UnmanagedType.R4)]System.Single r1,
            [MarshalAs(UnmanagedType.R4)]out System.Single num,
            [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder nnn,
            [MarshalAs(UnmanagedType.LPArray)] int[] arr,
            [MarshalAs(UnmanagedType.I4)] int aLen,
            [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder outstr,
            [MarshalAs(UnmanagedType.I4)]int len,
            [MarshalAs(UnmanagedType.I4)] int outlen);
        [STAThread]
        static void Main(string[] args)
        {
            System.Single r1 = 489.75F;
            System.Single num = 0F;
            System.Text.StringBuilder nnn = new System.Text.StringBuilder("abbccddddddd");
            System.Text.StringBuilder outstring = new System.Text.StringBuilder("                ");
            int len = nnn.Length;
            int[] arrYY = new int[9];
            int aL = 5;
            FortranDLL(r1, out num, nnn, arrYY, aL, outstring, len, 12);
            Console.WriteLine(num);
            Console.ReadLine();

        }
    }
}[/c-sharp]

       subroutine FortranDLL (r1, num, name, nArry, arrLen, outstr)

! Expose subroutine visualfortranDll1 to users of this DLL
!
       !DEC$ ATTRIBUTES DLLEXPORT:: FortranDLL
       !DEC$ ATTRIBUTES ALIAS:'FortranDLL':: FortranDLL
       !DEC$ ATTRIBUTES VALUE :: r1, arrLen
  

! Variables
        REAL, INTENT(IN) :: r1
        REAL, INTENT(OUT) :: num
        character*(*), INTENT(INOUT) :: name
        integer, INTENT(OUT) :: nArry(*)
        integer, INTENT(IN) :: arrLen
        character*(*) outstr
  
        print *, r1
        print *, name(1:1)
        name(1:1) = 'Z'
! Body of visualfortranDll1
        num = MOD(r1, 256.0)
        print *, num
    
        do 10  II = 1 , arrLen
            nArry(II) = II
  10    continue
        outstr='121'
        read(outstr, '(BN,I5)') IV
        outstr = name
      end subroutine FortranDLL

Steve - Intel Developer Support

I just attempted this sample (copy, paste) in vs2010.  Unfortunately, it unbalances the stack.

error message:

PInvokeStackImbalance was detected
Message: A call to PInvoke function 'CsharpCallsFortran!CsharpCallsFortran.Program::FortranDLL' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Could be - I wasn't able to determine if C# defaults to STDCALL or C for its calling convention.  Try adding STDCALL,REFERENCE to the ATTRIBUTES directive for the subroutine.

Steve - Intel Developer Support

Leave a Comment

Please sign in to add a comment. Not a member? Join today