I wrote in C a struct like that
struct IMAGE {
unsigned int x, y;
unsigned char **data;
};
Could anybody please tell me how to marshall this struct to use in C#?
my solution does not work.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class IMAGE
{
public UInt32 x;
public UInt32 y;
public byte[][] data;
};
Managed arrays are different than pointers. A managed array requires the size of the array, and if you’re trying to marshal a struct, it requires a fixed size to marshal directly.
You can use the
SizeConstparameter of theMarshalAsattribute to set the size of data when it gets marshaled.But I’m guessing that
xandyare the dimensions of the image and that the size ofdatadepends on those variables. The best solution here is to marshal it over as anIntPtrand access the data when you need it:If you are allowed to use unsafe code, you can change the
IntPtrto abyte**and work with it directly.With the setter, you’ll probably want to verify the dimensions of the value before you blindly write to unmanaged memory.