Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marshalling an unknown Array size

You have a structure that takes a byte array

byte[]

however, the size of that array depends on the image you are submitting (widthxheight)

So... how do you do

[MarshalAs(UnmanagedType.ByValArray, SizeConst = ???)]
public Byte[] ImageData;

Is the sizeconst a MUST HAVE when working with byte arrays being passed from C# to C dlls?

like image 488
Olewolfe Avatar asked Aug 06 '09 16:08

Olewolfe


1 Answers

You need to change the marshalling type. SizeConst is required if you're marshalling as ByValArray, but not with other types. For details, look at the UnmanagedType enum.

My suspicion is that you want to marshall as a C pointer to the array:

[MarshalAs(UnmanagedType.LPArray)]

This will cause it to marshall through to a standard C array (BYTE*), so only a pointer is passed through. Doing this allows you to pass any sized array. Typically, you'll also want to pass through the array size as another parameter (or image width/height/bpp, which provides the same info), since there's no way in C/C++ to tell that easily.

like image 145
Reed Copsey Avatar answered Sep 20 '22 19:09

Reed Copsey