[Mono-dev] Creating AppDomains From Embedded Mono

Robert Jordan robertj at gmx.net
Tue Jan 10 23:30:46 EST 2006


> I seem to have fixed my build problems: I ran make
> again without changing anything and managed to build
> the mono 1.1.13 tree in cygwin with the mono 1.1.13
> windows installation. Slightly disconcerting as it
> crashed the last time I tried it...
> I can run the Second Life simulator again and have it
> load the new 1.1.13 mono.dll, run ilasm.exe and my
> bytecode transformer, but the evil hack I've been
> using to create new AppDomains for the Mono scripts
> seems to be unravelling.
> To create a domain I execute an assembly in the root
> domain which creates another domain and executes
> another assembly:
> public static void Main() 
> {
>   AppDomain new_domain =
>   AppDomain.CreateDomain("Secondary Domain" +
> sDomainIndex++);
>   new_domain.ExecuteAssembly("DomainRegister.exe");
> }
> DomainRegister.exe then just makes an internal call
> which uses mono_domain_get to give me a C reference to
> the new domain, which is set up properly by the C#
> code. mono_domain_set can then be used from C code to
> switch
> between secondary domains without any problems.
> public class DomainRegister
> {
> [MethodImplAttribute(MethodImplOptions.InternalCall)]
>   public extern static void register_domain();
>   public static void Main()
>   {
>     register_domain();
>   }
> }	
> Everything I tried to do in C to create the domain
> left it partially initialised and things would crash
> later on.
> To unload domains I just call
> ves_icall_System_AppDomain_InternalUnload which I have
> to declare as an extern as it's not in the public
> headers. Again, it's a hack, but it seems to work.
> Does anyone know what might have changed recently to
> cause this to crash on Windows when the
> AppDomain.CreateDomain call is made?
> Even better, is it now possible to create properly
> initialised AppDomains using the embedding API without
> having to call in to managed code and then back in to
> unmanaged code again?

No, but you can and *should* invoke the managed AppDomain
methods to load and unload domains. You don't need an
intermediate managed assembly to do that (untested):

createDomain (char *name)
     MonoClass *ad_class;
     MonoMethod *create_domain_method;
     gpointer params [1];

     ad_class = mono_class_from_name (mono_get_corlib (),
         "System", "AppDomain");

     create_domain_method = mono_class_get_method_from_name (clazz,
         "CreateDomain", 1);

     params [0] = mono_string_new_wrapper (name);
     return (MonoAppDomain *)
         mono_runtime_invoke (create_domain_method, NULL, params, NULL);

unloadDomain (MonoAppDomain *domain)
     MonoClass *ad_class;
     MonoMethod *unload_domain_method;
     gpointer params [1];

     ad_class = mono_class_from_name (mono_get_corlib (),
         "System", "AppDomain");

     unload_domain_method = mono_class_get_method_from_name (clazz,
         "Unload", 1);

     params [0] = domain;
     mono_runtime_invoke (unload_domain_method, NULL, params, NULL);


More information about the Mono-devel-list mailing list