1

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.

  • 1
    Similar question is answered here https://stackoverflow.com/questions/20419415/c-sharp-call-c-dll-passing-pointer-to-pointer-argument – Imran Aug 14 '20 at 11:02
  • 1
    shouldn't `byte[] dataOut,` be `MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = size)] byte[] dataOut,` or something similar (LParray?)? – sommmen Aug 14 '20 at 11:15
  • 1
    Remove all your existing `MarshalAs` and [add `[Out]`](https://stackoverflow.com/a/21651331/11683) to `byte[] dataOut`. – GSerg Aug 14 '20 at 11:23

1 Answers1

2

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!