Delphi dll 导入到 C# - System.AccessViolationException DllImport

Delphi dll importing to C# - System.AccessViolationException DllImport

美好的一天。我正在努力获取对 Delphi 到 return 数据的函数调用。我知道 dll 正在工作,因为如果我在 dll 中使用 show msg,它 return 是正确的信息。

我认为这与不同的内存大小有关。我还阅读了很多关于预缓冲内存的内容,不幸的是,我不知道谁可以将它实现到我的项目中

异常详细信息: System.AccessViolationException {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}

Delphi函数:

function doStuff({var} c:string): pchar; stdcall;    

C#

   [DllImport("Utili.dll",
          CallingConvention = CallingConvention.StdCall,
          CharSet = CharSet.Unicode)]
   public static extern string doStuff(string a);

    static void Main(string[] args)
    {
        try
        {
          string q = "1234567890123456";
          string a =  doStuff(q);
        }
        catch (Exception)
        {

            throw;
        }

    }

让供应商修复他们的代码。

如果您有 Delphi 代码,请查看 CoTaskMemAlloc