[Mono-list] Unboxing a MonoObject

Paolo Molaro lupus@ximian.com
Thu, 12 Jun 2003 17:30:33 +0200


On 06/10/03 Candace Bain wrote:
> I need to unbox the MonoObject pointer that mono_runtime_invoke returned
> to me to get the return value of the function.  If I'm expecting a
> string I've found that the following code will work:
> 
> MonoString * str = NULL;
> gchar *message = (char *) "";
> 
> ....
> 
>   str = (MonoString *)mono_runtime_invoke (method, test, NULL, NULL);
>   if ( str )
>     {
>       message = mono_string_to_utf8(str);
>     }
> 
> But it's not clear to me how to unbox a MonoObject to get to a bool,
> double, or int value.  Thanks in advance for any pointers about how to
> do this.

I just added in cvs an API you can use to get a pointer to the data that
you'll need to dereference:

	gpointer mono_object_unbox (MonoObject *obj);

So, for a C# int, you'd do:

	gint32 *intval = mono_object_unbox (result);
	g_print ("got %d\n", *intval);

The implementation is just:

	(char*)obj + sizeof (MonoObject)

if you're not using cvs, but I suggest you switch to the API as soon as
we make the new release.

lupus

-- 
-----------------------------------------------------------------
lupus@debian.org                                     debian/rules
lupus@ximian.com                             Monkeys do it better