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?
SizeConst. You quite likely have to do the marshalling yourself.