How to marshall fixed byte array for C function

Viewed 47

Hi I have been working to create a wrapper for dll of a third party C library to be used in C#.

Suppose I have C function from the library:

void functionA(byte data[16], byte buffer[8]);

Is it correct for the following code to be used to wrap the code above ?:

[DllImport("C.dll",CallingConvention=CallingConvention.Cdecl)]
static extern void functionA(byte[] data, byte[] buffer)
1 Answers

No, that declaration would only be valid for byte *data, which is a pointer to an array.

What you have is a fixed size array, for which you need the following

[DllImport("C.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void functionA(
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16]
  byte[] data,
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8]
  byte[] buffer)
Related