[Mono-dev] Marshalling Structures (size = sizeof-1)

William Lahti xfurious at gmail.com
Mon Sep 11 23:01:26 EDT 2006


First, the structure in question:

[StructLayout(LayoutKind.Sequential)]
public struct XVisualInfo {
        public IntPtr visual;
        public XVisualID visualid; // enum XVisualID: uint { Zero }
        public int screen;
        public int depth;
        public int c_class;
        public uint red_mask;
        public uint green_mask;
        public uint blue_mask;
        public int colormap_size;
        public int bits_per_rgb;
}

Now, we use some unmanaged code to turn a pointer to an array of XVisualInfo
into a managed one. At first I was using:

Where "items" is the number of items in the array, and "arr_ptr" is the
XVisualInfo* pointer:

for (int x = 0; x < items; ++x) {
  arr[x] = *arr_ptr;
  ++arr_ptr;
}

The first item was marshalled fine, but the second item was offset by one
byte (IE, mono read one byte of memory before it should have)
. Marshal.SizeOf() produced the correct size (40 bytes). I changed the code
to do it's own measuring about the size using SizeOf:

Where arr_ptr is "byte *", instead of "XVisualInfo*":

for (int x = 0; x < items; ++x) {
  arr[x] = *(XVisualInfo*)arr_ptr;
  arr_ptr += Marshal.SizeOf(typeof(XVisualInfo));
}

And then it worked fine. I don't know if it's new, been fixed or if I am
missing the reason it doesn't work. I'm sorry I don't have time to search
through bug-o-matic :-\.

My mono is 1.1.13.1.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20060911/03060d0b/attachment.html 


More information about the Mono-devel-list mailing list