Marshalling a char array in a structure from C to .NET

B

Betty

When marshalling a char array in a structure from C to .NET, I get the
following error:
System.ArgumentException: Type could not be marshaled because the
length of an embedded array instance does not match the declared
length in the layout.

namespace RemoteClientNew
{
[StructLayout(LayoutKind.Sequential)]
public struct contacts
{
[MarshalAs UnmanagedType.ByValArray,
ArraySubType=UnmanagedType.AnsiBStr, SizeConst=10)] public byte[]
name;
public int age;
public int id;
}
}
The name field in C code also has a size of 10.


Trying using UnmanagedType as LPArray or ByValTStr with CharSet.Ansi
gives the following error:

System.TypeLoadException: Can not marshal field name of type
RemoteClientNew.con
tacts: This type can not be marshaled as a structure field.
 
G

Guest

Hi Betty,

One thing that I notice is that your ArraySubType is AnsiBstr which is
incorrect. It should be :
UnmanagedType.U1

HTH,
Tom T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top