[Mono-devel-list] ToString() in DateTime failed on embedded mono

eric lindvall eric at 5stops.com
Tue Jul 8 13:12:50 EDT 2003


The exact exception you're running into is that Thread.CurrentThread is
returning null. The reason for this is the environment isn't fully setup.

It isn't documented anywhere, but you have to call
mono_runtime_exec_managed_code() after you do your mono_jit_init(), and
have your C function as the callback to mono_runtime_exec_managed_code().

I haven't gotten any response as to if there are other restrictions, which
have lead me to some questions:

- once i've made the call to mono_runtime_exec_managed_code(), can i then run
  code in the main thread?

- once i've made the call to mono_runtime_exec_managed_code(), can i use
  pthread_create() in the main thread and then use
  mono_thread_attach(domain)?

it would really be nice if the embeded-api.html was updated to include all
of the requirements to being able to embed mono.. it took me a few days to
figure out this problem.

e.


On Tue, 08 Jul 2003, Mohammad DAMT wrote:

> I have test.cs:
> using System;
>                                                                                                                                
> namespace Test
> {
>     public class Test
>     {
>         public String test ()
>         {
> 		return DateTime.Now.ToString()
>         }
>     }
> }
> 
> 
> and test.c
> #include <mono/jit/jit.h>
> #include <mono/metadata/debug-helpers.h>
>                                                                                                                                
> char* process()
> {
>     MonoDomain *domain;
>     MonoAssembly *assembly;
>     MonoClass *class;
>     MonoMethod *method;
>     MonoMethodDesc *desc;
>     MonoObject *mono_ret, *exception = NULL;
>     gpointer arguments [4];
>                                                                                                                                    
> domain = mono_jit_init ("test");
>     assembly = mono_domain_assembly_open (domain, "test.dll");
>     if (!assembly) {
>         fprintf (stderr, "Assembly not loaded\n");
>         mono_jit_cleanup (domain);
>         return NULL;
>     }
>                                                                                                                                    
> class = mono_class_from_name (assembly->image, "Test", "Test");
>     if (!class) {
>         fprintf (stderr, "Class not loaded\n");
>         mono_jit_cleanup (domain);
>         return NULL;
>     }
>                                                                                                                                    
> desc = mono_method_desc_new (":test()", 0);
>                                                                                                                                    
> method = mono_method_desc_search_in_class (desc, class);
>     if (!method) {
>         fprintf (stderr, "Method Desc not loaded\n");
>         return NULL;
>     }
>                                                                                                                                    
> mono_ret = mono_runtime_invoke (method, NULL, NULL, NULL);
>     fprintf(stderr,"xxx\n");fflush(stderr);
>                                                                                                                                    
> mono_jit_cleanup (domain);
>     return (char *) mono_string_to_utf8((MonoString*) mono_ret);
>                                                                                                                                
> }
>                                                                                                                                
> void main () {
>     char *s;
>                                                                                                                                    
> s = process ();
>     fprintf(stderr, "%s\n", s);
> }
> 
> compile both with:
> mcs /t:library  test.cs -out:test.dll
> and
> gcc -o t test.c `PKG_CONFIG_PATH=/opt/mono/lib/pkgconfig pkg-config 
> --cflags --libs mono`
> 
> when I ran the program:
> [mdamt at gordon src]$ ./t
>  Unhandled Exception: System.NullReferenceException: A null value was 
> found where an object instance was required
> in <0x0000f> 00 System.Globalization.DateTimeFormatInfo:get_CurrentInfo 
> ()
> in <0x00066> 00 System.Globalization.DateTimeFormatInfo:GetInstance 
> (System.IFormatProvider)
> in <0x0002b> 00 System.DateTime:ToString (string,System.IFormatProvider)
> in <0x00017> 00 System.DateTime:ToString ()
> in <0x00039> 00 Test.Test:test ()
> 
> [hangs]
> 
> question 1:
> What I expect is the program showing DateTime.Now.ToString() string. If 
> I change DateTime with something like
> "abc".ToString(), it returns and print "abc".
> Did I miss something?
> 
> question 2:
> what is the best method to catch the exception like this without 
> hanging?
> 
> thanks



More information about the Mono-devel-list mailing list