[Mono-list] Mono Boolean to C bool

Robert Jordan robertj at gmx.net
Sun Nov 16 11:40:05 EST 2008


Kevin Heeney wrote:
> I am using C to invoke Mono and it is working well.  A few weeks ago I 
> hit an issue where I was calling a few functions that returned a boolean 
> from Mono.  I changed those functions to return an integer ( I cast the 
> boolean to an int and did Math.Abs on that int in Mono) and just set the 
> returned int to a bool in C.  That worked fine, but now I am 
> re-evaluating and cleaning some things up.  What would be the best way 
> to take a MonoObject* referencing a Boolean and get a C bool out of it 
> (or a integer representing the value of that boolean).
> 
> I have a couple of ideas for ways to do this that I know would work, but 
> I have a feeling there is an easier way.
> 
> In other words, how can I get a C bool (or an int where false=0 and true 
> is anything else) out of the monOb in this code:
> 
>         MonoMethodDesc *monoMethDesc2 =  mono_method_desc_new 
> (":callSomeMethodThatReturnsABoolean()",FALSE);
>         MonoMethod *meth2=     mono_method_desc_search_in_class 
> (monoMethDesc2, myMonoClass);
>         MonoObject* monOb =  mono_runtime_invoke (meth2, 
> theObjectThatOwnsTheMethodThatReturnsABoolean, NULL, NULL);

You must unbox value types because mono_runtime_invoke is
always returning value types boxed as objects:

if (monObj) {
	MonoBoolean *b = (MonoBoolean*) mono_object_unbox (monOb);
	if (*b) {
		--> true
	} else {
		--> false
	}
} else {
	// handle error condition
}

Robert



More information about the Mono-list mailing list