[Mono-list] Marshalling array of structs?

J. Perkins jason@379.com
30 Jan 2003 16:15:50 -0500


A general C# question, hope it isn't out of place here but I haven't had
much success with the MS folks.

I have a function that takes a generic array (I am using System.Array
right now but I can change that if needed) as a parameter. I know that
it is an array of structs with the LayoutKind.Sequential attribute, but
I don't know their exact format. I can get the stride and length of the
array, so I know how many bytes it takes up. Now I need to copy the
contents of the array to either unmanaged memory or a byte array. I can
call out to a C function if necessary. For the life of me I can't figure
out how to do it. I have tried Marshal.StructureToPtr(),
Buffer.BlockCopy(), and all kinds of MarshalAs attributes but I always
get some type of casting or argument exception complaining about a lack
of layout information.

Has anyone accomplished this, and if so how the heck did you do it? It
seems like it should be possible. Thanks for any advice.

Jason
379