embedded-api 7.1 KB

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