Marching an unknown array size

You have a structure that takes a byte array

byte[] 

however, the size of this array depends on the image you are sending (wid thanksheight)

So ... how do you do it

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

Is sizeconst MUST HAVE when working with byte arrays passed from C # to C dll?

+7
arrays c # marshalling size
source share
1 answer

You need to change the sort type. SizeConst is required if you are sorting as ByValArray, but not with other types. See the UnmanagedType enum for more details.

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

 [MarshalAs(UnmanagedType.LPArray)] 

This will move to the standard C array (BYTE *), so only the pointer is skipped. This allows you to transfer an array of any size. As a rule, you will also want to pass the size of the array as another parameter (or image width / height / bpp, which provides the same information), since in C / C ++ it is not so easy to say.

+2
source share

All Articles