Search code examples
c#.netpinvokemarshalling

How to marshal struct containing pointer to first element of a C-style array of unknown type


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:

  1. The number of elements in the array.
  2. 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

  1. 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.
  2. 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?


Solution

  • 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 the contents manually as you've been doing using the methods on the Marshal class.
    • Use 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.
    • Use C++/CLI to create a managed wrapper in C++ that will export .NET types, but handle the marshaling in C++ (which might be easier, depending on your comfort level and the complexity of the API you are trying to access).

    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).