teste.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <mono/jit/jit.h>
  2. #include <mono/metadata/environment.h>
  3. #include <stdlib.h>
  4. /*
  5. * Very simple mono embedding example.
  6. * Compile with:
  7. * gcc -o teste teste.c `pkg-config --cflags --libs mono` -lm
  8. * mcs test.cs
  9. * Run with:
  10. * ./teste test.exe
  11. */
  12. static MonoString*
  13. gimme () {
  14. return mono_string_new (mono_domain_get (), "All your monos are belong to us!");
  15. }
  16. static void main_function (MonoDomain *domain, const char *file, int argc, char** argv)
  17. {
  18. MonoAssembly *assembly;
  19. assembly = mono_domain_assembly_open (domain, file);
  20. if (!assembly)
  21. exit (2);
  22. /*
  23. * mono_jit_exec() will run the Main() method in the assembly.
  24. * The return value needs to be looked up from
  25. * System.Environment.ExitCode.
  26. */
  27. mono_jit_exec (domain, assembly, argc, argv);
  28. }
  29. int
  30. main(int argc, char* argv[]) {
  31. MonoDomain *domain;
  32. const char *file;
  33. int retval;
  34. if (argc < 2){
  35. fprintf (stderr, "Please provide an assembly to load");
  36. return 1;
  37. }
  38. file = argv [1];
  39. /*
  40. * mono_jit_init() creates a domain: each assembly is
  41. * loaded and run in a MonoDomain.
  42. */
  43. domain = mono_jit_init (file);
  44. /*
  45. * We add our special internal call, so that C# code
  46. * can call us back.
  47. */
  48. mono_add_internal_call ("MonoEmbed::gimme", gimme);
  49. main_function (domain, file, argc - 1, argv + 1);
  50. retval = mono_environment_exitcode_get ();
  51. mono_jit_cleanup (domain);
  52. return retval;
  53. }