[Mono-dev] Embedded mono; getting in C a pointer from an IntPtr object

Zoltan Varga vargaz at gmail.com
Mon Feb 18 11:52:08 UTC 2013


 If you have an IntPtr object, you can simply unbox it, that will return a
pointer to the value boxed inside the object, i.e., then you can
dereference it to get the value:

void *ptr = *(gpointer*)mono_object_unbox (obj);


On Mon, Feb 18, 2013 at 11:46 AM, <Jean-Michel.Perraud at csiro.au> wrote:

> Hi,
> I have a scenario where some C function is getting a MonoObject (*pobj)
> that happens to be an IntPtr. As the native pointer is known to be of a
> certain native type (SEXP), how do I retrieve it? I tried a few things, the
> latest being:
>         type_il =
> mono_type_get_type(mono_class_get_type(mono_object_get_class(pobj)));
>         switch(type_il)  /*MonoTypeEnum*/
>         {
>         case MONO_TYPE_I                : // IntPtr, that we assume to be
> a SEXP as coming from R.NET.                                  result =
> (SEXP)(void*)mono_object_unbox(rclr_mono_call_inst_method("ToPointer",
> pobj, NULL, 0));
>                 break;
> I soon get an access violation not long after the function return.
> I have a working solution when hosting the Microsoft CLR but this is not
> directly transferable to Mono given the significantly different APIs.
> Regards
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-devel-list/attachments/20130218/25ceba53/attachment.html>

More information about the Mono-devel-list mailing list