[Mono-list] Re: [Mono-devel-list] Managed/Unmanaged Code Interop Documentation

Paolo Molaro lupus at ximian.com
Thu Sep 4 10:22:29 EDT 2003


On 09/03/03 Jonathan Pryor wrote:
> On Wed, 2003-09-03 at 05:21, Paolo Molaro wrote:
> <snip/>
> > In some cases, no marshaling and no copy of data happens, for example
> > when the data is blittable most of the time it's enough to pass a
> > pointer to the managed representation.
> 
> How can you do that?  The only time I can see this happening is when
> you're passing a pointer to a structure (using ref or out).

Yes, but you can review any unmanaged API and you'll see that that
is the case most of the time, structs are almost always passed by
reference, not by value in C code.

> > >    <p>What's the memory management policy for using "string" as a return
> > >    value?  Does the runtime expect to free it?</p>
> > 
> > Yes, though I don't remember if we insert the free() yet: note we will
> > use free by default on unix-like platforms and the MS-specified free
> > routine on windows.
> 
> Would it be more .NET-compatible if I said that unmanaged memory were
> freed from within the runtime "as if" via
> System.Runtime.InteropServices.Marshal.FreeCoTaskMem (IntPtr)?
> 
> Now, on Unix this could call the platform-native free(3), but under
> Win32 this could call CoTaskMemFree().

Yes, please include this info.

> > >    <p>The immediate net effect of this is that you can't have string and array
> > >    members of marshaled classes.</p>
> > > 
> > >    <p>It's not quite as bad as this makes out.  You can't pass strings and
> > >    arrays BY DEFAULT.  If you help the runtime marshaler by addorning the
> > 
> > I'm not sure this is right: you can use strings and arrays in types that
> > will be marshaled and by default they will be converted to pointers to
> > the data.
> >
> > 	string -> char* (or gunichar2*, depending on the charset property)
> > 	int[]  -> gint32*
> > 	etc.
> 
> That was my argument for "BY DEFAULT."  By default, there is no
> StructLayout attribute, and hence there is no CharSet specified.

There is always a default charset value, Charset.Auto, even with no
StructLayout attribute. So, by default, it works.

> If CharSet isn't set, is there a default marshal format for strings and
> arrays?  I didn't think there was.  (Platform-native would be a sensible
> default, but since platform-native is UCS-2 on NT-based platforms and
> Ansi on Win9x-based platforms, this wouldn't be particularly developer
> friendly, so requiring that the CharSet be specified would be a good
> idea regardless.)

Yes, the P/Invoke specification (or lack thereof) is a mess. It was done
by people that didn't thought through the portability issues and this is
probably one of the reasons the MS CLR is not really supported on
9x-based platforms. Note: P/Invoke is intrinsically non-portable, the
main issue is that P/Invoke is poorly defined in a non-win32 system.
Think of Charset: they allow Ansi and Unicode (with Auto meaning one or
the other according to the platform), but the world uses also other
encodings. At the very least they should have added a Charset.Encoding
or something, with the actual encoding specified separately as a string,
for example (good luck, though, finding a UCS4 encoding
implementation in the base assemblies...).
</endofrant>

lupus

-- 
-----------------------------------------------------------------
lupus at debian.org                                     debian/rules
lupus at ximian.com                             Monkeys do it better



More information about the Mono-devel-list mailing list