简体   繁体   中英

How to pass byte array from C# to external DLL

Given a method with this signature in a DLL

int32_t __stdcall ndUDSReadDataByIdentifier(
    TD1 *diagRef, 
    uint16_t ID,  
    uint8_t dataOut[], 
    int32_t *len,
    int8_t *success);

How does the C# external call look like?

I tried:

[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);

The call is executed but the dataOut is not filled.

Okay I found the solution.

[DllImport("nidiagcs.dll", CallingConvention = CallingConvention.StdCall)]
private static extern Int32 ndUDSReadDataByIdentifier(
    ref TD1 diagRef,
    UInt16 ID,
    Byte[] dataOut,
    ref Int32 len, 
    out byte success);

This is the correct way to call the function. It was a mistake from my side. One needs to supply a buffer array in dataOut and the according size of the buffer in len. I always set len to 0 which makes the library think the dataOut array has a size of zero so nothing is returned.

Thanks for everyones help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM