Reputation: 4696
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:
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
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?
Upvotes: 3
Views: 1373
Reputation: 74540
Long story short, you can't. The SizeConst
field on the MarshalAsAttribute
class 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:
Marshal
class.unsafe
to access the pointer directly (and change your type to use pointers). This requires the /unsafe
compiler 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).
Upvotes: 3