[Mono-dev] Problem marshalling unions

Robert Jordan robertj at gmx.net
Thu Jan 19 15:43:30 EST 2006


David,

> I am having a problem marshalling unions using mono 1.1.13.

See

http://bugzilla.ximian.com/show_bug.cgi?id=77230

Robert

> 
> If I have a C union like this
> 
> union MYUNION
> {
>     int number;
>     double d;
> }
> 
> According to the MSDN docs, I should marshal it like this
> 
> // Declares managed structures instead of unions.
> [ StructLayout( LayoutKind.Explicit )]
> public struct MyUnion
> {
>    [ FieldOffset( 0 )]
>    public int i;
>    [ FieldOffset( 0 )]
>    public double d;
> }
> 
> Note that the Fieldoffset is the same for both fields. I have verified that
> this works fine under .NET 1.1.
> 
> When I run this under mono, I get this error
> 
> "Type MYUNION which has an [ExplicitLayout] attribute cannot have two
> fields with the same offset."
> 
> This error comes from  \mono\metadata\marshal.c line 1353
> if (((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) ==
> TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) && (usize == 0))
>                   g_error ("Type %s which has an [ExplicitLayout] attribute
> cannot have two fields with the same offset.", mono_type_full_name
> (&klass->byval_arg));
> 
> Is this check correct? For normal structs fieldoffsets should be different
> but for unions they can be the same.
> 
> Thanks
> Dave




More information about the Mono-devel-list mailing list