I have the folowing unmanaged structure.
struct MyNativeStruct_Foo
{
char product_name[4];
char product_model[2];
}
And the managed equivalent
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct MyManagedStruct_Foo
{
[MarshalAs(UnmanagedType.LPStr, SizeConst = 4)]
public string product_name;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 2)]
public string product_model;
}
Unfortunately the stings are not null terminated. All 4 characters for the product name are used. If I define the managed structure with "LPStr" the string have to be 0 terminated.
Is there another way to define the managed structure? It is possible to define a custom Marshaller attribute? Or do you have other Ideas?
Note; The native structure can not be changed.
Thanks
Amberg
[EDIT] changed to LPStr (comment Jason Larke)