And*_*res 7 c# arrays delphi dll
我需要在我的C#代码中使用Delphi DLL.
使用具有公共参数的其他方法时我取得了一些成功,但在这种情况下,解决方案仍然隐藏.
DLL文档提供了此声明:
Function Get_Matrix (var Matrix : array [ 1..200 ] of char) : boolean ; stdcall;
Run Code Online (Sandbox Code Playgroud)
我试着用:
[DllImport("DLL.dll")]
public static extern bool Get_Matrix(ref char[] Matrix);
Run Code Online (Sandbox Code Playgroud)
不成功.一些帮助?
您需要做的第一件事是stdcall在C#端使用:
[DllImport("DLL.dll", CallingConvention=CallingConvention.StdCall,
CharSet=CharSet.Auto)]
Run Code Online (Sandbox Code Playgroud)
我还想确保Delphi方面是在Delphi 2009之后发布的,因此使用了宽字符.如果是这样,那就没有问题了.如果您使用的是非Unicode Delphi,那么您需要CharSet.Ansi.
我可能还会LongBool在德尔福方面返回并将其编组
[return: MarshalAs(UnmanagedType.Bool)]
Run Code Online (Sandbox Code Playgroud)
回到.NET端.
最后,需要对固定长度阵列进行不同的编组.固定长度字符数组的标准方法是StringBuilder在.NET端使用a ,根据需要进行编组.
完全放在一起,并修复您的Delphi语法,给出:
德尔福
type
TFixedLengthArray = array [1..200] of char;
function Get_Matrix(var Matrix: TFixedLengthArray): LongBool; stdcall;
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
最后,确保在从DLL返回时将null终止字符串!
| 归档时间: |
|
| 查看次数: |
2414 次 |
| 最近记录: |