[Mono-dev] Seeking advice on converting basic value types from the CLR representation to native C types, using mono embedding

Jean-Michel.Perraud at csiro.au Jean-Michel.Perraud at csiro.au
Thu Oct 11 02:45:11 UTC 2012


I am working on a package to access the Mono (resp. MS.NET) CLR from the R statistical language. I am trying to convert 'simple' CLR value types (e.g. string, double, bool, DateTime) to marshall them to their R equivalent. I managed to deal with the 'bool', but stumbled on the DateTime

I think Mono's print_field_value in debug-helpers.c seems the closest I can infer from, to mimic it. But I have trouble doing so. I understand little of what I get returned from mono_object_unbox. Any guidance on how to go about it is welcome. In return, I'd like to contribute some 'howto' documentation to Mono.

The task is logged at http://r2clr.codeplex.com/workitem/14 for background info.

You can get the complete source code from http://r2clr.codeplex.com if you wish, but I include below code that should convey the idea.




type_il = mono_type_get_type(mono_class_get_type(mono_object_get_class(pobj)));

switch(type_il)  /*MonoTypeEnum*/ {

case MONO_TYPE_R8             :

      result = mkNumericSexp(1, (double*)mono_object_unbox(pobj));


case MONO_TYPE_STRING         :

      str = mono_string_to_utf8((MonoString*)pobj); // this one should work; yet to pass formal unit test.

      result = mkCharSexp(1, &str);



case MONO_TYPE_BOOLEAN        : // this one seems to work

      result = mkBoolSexp(1, (int*)mono_object_unbox (pobj));




            if(strcmp( klassName, "DateTime") == 0)


                  double * valSinceNinteenhunds = getRDateOffset(pobj); // this seems very off.

                  result = mkDateSexp(1, valSinceNinteenhunds);





double * getRDateOffset(CLR_OBJ * pobj) {

      MonoMethod * method = rclr_mono_get_method( spTypeClrFacade, "GetRDateDoubleRepresentation", 1); // C# GetRDateDoubleRepresentation returns a double, a TimeSpan.TotalDays

      void ** params = (void**) &(pobj);

      MonoObject * exception;

      // FIXME: seems I should seek gint64; Where is it defined in the mono distribution include headers? seems absent from glib.h and anywhere else.

      return (double*)mono_object_unbox(mono_runtime_invoke(method, NULL, params, &exception)); }

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-devel-list/attachments/20121011/1a3a13cd/attachment.html>

More information about the Mono-devel-list mailing list