[Mono-list] Passing UTF-8 to a method that takes a .Net string - not possible right?

Jonathan Pryor jonpryor at vt.edu
Wed Oct 13 08:32:04 EDT 2010


On Tue, 2010-10-12 at 22:33 -0700, nev wrote:
> So I've been using Mono.Cairo.Context.ShowText() which takes a string. On
> Linux everything is good, never had a problem.
> 
> But running my assemblies on Windows with the Mono assemblies from the
> Windows Mono installer everything works - except that ShowText() has issues
> with accents and guillemets (and maybe other things).
...
> Is this possible? It takes a .Net string

This is a bug in the binding, and appears to be present for all string
methods.  For example, cairo_show_text() is declared as:

        [DllImport(cairo)]
        internal static extern void cairo_show_text(IntPtr cr, string
        utf8);

As you note, this fails under .NET because .NET will marshal the string
as an "ANSI" string, and ANSI is *never* UTF-8 on Windows.

A possible fix would be to instead do:

        [DllImport(cairo)]
        static extern void cairo_show_text(IntPtr cr, byte[] utf8);
        
        internal static void cairo_show_text(IntPtr cr, string utf8)
        {
                var e = Encoding.UTF8;
                byte[] data = new byte[e.GetByteCount(utf8) + 1];
                e.GetBytes (utf8, 0, utf8.Length, data, 0);
                cairo_show_text (cr, data);
        }

..and repeat for all other methods which require UTF-8 strings.

(The above could doubtlessly be improved some, but it demonstrates the
basics of what needs to be done.)

You should probably file a bug for this so that it can be tracked, etc.

 - Jon




More information about the Mono-list mailing list