embedded-api 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. * Embedding the Mono runtime, preliminary version
  2. This document describes how to embed the Mono runtime in your
  3. application, and how to invoke CIL methods from C, and how to
  4. invoke C code from CIL
  5. Slides for Paolo's presentation at .NET ONE on the embedding
  6. API are available here: <a
  7. href="http://primates.ximian.com/~lupus/slides/embed">Hosting the Mono
  8. Runtime</a>. You can also get his <a
  9. href="http://primates.ximian.com/~lupus/slides/embed/Mono-0.01.tar.gz">sample
  10. Mono module for Perl</a>
  11. Authors: Paolo Molaro, Miguel de Icaza.
  12. * Embedding the runtime.
  13. Embedding the runtime consists of various steps:
  14. <ul>
  15. * Compiling and linking the Mono runtime
  16. * Initializing the Mono runtime
  17. * Optionally expose C code to the C#/CIL universe.
  18. </ul>
  19. These are discussed in detail next.
  20. ** Compiling and Linking
  21. To embed the runtime, you have to link your code against the
  22. Mono runtime libraries. To do this, you want to pass the
  23. flags returned by pkg-config to your compiler:
  24. <pre>
  25. pkg-config --cflags --libs mono
  26. </pre>
  27. Like this:
  28. <pre>
  29. gcc sample.c `pkg-config --cflags --libs mono`
  30. </pre>
  31. You can separate the compilation flags from the linking flags, for
  32. instance, you can use the following macros in your makefile:
  33. <pre>
  34. CFLAGS=`pkg-config --cflags mono`
  35. LDFLAGS=`pkg-config --libs mono`
  36. </pre>
  37. ** Initializing the Mono runtime
  38. To initialize the runtime, call mono_jit_init, like this:
  39. <pre>
  40. MonoDomain *domain;
  41. domain = mono_jit_init ("domain-name");
  42. </pre>
  43. That will return a MonoDomain where your code will be
  44. executed. You can create multiple domains. Each domain is
  45. isolated from the other domains and code in one domain will
  46. not interfere with code in other domains. This is useful if
  47. you want to host different applications in your program.
  48. Then you can load an assembly containing code into the domain:
  49. <pre>
  50. MonoAssembly *assembly;
  51. assembly = mono_domain_assembly_open (domain, "file.dll");
  52. if (!assembly)
  53. error ();
  54. </pre>
  55. In the above example, the contents of `file.dll' will be
  56. loaded into the domain. This only loads the code, but it will
  57. not execute anything yet. You can replace `file.dll' with
  58. another transport file, like `file.exe'
  59. To start executing code, you must invoke a method in the
  60. assembly, or if you have provided a static Main method (an
  61. entry point), you can use the convenience function:
  62. <pre>
  63. retval = mono_jit_exec (domain, assembly, argc - 1, argv + 1);
  64. </pre>
  65. If you want to invoke a different method, look at the
  66. `Invoking Methods in the CIL universe' section later on.
  67. ** Shutting down the runtime
  68. To shutdown the Mono runtime, you have to clean up all the
  69. domains that were created, use this function:
  70. <pre>
  71. mono_jit_cleanup (domain);
  72. </pre>
  73. ** Applications that use threads.
  74. The Boehm GC system needs to catch your calls to the pthreads
  75. layer, so in each file where you use pthread.h you should
  76. include the <gc/gc.h> file.
  77. * Exposing C code to the CIL universe
  78. The Mono runtime provides two mechanisms to expose C code to
  79. the CIL universe: internal calls and native C code. Internal
  80. calls are tightly integrated with the runtime, and have the
  81. least overhead, as they use the same data types that the
  82. runtime uses.
  83. The other option is to use the Platform Invoke (P/Invoke) to
  84. call C code from the CIL universe, using the standard P/Invoke
  85. mechanisms.
  86. To register an internal call, use this call in the C code:
  87. <pre>
  88. mono_add_internal_call ("Hello::Sample", sample);
  89. </pre>
  90. Now, you need to declare this on the C# side:
  91. <pre>
  92. using System;
  93. using System.Runtime.CompilerServices;
  94. </pre>
  95. <pre>
  96. class Hello {
  97. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  98. extern static string Sample ();
  99. }
  100. </pre>
  101. Since this routine returns a string, here is the C definition:
  102. <pre>
  103. static MonoString*
  104. Sample ()
  105. {
  106. return mono_string_new (mono_domain_get (), "Hello!");
  107. }
  108. </pre>
  109. Notice that we have to return a `MonoString', and we use the
  110. `mono_string_new' API call to obtain this from a string.
  111. * Invoking Methods in the CIL universe
  112. Calling a method in the CIL universe from C requires a number of steps:
  113. <ul>
  114. * Obtaining the MonoMethod handle to the method.
  115. * The method invocation.
  116. </ul>
  117. ** Obtaining a MonoMethod
  118. To get a MonoMethod there are several ways.
  119. You can get a MonoClass (the structure representing a type)
  120. using:
  121. <pre>
  122. MonoClass *
  123. mono_class_from_name (MonoImage *image, const char* name_space, const char *name);
  124. </pre>
  125. and then loop in the returned class method array until you get
  126. the one you're looking for. There are examples of such
  127. searches as static functions in several C files in
  128. metadata/*.c: we need to expose one through the API and remove
  129. the duplicates.
  130. The other, simpler, way is to use the functions in
  131. debug-helpers.h: there are examples of their use in monograph,
  132. mint and the jit as well. You basically use a string
  133. description of the method, like:
  134. <pre>
  135. "System.Object:GetHashCode()"
  136. </pre>
  137. and create a MonoMethodDesc out of it with:
  138. <pre>
  139. MonoMethodDesc* mono_method_desc_new (const char *name, gboolean include_namespace);
  140. </pre>
  141. You can then use:
  142. <pre>
  143. MonoMethod* mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass);
  144. MonoMethod* mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image);
  145. </pre>
  146. to search for the method in a class or in an image. You would
  147. tipically do this just once at the start of the program and
  148. store the result for reuse somewhere.
  149. ** Invoking a Method
  150. There are two functions to call a managed method:
  151. <pre>
  152. MonoObject*
  153. mono_runtime_invoke (MonoMethod *method, void *obj, void **params,
  154. MonoObject **exc);
  155. and
  156. MonoObject*
  157. mono_runtime_invoke_array (MonoMethod *method, void *obj, MonoArray *params,
  158. MonoObject **exc);
  159. </pre>
  160. obj is the 'this' pointer, it should be NULL for static
  161. methods, a MonoObject* for object instances and a pointer to
  162. the value type for value types.
  163. The params array contains the arguments to the method with the
  164. same convention: MonoObject* pointers for object instances and
  165. pointers to the value type otherwise. The _invoke_array
  166. variant takes a C# object[] as the params argument (MonoArray
  167. *params): in this case the value types are boxed inside the
  168. respective reference representation.
  169. From unmanaged code you'll usually use the
  170. mono_runtime_invoke() variant.
  171. Note that this function doesn't handle virtual methods for
  172. you, it will exec the exact method you pass: we still need to
  173. expose a function to lookup the derived class implementation
  174. of a virtual method (there are examples of this in the code,
  175. though).
  176. You can pass NULL as the exc argument if you don't want to
  177. catch exceptions, otherwise, *exc will be set to the exception
  178. thrown, if any. if an exception is thrown, you can't use the
  179. MonoObject* result from the function.
  180. If the method returns a value type, it is boxed in an object
  181. reference.
  182. We have plans for providing an additional method that returns
  183. an unmanaged->managed thunk like this:
  184. <pre>
  185. void* mono_method_get_unmanaged_thunk (MonoMethod *method);
  186. </pre>
  187. You'll be able to store the returned pointer in a function
  188. pointer with the proper signature and call that directly from
  189. C:
  190. <pre>
  191. typedef gint32 (*GetHashCode) (MonoObject *obj);
  192. GetHashCode func = mono_method_get_unmanaged_thunk (System_Object_GetHashCode_method);
  193. gint32 hashvalue = func (myobject);
  194. </pre>
  195. It may not be possible to manage exceptions in that case,
  196. though. I need to think more about it.
  197. * Samples
  198. See the sample programs in mono/sample/embed for examples of
  199. embedding the Mono runtime in your application.