[Mono-list] Interop problem: ** ERROR **: Type System.MarshalByRefObject which is passed to unmanaged code must have a StructLayout attribute
Frederik Carlier
frederik.carlier at carlier-online.be
Mon Feb 26 10:32:19 EST 2007
Robert,
Thanks. So it is a "bug" (or at least something that could be improved
upon) in the MS framework, then. But indeed, the basic problem is this:
I need to set some callbacks from unmanaged code. The callback should
contain a stream (or a reference to it) as a parameter.
Is there any way this can be achieved (I heard about "pinning", but is
that something that I can use here)? Another solution would be to use a
dictionary in managed memory and use the key as the parameter on the
callback funcion, but then again that seems like some overkill to me.
Any ideas?
Frederik.
Robert Jordan schreef:
> Frederik Carlier wrote:
>
>> Hi all,
>>
>> I have, unfortunately, another Interop problem with Mono. Using a struct
>> like this:
>>
>> [StructLayout(LayoutKind.Sequential)]
>> internal struct StreamSource
>> {
>> private TidyBuffer buffer;
>> private Stream stream;
>> }
>> , I have the following code:
>>
>> StreamSource data = new StreamSource();
>> data.stream = stream;
>> IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(data));
>> Marshal.StructureToPtr(data, ptr, true);
>>
>> which runs fine on Windows and Microsoft .NET, but crashes badly on Mono:
>>
>> ** ERROR **: Type System.MarshalByRefObject which is passed to unmanaged
>> code must have a StructLayout attribute
>> aborting...
>> Stacktrace:
>>
>> at (wrapper managed-to-native)
>> System.Runtime.InteropServices.Marshal.StructureToPtr
>> (object,intptr,bool) <0x00004>
>> at (wrapper managed-to-native)
>> System.Runtime.InteropServices.Marshal.StructureToPtr
>> (object,intptr,bool) <0xffffffff>
>> at (...)
>>
>> So, obviously, the question is: what am I doing wrong, if anything?
>>
>
> Your struct is containing a Stream object which can't be
> safely passed to unmanaged code. MS.NET 2.0 doesn't seem to
> care in this case (1.1 does), but the code will definitely break
> if you're relying on the stream being marshaled back correctly
> (MS.NET has a moving/compacting GC).
>
> Please file a bug for the ** ERROR ** on the 2.0 profile
> with a compilable test case, but also consider changing the
> struct because it's definitely not suitable for p/invoke.
>
> Robert
>
> _______________________________________________
> Mono-list maillist - Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>
More information about the Mono-list
mailing list