如何将字节数组从C#传递到外部DLL

如何将字节数组从C#传递到外部DLL,c#,C#,给定在DLL中具有此签名的方法 int32_t __stdcall ndUDSReadDataByIdentifier( TD1 *diagRef, uint16_t ID, uint8_t dataOut[], int32_t *len, int8_t *success); C#外部呼叫是什么样子的 我试过: [DllImport("nidiagcs.dll")] public static extern Int32 ndU

给定在DLL中具有此签名的方法

int32_t __stdcall ndUDSReadDataByIdentifier(
    TD1 *diagRef, 
    uint16_t ID,  
    uint8_t dataOut[], 
    int32_t *len,
    int8_t *success);
C#外部呼叫是什么样子的

我试过:

[DllImport("nidiagcs.dll")]
public static extern Int32 ndUDSReadDataByIdentifier(
    ref TD1 diagRef, 
    [MarshalAs(UnmanagedType.U2)] UInt16 ID,
    byte[] dataOut,
    [MarshalAs(UnmanagedType.U4)] ref Int32 len,
    [MarshalAs(UnmanagedType.U1)] ref byte success);

调用已执行,但数据输出未填充。

好的,我找到了解决方案

[DllImport("nidiagcs.dll", CallingConvention = CallingConvention.StdCall)]
private static extern Int32 ndUDSReadDataByIdentifier(
    ref TD1 diagRef,
    UInt16 ID,
    Byte[] dataOut,
    ref Int32 len, 
    out byte success);
这是调用函数的正确方法。这是我这边的错误。需要在dataOut中提供一个缓冲区数组,并在len中提供相应的缓冲区大小。我总是将len设置为0,这使库认为dataOut数组的大小为零,因此不返回任何内容


谢谢大家的帮助

这里回答了类似的问题:byte[]dataOut、不应该是
Marshallas(UnmanagedType.ByValArray,ArraySubType=UnmanagedType.U1,SizeConst=size)]byte[]dataOut、
或类似的东西(LParray?)吗?删除所有现有的
Marshallas
byte[]dataOut