nattylife

im trying to create a parallel managed struct in a .net dll from a C application.
in my C app i have a struct with this in it:

struct myStruct
{
.
.
.
char header[7][30];
}

and i wanted a managed version. i havent found much on marshalling an array of
c style strings... but it might be because im not using the right search words.
i saw this example, would this be correct

[StructLayout(LayoutKind.Sequential)]
public struct myManagedStruct
{
.
.
.
[MarshalAs(UnmanagedType.ByValArray,
SizeConst = 7,
ArraySubType = UnmanagedType.ByValTStr)]
public string header;
}

and if it is, how would i marshal over each string in the array to its parallel
managed version



Re: Visual C# General Best way to marshal an array of C style strings

Mattias Sjogren

The SizeConst would have to be set to 7 * 30 = 210. You then have to split the string into pieces of 30 characters.






Re: Visual C# General Best way to marshal an array of C style strings

nattylife

so to marshal over the C strings, i would need to copy them over at offsets in the managed string





Re: Visual C# General Best way to marshal an array of C style strings

Mattias Sjogren

Yes. Another option is to split the field into seven different strings of length 30 if that's more conventient. You can also use a char[] instead of a string if you prefer.