简体   繁体   中英

Passing string from Fortran dll to C#

I am trying to load a Fortran dll dynamically and pass a string back from fortran to C#. Everything looks fine when inside the fortran code, but when returning to C# the value of the string is lost. Instead the initial value, set in C#, is back. I have tried to use the 'ref' keyword to get the string to be passed by reference, but then I get error like below. What am I doing wrong?

The runtime has encountered a fatal error. The address of the error was at 0x709ce248, on thread 0x2ac4. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

Fortran code:

module FortLibInterface
implicit none

integer, parameter :: STR_LENGTH = 256

contains

subroutine GetString(Str)
    !DIR$ ATTRIBUTES DLLEXPORT::GetString
    !DIR$ ATTRIBUTES ALIAS: 'GetString' :: GetString
    !DIR$ ATTRIBUTES REFERENCE:: Str

    character(len=STR_LENGTH), intent(inout) :: Str

    Str = 'bcdef...'

end subroutine

end module

C# code:

using System;
using System.Runtime.InteropServices;

namespace FortranCSTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string dllPath = "C:\\Temp\\FortLib.dll";

            FortLibTest lib = new FortLibTest(dllPath);

            lib.MakeTestCall();
        }
    }

    public class FortLibTest
    {
        public const int STR_LENGTH = 256;

        public const string FortranFuncName = "GetString";

        private string pathToDll = null;

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern IntPtr LoadLibrary(String DllName);

        [DllImport("kernel32.dll")]
        private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

        [DllImport("kernel32.dll")]
        private static extern bool FreeLibrary(IntPtr hModule);

        public FortLibTest(string FullPathToDll)
        {
            pathToDll = FullPathToDll;
        }

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private delegate void TypeGetStrInfo(char[] str);

        void GetStrInfo(char[] str)
        {
            IntPtr pDll = LoadLibrary(pathToDll);
            if (pDll != IntPtr.Zero)
            {
                IntPtr pFunc = GetProcAddress(pDll, FortranFuncName);
                if (pFunc != IntPtr.Zero)
                {
                    TypeGetStrInfo func = (TypeGetStrInfo)Marshal.GetDelegateForFunctionPointer(pFunc, typeof(TypeGetStrInfo));

                    func(str);
                }
                else
                {
                    //Something
                }

                FreeLibrary(pDll);
            }
            else
            {
                //Something
            }
        }

        public void MakeTestCall()
        {
            char[] str = new char[STR_LENGTH];

            str[0] = 'a'; 

            GetStrInfo(str);
        }
    }
}

For future reference. I added [In, Out] and everything works.

 [DllImport(_dllName, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
 public static extern void GetString([In, Out] char[] str);  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM