[Mono-list] Strange behavior with P/Invoke and return values
Thu, 28 Aug 2003 11:45:10 -0500
Paolo Molaro wrote:
> On 08/25/03 Patrick Hartling wrote:
>>I am experimenting with mapping C++ to C# via P/Invoke and Mono 0.26 on
>>Red Hat 8.0, and I have run into something odd when a native function
>>returns a C++ object by value. From the way it is behaving, it appears
>>that something is going wrong in the allocation of memory on the stack
>>for the return value.
>>I have attached some code that demonstrates the problem. My intent is to
>>use a custom marshaler to make a copy of the return value object in a
>>freshly allocated C# object reference. (The validity of this technique
>>is what I am evaluating, so it's possible I am going down the wrong road
>> [return : MarshalAs(UnmanagedType.CustomMarshaler,
>> MarshalTypeRef = typeof(ReferenceDataMarshaler))]
>> private static extern ReferenceData Marshal_getReferenceCopy(IntPtr instPtr);
> Does your code work under the MS runtime?
As you point out below, there was a mismatch in the code, but with a
modification, it did work with the MS runtime. It doesn't fix the
problems with Mono, unfortunately.
> I'm not sure it's supposed to work.
> ReferenceData in C# is a class, so by default the marshal code in mono
> (check metadata/marshal.c) assumes the unmanaged function returns a
> pointer. The C++ code, though returns the data by value, so you have a
> mismatch. You may try to define a C# struct that matches ReferenceData
> and use that as the type returned from Marshal_getReferenceCopy().
> In a wrapper you create a ReferenceData obj and copy the fields from the
That sounds like a good idea. Thanks for the tip.
Patrick L. Hartling | Research Assistant, VRAC
firstname.lastname@example.org | 2624 Howe Hall: 1.515.294.4916
http://www.137.org/patrick/ | http://www.vrac.iastate.edu/