从C#调用delphi dll函数,传入字节数组

从C#调用delphi dll函数,传入字节数组,c#,delphi,interop,pinvoke,bytearray,C#,Delphi,Interop,Pinvoke,Bytearray,我很难找到让delphi函数对.net中的字节数组进行操作的最佳方法 delphi签名如下所示: procedure Encrypt( var Bytes: array of byte; const BytesLength: Integer; const Password: PAnsiChar); stdcall; export; [DllImport("Encrypt.dll", CallingConvention = CallingConvention.St

我很难找到让delphi函数对.net中的字节数组进行操作的最佳方法

delphi签名如下所示:

procedure Encrypt(
    var Bytes: array of byte;
    const BytesLength: Integer;
    const Password: PAnsiChar); stdcall; export;
[DllImport("Encrypt.dll",
    CallingConvention = CallingConvention.StdCall,
    CharSet = CharSet.Ansi)]
public static extern void Encrypt(
    ref byte[] bytes,
    int bytesLength,
    string password);
C代码如下所示:

procedure Encrypt(
    var Bytes: array of byte;
    const BytesLength: Integer;
    const Password: PAnsiChar); stdcall; export;
[DllImport("Encrypt.dll",
    CallingConvention = CallingConvention.StdCall,
    CharSet = CharSet.Ansi)]
public static extern void Encrypt(
    ref byte[] bytes,
    int bytesLength,
    string password);
在字节数组声明失败之前省略
var
ref
,但这是必需的,因为我将只更改数组的内容,而不更改数组本身


另外,由于某种原因,我似乎无法在delphi中获得数组的长度,如果我删除
BytesLength
参数,那么
length(Bytes)
将不起作用,如果我添加
BytesLength
参数,
length(Bytes)
开始工作,但
BytesLength
有一个错误的值。

将Delphi
Encrypt
的第一个参数设置为
Bytes:PByte
,您就可以开始了

正如您所看到的,一个开放数组需要传递指向第一个元素的指针和解释您在问题中所描述内容的长度