teste.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\n");
  36. return 1;
  37. }
  38. file = argv [1];
  39. /*
  40. * Load the default Mono configuration file, this is needed
  41. * if you are planning on using the dllmaps defined on the
  42. * system configuration
  43. */
  44. mono_config_parse (NULL);
  45. /*
  46. * mono_jit_init() creates a domain: each assembly is
  47. * loaded and run in a MonoDomain.
  48. */
  49. domain = mono_jit_init (file);
  50. /*
  51. * We add our special internal call, so that C# code
  52. * can call us back.
  53. */
  54. mono_add_internal_call ("MonoEmbed::gimme", gimme);
  55. main_function (domain, file, argc - 1, argv + 1);
  56. retval = mono_environment_exitcode_get ();
  57. mono_jit_cleanup (domain);
  58. return retval;
  59. }