Marshaling a C++ two-dimensional fixed length char array as a structure member
c#, c++, marshalling, multidimensional-array, struct
Solution
Try using multiple C# structs:
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct MyStruct_Name
{
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 25)]
public string name;
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct MyStruct
{
public int siOrder;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 6)]
public MyStruct_Name aaszNames;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 6, ArraySubType = UnmanagedType.I4)]
public int[] siId;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 6, ArraySubType = UnmanagedType.I4)]
public int[] siTones;
}
This is how I've been passing arrays of C-style strings around.
Don't forget to create the contents of aaszNames! The marshaller hates null references.
MyStruct foo = new MyStruct();
for (int i = 0; i < 6; i++)
{
foo.aaszNames[i] = new MyStruct_Name();
foo.aaszNames[i].name = "";
}
Good luck!
Problem
I am trying to call an unmanaged C++ function, that has a structure as an input parameter. The structure is defined in the header file like this: ``` struct MyStruct { int siOrder; char aaszNames[6][25]; int siId[6]; int siTones[6]; }; ``` I tried to declare the managed struct as following: ``` [StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Ansi)] public struct MyStruct { public int siOrder; [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst=150)] public string aaszNames; [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=6, ArraySubType=UnmanagedType.I4)] public int[] siId; [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=6, ArraySubType=UnmanagedType.I4)] public int[] siTones; } ``` But without any success. I am guessing that the marshaling fails, since the aaszNames is actually an array of six 25 long null-terminating strings. I tried declaring aaszNames as ``` [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=150)] public char[] aaszNames; ``` filling the array with nulls where necessary. But, again, nothing. Is there something I am missing? What am I dong wrong? What is the best way to marshal this 2-D char array? Any hints, please.