[Mono-dev] Specifying MarshallAs for [Out] StringBuilder
Kornél Pál
kornelpal at gmail.com
Sun Mar 23 15:40:14 EDT 2008
Hi,
Sorry it's late night here and I forgot about [Out].:)
In this case you only need to allocate a buffer large enough to hold the
string using AllocHeap, call PtrToString on the result, and FreeHeap on the
buffer.
Kornél
----- Original Message -----
From: "Kornél Pál" <kornelpal at gmail.com>
To: "Debajyoti Bera" <dbera.web at gmail.com>;
<mono-devel-list at lists.ximian.com>
Sent: Sunday, March 23, 2008 6:02 PM
Subject: Re: [Mono-dev] Specifying MarshallAs for [Out] StringBuilder
CharSet.Auto is UTF-8 for strings as well as for StringBuilder. So you have
to use some custom marshaling like UnixMarshal.StringToHeap.
Strings are read-only. StringBuilder is read-write. When marshaling UTF-16
the internal buffer of StringBuilder can be used so no buffer copy is
required. But when character conversion is required a new buffer has to be
allocated. StringBuilder.ToString should be used to obtain the string and if
you need the result in the StringBuilder, set Length to zero and Append the
new string.
Note that you may need a modified version of StringToHeap that allocates
larger buffer than the actual string as required by the P/Invoked function.
Kornél
----- Original Message -----
From: "Debajyoti Bera" <dbera.web at gmail.com>
To: <mono-devel-list at lists.ximian.com>
Sent: Sunday, March 23, 2008 2:59 PM
Subject: [Mono-dev] Specifying MarshallAs for [Out] StringBuilder
> Hi,
> I have a native method that I P/Invoke which returns a string. I use an
> [Out]
> StringBuilder to get that returned string in my managed code. But the
> native
> method returns the string in platform encoding and not utf8 and when the
> P/Invoke returns, the StringBuilder is empty !
> I read somewhere that by default mono marshalls and unmarshalls strings as
> utf8 so this is not surprising. I have been able to pass strings to
> another
> method which requires platform encoding by manually marshalling it, as
> shown
> in the MONO FAQ (using UnixMarshal.StringToHeap).
> But how I do manually unmarshall a StringBuilder ? I tried using
> Charset.Auto
> and MarshalAs(LPTStr) - that did not help.
>
> Thanks,
> - dBera
>
> --
> -----------------------------------------------------
> Debajyoti Bera @ http://dtecht.blogspot.com
> beagle / KDE / Mandriva / Inspiron-1100
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
_______________________________________________
Mono-devel-list mailing list
Mono-devel-list at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list
More information about the Mono-devel-list
mailing list