1

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.

2 Answers 2

1
vcpBuffer = new VCP_DATA();
GCHandle handle = GCHandle.Alloc(bytearray, GCHandleType.Pinned); 
try 
{ 
    IntPtr pBuffer = handle.AddrOfPinnedObject(); 
    vcpBuffer = (VCP_DATA)Marshal.PtrToStructure(pBuffer, typeof(VCP_DATA)); 
} 
finally 
{ 
    if (handle.IsAllocated) 
        handle.Free(); 
}
Sign up to request clarification or add additional context in comments.

Comments

0

First of all, ERROR_ITEM[] is a managed array, so that's not a blittable structure. It's just a managed reference. The memory that reference points to has a syncblock, method table pointer, and a length specifier sitting in front of the actual elements.

However, using 'fixed' (https://msdn.microsoft.com/en-us/library/zycewsya.aspxhttps://msdn.microsoft.com/en-us/library/zycewsya.aspx) isn't going to help (but check me on that). To get past this error, since ERROR_ITEM[] is of a fixed length, just replace the array with 16 of those ERROR_ITEM fields. You can still use array syntax against the address of the first ERROR_ITEM (ERROR_ITEM*) to access subsequent elements.

Alternately, just compute the size of all 16 elements, but include only the first one as a field, then specify the Size parameter on the StructLayout attribute for ERROR_DATA so that it's big enough to hold them all.

Also, Resharper sometimes whines about nested stuff when the actual compiler is perfectly happy with it. But this is caused by it being an array. Even a fixed unsafe embdedded array makes C# think it's unblittable in my experience.

2 Comments

Thanks for the suggestions. I am sure they will work, but I found another way of doing what I needed. vcpBuffer = new VCP_DATA(); GCHandle handle = GCHandle.Alloc(bytearray, GCHandleType.Pinned); try { IntPtr pBuffer = handle.AddrOfPinnedObject(); vcpBuffer = (VCP_DATA)Marshal.PtrToStructure(pBuffer, typeof(VCP_DATA)); } finally { if (handle.IsAllocated) handle.Free(); }
@Hassan - You should make that an answer and accept it! It's legitimate to do that and improves the site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.