You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
4.0 KiB

  1. /*
  2. * mono.h -- Definitions for Mono C API functions.
  3. *
  4. * The file contains definitions for some of functions provided by Mono C API.
  5. *
  6. * Note: Since we don't use any mono headers, all pointers to mono-related structs are
  7. * replaced with void *.
  8. */
  9. #pragma once
  10. #pragma warning( disable : 4152 )
  11. #include <windows.h>
  12. // Creates a MonoString based from a C wide string
  13. #define MONO_STRING(str) mono_string_new_utf16(domain, str, wcslen(str))
  14. // Set MonoArray's index to a reference type value (i.e. string)
  15. #define SET_ARRAY_REF(arr, index, refVal) \
  16. { \
  17. void **p = (void**) mono_array_addr_with_size(arr, sizeof(void*), index); \
  18. mono_gc_wbarrier_set_arrayref(arr, p, refVal); \
  19. }
  20. // Here we define the pointers to some functions within mono.dll
  21. // Note to C learners: these are not signature definitions, but rather "variable"
  22. // definitions with the function pointer type.
  23. // Note: we use void* instead of the real intented structs defined in mono API
  24. // This way we don't need to include or define any of Mono's structs, which saves space
  25. // This, obviously, comes with a drawback of not being able to easily access the contents of the structs
  26. typedef enum {
  27. MONO_DEBUG_FORMAT_NONE,
  28. MONO_DEBUG_FORMAT_MONO,
  29. /* Deprecated, the mdb debugger is not longer supported. */
  30. MONO_DEBUG_FORMAT_DEBUGGER
  31. } MonoDebugFormat;
  32. void (*mono_jit_parse_options)(int argc, char * argv[]);
  33. void (*mono_debug_init)(MonoDebugFormat format);
  34. void(*mono_debug_domain_create)(void*);
  35. void *(*mono_jit_init_version)(const char *root_domain_name, const char *runtime_version);
  36. void *(*mono_domain_assembly_open)(void *domain, const char *name);
  37. void *(*mono_assembly_get_image)(void *assembly);
  38. void *(*mono_runtime_invoke)(void *method, void *obj, void **params, void **exc);
  39. void *(*mono_method_desc_new)(const char *name, int include_namespace);
  40. void *(*mono_method_desc_search_in_image)(void *desc, void *image);
  41. void *(*mono_method_signature)(void *method);
  42. UINT32 (*mono_signature_get_param_count)(void *sig);
  43. void *(*mono_array_new)(void *domain, void *eclass, uintptr_t n);
  44. void (*mono_gc_wbarrier_set_arrayref)(void *arr, void *slot_ptr, void *value);
  45. char *(*mono_array_addr_with_size)(void *arr, int size, uintptr_t idx);
  46. void *(*mono_get_string_class)();
  47. void *(*mono_string_new_utf16)(void *domain, const wchar_t *text, INT32 len);
  48. void* (*mono_object_to_string)(void* obj, void** exc);
  49. char* (*mono_string_to_utf8)(void* str);
  50. void (*mono_free)(void*);
  51. /* Installs a function which is called when the runtime encounters an unhandled exception.
  52. * This hook isn't expected to return.
  53. * If no hook has been installed, the runtime will print a message before aborting.
  54. */
  55. typedef void (*MonoUnhandledExceptionFunc)(void* exc, void* user_data);
  56. void (*mono_install_unhandled_exception_hook)(MonoUnhandledExceptionFunc func, void* user_data);
  57. /**
  58. * \brief Loads Mono C API function pointers so that the above definitions can be called.
  59. * \param monoLib Mono.dll module.
  60. */
  61. inline void loadMonoFunctions(HMODULE monoLib)
  62. {
  63. // Enjoy the fact that C allows such sloppy casting
  64. // In C++ you would have to cast to the precise function pointer type
  65. #define GET_MONO_PROC(name) name = (void*)GetProcAddress(monoLib, #name)
  66. // Find and assign all our functions that we are going to use
  67. GET_MONO_PROC(mono_debug_domain_create);
  68. GET_MONO_PROC(mono_domain_assembly_open);
  69. GET_MONO_PROC(mono_assembly_get_image);
  70. GET_MONO_PROC(mono_runtime_invoke);
  71. GET_MONO_PROC(mono_debug_init);
  72. GET_MONO_PROC(mono_jit_init_version);
  73. GET_MONO_PROC(mono_jit_parse_options);
  74. GET_MONO_PROC(mono_method_desc_new);
  75. GET_MONO_PROC(mono_method_desc_search_in_image);
  76. GET_MONO_PROC(mono_method_signature);
  77. GET_MONO_PROC(mono_signature_get_param_count);
  78. GET_MONO_PROC(mono_array_new);
  79. GET_MONO_PROC(mono_get_string_class);
  80. GET_MONO_PROC(mono_string_new_utf16);
  81. GET_MONO_PROC(mono_gc_wbarrier_set_arrayref);
  82. GET_MONO_PROC(mono_array_addr_with_size);
  83. GET_MONO_PROC(mono_object_to_string);
  84. GET_MONO_PROC(mono_string_to_utf8);
  85. GET_MONO_PROC(mono_free);
  86. GET_MONO_PROC(mono_install_unhandled_exception_hook);
  87. }