[Mono-list] Mono Boolean to C bool
Kevin Heeney
koheeney at gmail.com
Sun Nov 16 16:34:49 EST 2008
Thanks. That worked. I didn't realize there was a MonoBoolean. I
just knew of MonoObject, MonoString and MonoArray for managed
objects. Is there any API documentation on top of the embedding mono
page?
Thanks,
Kevin
On Nov 16, 2008, at 10:40 AM, Robert Jordan wrote:
> 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
>
> _______________________________________________
> Mono-list maillist - Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
More information about the Mono-list
mailing list