[Mono-list] C -> C# strings

Bryan Buchanan bryanb@webbtide.com
13 Dec 2004 10:24:19 +1000


Can some please tell me why I cannot return 8 bit strings from C to C#.

Using this example:

#include <gnome.h>

$ cat bjb.c 
gchar *test(void)
        gchar *y = g_strconcat("xxx", "\xFE", "yyy", NULL);
        return y;
$ cat bjb.cs
using System;
using System.Runtime.InteropServices;
public class bjb {
        [DllImport("bjb", EntryPoint="test", CharSet=CharSet.Auto)]
        public static extern string test();
        static void Main() {
                string str = test();
                Console.WriteLine("{0}", str);

If the string in C has a high bit character set (eg xFE) the string
returned is alway null. If I change it to, say, x4E, a valid string is
returned. I've trued CharSet Auto, Ansi, Unicode and none make any
difference. (g_strconcat works fine, the "problem" is in the marshalling
of the C string to return to Mono).