您需要做的第一件事是在 C# 端使用stdcall:
[DllImport("DLL.dll", CallingConvention=CallingConvention.StdCall,
CharSet=CharSet.Auto)]
我还想确定 Delphi 方面是 Delphi 2009 之后的版本,因此使用宽字符。如果是这样,那么那里没有问题。如果您使用的是非 Unicode Delphi,那么您需要 CharSet.Ansi。
我可能还会在 Delphi 端返回一个 LongBool 并将其编组
[return: MarshalAs(UnmanagedType.Bool)]
回到 .NET 端。
最后,固定长度数组需要以不同方式编组。固定长度字符数组的标准方法是在 .NET 端使用 StringBuilder,根据需要进行编组。
把它放在一起,并修复你的 Delphi 语法,给出:
德尔福
type
TFixedLengthArray = array [1..200] of char;
function Get_Matrix(var Matrix: TFixedLengthArray): LongBool; stdcall;
C#
[DllImport("DLL.dll", CallingConvention=CallingConvention.StdCall,
CharSet=CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool Get_Matrix(StringBuilder Matrix);
static void Main(string[] args)
{
StringBuilder Matrix = new StringBuilder(200);
Get_Matrix(Matrix);
}
最后,确保在从 DLL 中返回字符串时以空值结尾!