- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!!!
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!!!
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page