I’m trying to marshal from C++ to C# a struct that looks something like this:
typedef struct FooStruct {
Uint8 bytesPerThingie;
void *arrayOfThingies;
// other members ...
}
So, in this case there are two unknowns:
- The number of elements in the array.
- The size (in bytes) of each element.
I had successfully marshaled the struct itself previously, with a definition like this:
[StructLayout(LayoutKind.Sequential)]
public struct FooStruct {
public byte bytesPerThingie;
public IntPtr arrayOfThingies;
// other members...
}
but now I need to inspect and modify the embedded array.
I understand that
- By itself, an array of blittable elements of a blittable type is
itself blittable, but not when it is used as a field within a
structure. - When marshaling from unmanaged code to the .NET Framework, the array length is
determined from the SizeConst argument, optionally followed by the unmanaged type
of the array elements, if they aren’t blittable.
Even assuming that the elements in the array in this case are of a blittable type, how can I set SizeConst, a compile-time argument, if I can’t know the size of the array until runtime?
Long story short, you can’t. The
SizeConstfield on theMarshalAsAttributeclass is compiled into metadata on the field and cannot be changed at runtime (at least, not in a way that would benefit you).That said, you have the following options:
Marshalclass.unsafeto access the pointer directly (and change your type to use pointers). This requires the/unsafecompiler option which may or may not be an option for you.Note that in all of the cases above, you still have to know the length of the array that is returned (it’s probably in the structure along with the pointer and the type).