Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

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

rajxabc
Beginner
1,018 Views
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!!!
0 Kudos
4 Replies
TimP
Honored Contributor III
1,018 Views
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.
0 Kudos
Steven_L_Intel1
Employee
1,018 Views

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]

[plain]       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[/plain]

0 Kudos
Patrick_M_1
Beginner
1,018 Views

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.

0 Kudos
Steven_L_Intel1
Employee
1,018 Views

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.

0 Kudos
Reply