[Mono-list] Trouble with utf-16 marshaling

Maser, Dan Dan.Maser at inin.com
Mon Jul 2 12:15:56 EDT 2007

  Thanks to the ideas suggested by those who responded, I have a little
more info on this.  I was trying to make a smaller test project that
reproduced this so I could submit a bug and found out that the real
problem is slightly different than I originally reported.  The real
problem isn't with simple marshaling of UTF-16 data using the
UnmanagedType.LPWStr, but rather the problem is with some SWIG code that
uses some function pointer tricks to allow C library functions to return
pointers-to-buffers without leaking memory.

   The trick I refer to is where the C Library has a pointer-to-function
that allocates string data for C# strings.  And the C# stub sends a
delegate function implementation to the C library to use for this - and
thus the string return values are managed by the C# world and not

   I've got a very small test project that reproduces this.  I'll create
a bug and attach it shortly.  Two bugs, really.  Because as I created
this small test project I notice that if I compile the project with MS
visual studio it's ok but if I compile with mono the test project
crashes well before the spot I'm trying to test at.

   I'll create two bugs for this and send the links in case any brave
souls would like to take a look.

-----Original Message-----
From: mono-list-bounces at lists.ximian.com
[mailto:mono-list-bounces at lists.ximian.com] On Behalf Of Robert Jordan
Sent: Friday, June 29, 2007 9:02 PM
To: Mono-list at lists.ximian.com
Subject: Re: [Mono-list] Trouble with utf-16 marshaling

Hi Dan,

Maser, Dan wrote:

Here are you speaking about "out" marshaling:

>    It works properly in windows with MS .NET, but doesn't work for me
> linux with mono.   I've verified in gdb that the C library is
> the correct string, but immediately after the C dll returns and mono
> does the LPWStr marshaling the string is total garbage characters.   I
> am under the impression from previous posts that 2-byte UTF-16 should
> marshal properly to mono with the LPWStr attribute.  In fact it looks
> like some of the gdiplus calls use that same thing and work... any
> what I can check on because mine doesn't?

But your sample is about "in" marshaling:

>    For more clarification my C library has a function signature like
> this:
> void my_function(unsigned short* myArg);
>     And my C# code looks like this:
> [DllImport("myCLib")]
> public static extern void
> string myArg);

Please post, or better: file a bug with a self-contained and
compilable sample.


Mono-list maillist  -  Mono-list at lists.ximian.com

More information about the Mono-list mailing list