[Mono-list] Problem with DateTime class from C code

Fede federico.omoto at gmail.com
Sun Oct 24 13:06:14 EDT 2010


Robert, thank you again for your answer!

Ok, here's my code, I've tested it on Mono 2.6.4 Linux x86_64 (Fedora 13)
and on Mono 2.8 Windows XP 32bit (replacing g_free() with a mono_free()
call):

#include <mono/jit/jit.h>
#include <mono/metadata/object.h>
#include <mono/metadata/environment.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/debug-helpers.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>

int main(int argc, char* argv[])
{
        MonoDomain *domain;
        MonoClass *class;
        MonoObject *object;
        MonoMethod *method;
        MonoString *string;
        char *pointer;

        domain = mono_jit_init (argv[0]);

        class = mono_class_from_name(mono_get_corlib(), "System",
"DateTime");
        object = mono_object_new(domain, class);
        method = mono_class_get_method_from_name(class, "ToString", 0);
        string = (MonoString*)mono_runtime_invoke(method, object, NULL,
NULL);
        pointer = mono_string_to_utf8(string);
        printf("%s\n", pointer);
        g_free(pointer);

        mono_jit_cleanup (domain);
}

As you said, sometimes I get the correct string, but if you execute it many
times you'll see it returning different strings (in my case ranging from
"1/1/0001 12:00:00 AM" to "1/1/0001 12:00:03 AM").

Thank you in advance,


Federico Omoto



On Sun, Oct 24, 2010 at 9:32 AM, Robert Jordan <robertj at gmx.net> wrote:

> On 23.10.2010 19:18, Fede wrote:
> > Hi Robert, thank you for your answer!
> >
> > I've tried calling the default ctor using mono_runtime_object_init() on
> the
> > DateTime instance but I get an assertion:
> >
> > ERROR:object.c:95:mono_runtime_object_init: assertion failed: (method)
> > Stacktrace:
>
> Yes, my bad. DateTime does not have an explicit default ctor
> because it's a value type. Hence the assertion.
>
> Your code is correct as far I can tell from just looking at it.
> A small test case of mine also returns the correct string
> on both 2-6 and 2-8 Mono versions.
>
> Please post something compilable and don't forget to mention the
> Mono version and the platform.
>
> Robert
>
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20101024/26223bcb/attachment.html 


More information about the Mono-list mailing list