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

C# fortran string by ref

babe59
Beginner
466 Views
Hello,
I do not manage to create a method with a by ref String parameter. I want to give a string to a Fortran method, change this s tring on my fortran method et return the new value. I try StringBuilder, byte[]... but nothing work..
I work on VS2005, C#, Fortran 77

<<<<<<<<<<<< Fortran Code (in DLL) >>>>>>>>>>>>

[bash]    SUBROUTINE PARAM_REF (I2, I4, C6, R4)

      IMPLICIT NONE
     
      INTEGER*2 I2
      INTEGER*4 I4
      CHARACTER*6 C6
      REAL*4 R4
      
      !DEC$ ATTRIBUTES DLLEXPORT :: PARAM_REF
      !DEC$ ATTRIBUTES ALIAS:'PARAM_REF'::PARAM_REF
      !DEC$ ATTRIBUTES REFERENCE :: C6
      I2 = 1
      I4 = 2
      C6 = '654321'
      R4 = 1.2
      
      END SUBROUTINE[/bash]




<<<<<<< C# Program >>>>>>>

[bash]        [DllImport("dll1.dll")]
        public static extern void PARAM_REF(
           [MarshalAs(UnmanagedType.I2)] ref Int16 i2,
           [MarshalAs(UnmanagedType.I4)] ref Int32 i4,
           String c6,
           [MarshalAs(UnmanagedType.R4)]ref  float r4); // Passage par adresse obligatoire

           Int16 i2 = 10;
            Int32 i4 = 12;
            String c6 = "123456";
            float r4 = 12.54541122f;    // Limit  5 dcimales . A passer en ref


            PARAM_REF(ref i2, ref i4, c6, ref r4);

[/bash]

Could you help me ? Thks
0 Kudos
1 Reply
anthonyrichards
New Contributor III
466 Views
I'm not sure about C#, but it may be very similar to VB .NET, in which case this is how I send an array of Bit-strings to Fortran from Visual Basic:

Imports System.Runtime.InteropServices
Public Class Form1
' Note UPPERCASE symbols are exported as default in the Fortran DLL
Private Declare Sub PASSSTRINGSTOFORTRAN Lib "c:\f90\test_VBstrings\debug\test_VBstrings.dll" _
(ByRef nelements As Long, ByVal str() As String)

nelements is the number of strings. On the Fortran side I have

subroutine PassStringsToFortran(nelements, ptr_Array)
! Expose subroutine PASSSTRINGSTOFORTRAN (n.b. exported symbol defaults to Uppercase in Fortran) to users of this DLL
! and specify that nelements is passed by reference but ptr_Array is passed by value
!
!DEC$ ATTRIBUTES DLLEXPORT::PassStringsToFortran
!DEC$ ATTRIBUTES VALUE::ptr_Array
!DEC$ ATTRIBUTES REFERENCE::nelements
USE OLEAUT32
USE DFNLS
USE DFWIN
! ptr_array is a pointer to a SafeArray of bit-strings
! nelements is the number of strings in the array
integer ptr_array, nelements

and I deal with the strings as a SafeArray using the supplied pointer. So try changing the UnmanagedType from SafeArray to the C#-equivalent of VT_BSTR and then deal with the pointer on the Fortran side.
0 Kudos
Reply