Merge pull request #109 from bl8/master

Variant: Fix crash when casting a Variant to string
This commit is contained in:
Bertrand Lorentz 2014-06-17 21:34:04 +02:00
commit 83b602320a
2 changed files with 9 additions and 3 deletions

View file

@ -283,12 +283,12 @@ namespace GLib {
} }
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)] [DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr g_variant_get_string (IntPtr handle); static extern IntPtr g_variant_get_string (IntPtr handle, IntPtr length);
public static explicit operator string (Variant val) public static explicit operator string (Variant val)
{ {
IntPtr str = g_variant_get_string (val.Handle); IntPtr str = g_variant_get_string (val.Handle, IntPtr.Zero);
return str == IntPtr.Zero ? null : GLib.Marshaller.Utf8PtrToString (str); return GLib.Marshaller.Utf8PtrToString (str);
} }
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)] [DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]

View file

@ -27,6 +27,7 @@ namespace sample
var dict = new Dictionary<string, Variant> (); var dict = new Dictionary<string, Variant> ();
dict.Add ("strv", new Variant (strv)); dict.Add ("strv", new Variant (strv));
dict.Add ("unit", Variant.NewTuple (null)); dict.Add ("unit", Variant.NewTuple (null));
dict.Add ("str", new Variant ("String 6"));
variant = new Variant (dict); variant = new Variant (dict);
Console.WriteLine (variant.Print (true)); Console.WriteLine (variant.Print (true));
@ -35,6 +36,11 @@ namespace sample
Console.WriteLine ("unit: " + asv["unit"].Print(true)); Console.WriteLine ("unit: " + asv["unit"].Print(true));
Console.WriteLine ("type: " + variant.Type.ToString ()); Console.WriteLine ("type: " + variant.Type.ToString ());
Variant tmp;
asv.TryGetValue ("str", out tmp);
var str = (string) tmp;
Console.WriteLine ("out str " + str);
} }
public static void Main (string[] args) public static void Main (string[] args)