I have the following nested structures.
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct ERROR_ITEM
{
byte ErrorID;
};
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct ERROR_DATA
{
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 10)]
ERROR_ITEM[] ErrorItem;
};
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct VCP_DATA
{
[MarshalAs(UnmanagedType.Struct)]
ERROR_DATA ErrorData;
};
I need to copy a byte array to this structure, so I tried the following
vcpBuffer = new VCP_DATA();
GCHandle handle = GCHandle.Alloc(vcpBuffer, GCHandleType.Pinned);
try
{
IntPtr pBuffer = handle.AddrOfPinnedObject();
Marshal.Copy(bytarray, 0, pBuffer, length);
}
finally
{
if (handle.IsAllocated)
handle.Free();
}
But GCHandle.Alloc() returns the error "An unhandled exception of type System.Argument.Execption" occurred in mscorlib.dll. Additional information: Object contains non-primitive or non-blittable data.