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.

112 lines
4.3 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. BOOL (*mono_debug_enabled)(void);
  35. void (*mono_debug_domain_create)(void*);
  36. void *(*mono_jit_init_version)(const char *root_domain_name, const char *runtime_version);
  37. void *(*mono_domain_assembly_open)(void *domain, const char *name);
  38. void *(*mono_assembly_get_image)(void *assembly);
  39. void *(*mono_runtime_invoke)(void *method, void *obj, void **params, void **exc);
  40. void *(*mono_method_desc_new)(const char *name, int include_namespace);
  41. void *(*mono_method_desc_search_in_image)(void *desc, void *image);
  42. void *(*mono_method_signature)(void *method);
  43. UINT32 (*mono_signature_get_param_count)(void *sig);
  44. void *(*mono_array_new)(void *domain, void *eclass, uintptr_t n);
  45. void (*mono_gc_wbarrier_set_arrayref)(void *arr, void *slot_ptr, void *value);
  46. char *(*mono_array_addr_with_size)(void *arr, int size, uintptr_t idx);
  47. void *(*mono_get_string_class)();
  48. void *(*mono_string_new_utf16)(void *domain, const wchar_t *text, INT32 len);
  49. void* (*mono_object_to_string)(void* obj, void** exc);
  50. void (*mono_dllmap_insert)(void* assemblyImage, const char* dll, const char* func, const char* tdll, const char* tfunc);
  51. char* (*mono_string_to_utf8)(void* str);
  52. wchar_t* (*mono_string_to_utf16)(void* str);
  53. void (*mono_free)(void*);
  54. /* Installs a function which is called when the runtime encounters an unhandled exception.
  55. * This hook isn't expected to return.
  56. * If no hook has been installed, the runtime will print a message before aborting.
  57. */
  58. typedef void (*MonoUnhandledExceptionFunc)(void* exc, void* user_data);
  59. void (*mono_install_unhandled_exception_hook)(MonoUnhandledExceptionFunc func, void* user_data);
  60. /**
  61. * \brief Loads Mono C API function pointers so that the above definitions can be called.
  62. * \param monoLib Mono.dll module.
  63. */
  64. inline void loadMonoFunctions(HMODULE monoLib)
  65. {
  66. // Enjoy the fact that C allows such sloppy casting
  67. // In C++ you would have to cast to the precise function pointer type
  68. #define GET_MONO_PROC(name) name = (void*)GetProcAddress(monoLib, #name)
  69. // Find and assign all our functions that we are going to use
  70. GET_MONO_PROC(mono_debug_domain_create);
  71. GET_MONO_PROC(mono_domain_assembly_open);
  72. GET_MONO_PROC(mono_assembly_get_image);
  73. GET_MONO_PROC(mono_runtime_invoke);
  74. GET_MONO_PROC(mono_debug_init);
  75. GET_MONO_PROC(mono_debug_enabled);
  76. GET_MONO_PROC(mono_jit_init_version);
  77. GET_MONO_PROC(mono_jit_parse_options);
  78. GET_MONO_PROC(mono_method_desc_new);
  79. GET_MONO_PROC(mono_method_desc_search_in_image);
  80. GET_MONO_PROC(mono_method_signature);
  81. GET_MONO_PROC(mono_signature_get_param_count);
  82. GET_MONO_PROC(mono_array_new);
  83. GET_MONO_PROC(mono_get_string_class);
  84. GET_MONO_PROC(mono_string_new_utf16);
  85. GET_MONO_PROC(mono_gc_wbarrier_set_arrayref);
  86. GET_MONO_PROC(mono_array_addr_with_size);
  87. GET_MONO_PROC(mono_object_to_string);
  88. GET_MONO_PROC(mono_string_to_utf8);
  89. GET_MONO_PROC(mono_string_to_utf16);
  90. GET_MONO_PROC(mono_free);
  91. GET_MONO_PROC(mono_dllmap_insert);
  92. GET_MONO_PROC(mono_install_unhandled_exception_hook);
  93. }