Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / docs / embedded-api
index 1ed0f5381f101d82198afa674a0a5446d5035dfe..f44f4ed15cad9abbf2274dc5d336b5e1e7f51e5d 100644 (file)
@@ -1,11 +1,22 @@
        
           Embedding the Mono runtime, preliminary version
+                Miguel de Icaza (miguel@ximian.com),
+                  Paolo Molaro (lupus@ximian.com)
 
-                   Miguel de Icaza, Paolo Molaro.
 
        This document describes how to embed the Mono runtime in your
        application, and how to invoke CIL methods from C, and how to
-       invoke C code from CIL.
+       invoke C code from CIL. Both the JIT and interpreter can be
+       embedded in very similar ways so most of what is described
+       here can be used in either case.
+
+* IMPORTANT
+
+       This document is now outdated, see:
+
+               http://www.mono-project.com/Embedding_Mono
+
+        For an up-to-date version of this document
 
 * Embedding the runtime.
 
 
                pkg-config --cflags --libs mono
 
+       is used to get the flags for the JIT runtime and
+
+               pkg-config --cflags --libs mint
+
+       for the interpreted runtime.
+
        Like this:
 
                gcc sample.c `pkg-config --cflags --libs mono`
 
 ** Initializing the Mono runtime
 
-       To initialize the runtime, call mono_jit_init, like this:
+       To initialize the JIT runtime, call mono_jit_init, like this:
+
+               #include <mono/mini/jit.h>
 
                MonoDomain *domain;
 
                domain = mono_jit_init ("domain-name");
 
+       For the interpreted runtime use mono_interp_init instead:
+
+               #include <mono/interpreter/embed.h>
+
+               MonoDomain *domain;
+
+               domain = mono_interp_init ("domain-name");
+
        That will return a MonoDomain where your code will be
        executed.  You can create multiple domains.  Each domain is
        isolated from the other domains and code in one domain will
        not interfere with code in other domains.  This is useful if
        you want to host different applications in your program.  
 
-       Then you can load an assembly containing code into the domain:
+       Now, it is necessary to transfer control to Mono, and setup
+       the threading infrastructure, you do this like this:
+
+               void *user_data = NULL;
+
+               mono_runtime_exec_managed_code (domain, main_thread_handler, user_data);
+
+       Where your main_thread_handler can load your assembly and execute it:
+
+       static void main_thread_handler (gpointer user_data)
 
                MonoAssembly *assembly;
 
 
                retval = mono_jit_exec (domain, assembly, argc - 1, argv + 1);
 
+       or when using the interpreter use:
+
+               retval = mono_interp_exec (domain, assembly, argc - 1, argv + 1);
+
        If you want to invoke a different method, look at the
        `Invoking Methods in the CIL universe' section later on.
 
 
                mono_jit_cleanup (domain);
 
+       Or in the case of the interpreted runtime use:
+
+               mono_interp_cleanup (domain);
+
 ** Applications that use threads.
 
        The Boehm GC system needs to catch your calls to the pthreads
        layer, so in each file where you use pthread.h you should
        include the <gc/gc.h> file.  
 
+       If you can not do this for any reasons, just remember that you
+       can not store pointers to Mono Objects on the stack, you can
+       store them safely in the heap, or in global variables though
+
 * Exposing C code to the CIL universe
 
        The Mono runtime provides two mechanisms to expose C code to
        methods, a MonoObject* for object instances and a pointer to
        the value type for value types.
 
+       These functions can be used in both the JIT and the interpreted
+       environments.
+
        The params array contains the arguments to the method with the
        same convention: MonoObject* pointers for object instances and
        pointers to the value type otherwise. The _invoke_array
        It may not be possible to manage exceptions in that case,
        though. I need to think more about it.
 
+** Threading issues
+
+       If your application creates threads on its own, and you want them to 
+       be able to call code into the CIL universe with Mono, you have to
+       register the thread with Mono before issuing the call.
+
+       To do so, call the mono_thread_attach() function before you execute
+       any managed code from the thread
+
 * Samples
 
-       See the sample programs inmono/sample/embed for examples of
+       See the sample programs in mono/sample/embed for examples of
        embedding the Mono runtime in your application.