[Mono-dev] Specifying MarshallAs for [Out] StringBuilder
kornelpal at gmail.com
Sun Mar 23 13:02:27 EDT 2008
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
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.
----- 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
> I have a native method that I P/Invoke which returns a string. I use an
> StringBuilder to get that returned string in my managed code. But the
> 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
> method which requires platform encoding by manually marshalling it, as
> in the MONO FAQ (using UnixMarshal.StringToHeap).
> But how I do manually unmarshall a StringBuilder ? I tried using
> and MarshalAs(LPTStr) - that did not help.
> - dBera
> Debajyoti Bera @ http://dtecht.blogspot.com
> beagle / KDE / Mandriva / Inspiron-1100
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
More information about the Mono-devel-list