hostfxr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. #ifndef __HOSTFXR_H__
  4. #define __HOSTFXR_H__
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #if defined(_WIN32)
  8. #define HOSTFXR_CALLTYPE __cdecl
  9. #ifdef _WCHAR_T_DEFINED
  10. typedef wchar_t char_t;
  11. #else
  12. typedef unsigned short char_t;
  13. #endif
  14. #else
  15. #define HOSTFXR_CALLTYPE
  16. typedef char char_t;
  17. #endif
  18. enum hostfxr_delegate_type
  19. {
  20. hdt_com_activation,
  21. hdt_load_in_memory_assembly,
  22. hdt_winrt_activation,
  23. hdt_com_register,
  24. hdt_com_unregister,
  25. hdt_load_assembly_and_get_function_pointer,
  26. hdt_get_function_pointer,
  27. };
  28. typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_main_fn)(const int argc, const char_t **argv);
  29. typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_main_startupinfo_fn)(
  30. const int argc,
  31. const char_t **argv,
  32. const char_t *host_path,
  33. const char_t *dotnet_root,
  34. const char_t *app_path);
  35. typedef int32_t(HOSTFXR_CALLTYPE* hostfxr_main_bundle_startupinfo_fn)(
  36. const int argc,
  37. const char_t** argv,
  38. const char_t* host_path,
  39. const char_t* dotnet_root,
  40. const char_t* app_path,
  41. int64_t bundle_header_offset);
  42. typedef void(HOSTFXR_CALLTYPE *hostfxr_error_writer_fn)(const char_t *message);
  43. //
  44. // Sets a callback which is to be used to write errors to.
  45. //
  46. // Parameters:
  47. // error_writer
  48. // A callback function which will be invoked every time an error is to be reported.
  49. // Or nullptr to unregister previously registered callback and return to the default behavior.
  50. // Return value:
  51. // The previously registered callback (which is now unregistered), or nullptr if no previous callback
  52. // was registered
  53. //
  54. // The error writer is registered per-thread, so the registration is thread-local. On each thread
  55. // only one callback can be registered. Subsequent registrations overwrite the previous ones.
  56. //
  57. // By default no callback is registered in which case the errors are written to stderr.
  58. //
  59. // Each call to the error writer is sort of like writing a single line (the EOL character is omitted).
  60. // Multiple calls to the error writer may occur for one failure.
  61. //
  62. // If the hostfxr invokes functions in hostpolicy as part of its operation, the error writer
  63. // will be propagated to hostpolicy for the duration of the call. This means that errors from
  64. // both hostfxr and hostpolicy will be reporter through the same error writer.
  65. //
  66. typedef hostfxr_error_writer_fn(HOSTFXR_CALLTYPE *hostfxr_set_error_writer_fn)(hostfxr_error_writer_fn error_writer);
  67. typedef void* hostfxr_handle;
  68. struct hostfxr_initialize_parameters
  69. {
  70. size_t size;
  71. const char_t *host_path;
  72. const char_t *dotnet_root;
  73. };
  74. //
  75. // Initializes the hosting components for a dotnet command line running an application
  76. //
  77. // Parameters:
  78. // argc
  79. // Number of argv arguments
  80. // argv
  81. // Command-line arguments for running an application (as if through the dotnet executable).
  82. // Only command-line arguments which are accepted by runtime installation are supported, SDK/CLI commands are not supported.
  83. // For example 'app.dll app_argument_1 app_argument_2`.
  84. // parameters
  85. // Optional. Additional parameters for initialization
  86. // host_context_handle
  87. // On success, this will be populated with an opaque value representing the initialized host context
  88. //
  89. // Return value:
  90. // Success - Hosting components were successfully initialized
  91. // HostInvalidState - Hosting components are already initialized
  92. //
  93. // This function parses the specified command-line arguments to determine the application to run. It will
  94. // then find the corresponding .runtimeconfig.json and .deps.json with which to resolve frameworks and
  95. // dependencies and prepare everything needed to load the runtime.
  96. //
  97. // This function only supports arguments for running an application. It does not support SDK commands.
  98. //
  99. // This function does not load the runtime.
  100. //
  101. typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_initialize_for_dotnet_command_line_fn)(
  102. int argc,
  103. const char_t **argv,
  104. const struct hostfxr_initialize_parameters *parameters,
  105. /*out*/ hostfxr_handle *host_context_handle);
  106. //
  107. // Initializes the hosting components using a .runtimeconfig.json file
  108. //
  109. // Parameters:
  110. // runtime_config_path
  111. // Path to the .runtimeconfig.json file
  112. // parameters
  113. // Optional. Additional parameters for initialization
  114. // host_context_handle
  115. // On success, this will be populated with an opaque value representing the initialized host context
  116. //
  117. // Return value:
  118. // Success - Hosting components were successfully initialized
  119. // Success_HostAlreadyInitialized - Config is compatible with already initialized hosting components
  120. // Success_DifferentRuntimeProperties - Config has runtime properties that differ from already initialized hosting components
  121. // CoreHostIncompatibleConfig - Config is incompatible with already initialized hosting components
  122. //
  123. // This function will process the .runtimeconfig.json to resolve frameworks and prepare everything needed
  124. // to load the runtime. It will only process the .deps.json from frameworks (not any app/component that
  125. // may be next to the .runtimeconfig.json).
  126. //
  127. // This function does not load the runtime.
  128. //
  129. // If called when the runtime has already been loaded, this function will check if the specified runtime
  130. // config is compatible with the existing runtime.
  131. //
  132. // Both Success_HostAlreadyInitialized and Success_DifferentRuntimeProperties codes are considered successful
  133. // initializations. In the case of Success_DifferentRuntimeProperties, it is left to the consumer to verify that
  134. // the difference in properties is acceptable.
  135. //
  136. typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_initialize_for_runtime_config_fn)(
  137. const char_t *runtime_config_path,
  138. const struct hostfxr_initialize_parameters *parameters,
  139. /*out*/ hostfxr_handle *host_context_handle);
  140. //
  141. // Gets the runtime property value for an initialized host context
  142. //
  143. // Parameters:
  144. // host_context_handle
  145. // Handle to the initialized host context
  146. // name
  147. // Runtime property name
  148. // value
  149. // Out parameter. Pointer to a buffer with the property value.
  150. //
  151. // Return value:
  152. // The error code result.
  153. //
  154. // The buffer pointed to by value is owned by the host context. The lifetime of the buffer is only
  155. // guaranteed until any of the below occur:
  156. // - a 'run' method is called for the host context
  157. // - properties are changed via hostfxr_set_runtime_property_value
  158. // - the host context is closed via 'hostfxr_close'
  159. //
  160. // If host_context_handle is nullptr and an active host context exists, this function will get the
  161. // property value for the active host context.
  162. //
  163. typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_get_runtime_property_value_fn)(
  164. const hostfxr_handle host_context_handle,
  165. const char_t *name,
  166. /*out*/ const char_t **value);
  167. //
  168. // Sets the value of a runtime property for an initialized host context
  169. //
  170. // Parameters:
  171. // host_context_handle
  172. // Handle to the initialized host context
  173. // name
  174. // Runtime property name
  175. // value
  176. // Value to set
  177. //
  178. // Return value:
  179. // The error code result.
  180. //
  181. // Setting properties is only supported for the first host context, before the runtime has been loaded.
  182. //
  183. // If the property already exists in the host context, it will be overwritten. If value is nullptr, the
  184. // property will be removed.
  185. //
  186. typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_set_runtime_property_value_fn)(
  187. const hostfxr_handle host_context_handle,
  188. const char_t *name,
  189. const char_t *value);
  190. //
  191. // Gets all the runtime properties for an initialized host context
  192. //
  193. // Parameters:
  194. // host_context_handle
  195. // Handle to the initialized host context
  196. // count
  197. // [in] Size of the keys and values buffers
  198. // [out] Number of properties returned (size of keys/values buffers used). If the input value is too
  199. // small or keys/values is nullptr, this is populated with the number of available properties
  200. // keys
  201. // Array of pointers to buffers with runtime property keys
  202. // values
  203. // Array of pointers to buffers with runtime property values
  204. //
  205. // Return value:
  206. // The error code result.
  207. //
  208. // The buffers pointed to by keys and values are owned by the host context. The lifetime of the buffers is only
  209. // guaranteed until any of the below occur:
  210. // - a 'run' method is called for the host context
  211. // - properties are changed via hostfxr_set_runtime_property_value
  212. // - the host context is closed via 'hostfxr_close'
  213. //
  214. // If host_context_handle is nullptr and an active host context exists, this function will get the
  215. // properties for the active host context.
  216. //
  217. typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_get_runtime_properties_fn)(
  218. const hostfxr_handle host_context_handle,
  219. /*inout*/ size_t * count,
  220. /*out*/ const char_t **keys,
  221. /*out*/ const char_t **values);
  222. //
  223. // Load CoreCLR and run the application for an initialized host context
  224. //
  225. // Parameters:
  226. // host_context_handle
  227. // Handle to the initialized host context
  228. //
  229. // Return value:
  230. // If the app was successfully run, the exit code of the application. Otherwise, the error code result.
  231. //
  232. // The host_context_handle must have been initialized using hostfxr_initialize_for_dotnet_command_line.
  233. //
  234. // This function will not return until the managed application exits.
  235. //
  236. typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_run_app_fn)(const hostfxr_handle host_context_handle);
  237. //
  238. // Gets a typed delegate from the currently loaded CoreCLR or from a newly created one.
  239. //
  240. // Parameters:
  241. // host_context_handle
  242. // Handle to the initialized host context
  243. // type
  244. // Type of runtime delegate requested
  245. // delegate
  246. // An out parameter that will be assigned the delegate.
  247. //
  248. // Return value:
  249. // The error code result.
  250. //
  251. // If the host_context_handle was initialized using hostfxr_initialize_for_runtime_config,
  252. // then all delegate types are supported.
  253. // If the host_context_handle was initialized using hostfxr_initialize_for_dotnet_command_line,
  254. // then only the following delegate types are currently supported:
  255. // hdt_load_assembly_and_get_function_pointer
  256. // hdt_get_function_pointer
  257. //
  258. typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_get_runtime_delegate_fn)(
  259. const hostfxr_handle host_context_handle,
  260. enum hostfxr_delegate_type type,
  261. /*out*/ void **delegate);
  262. //
  263. // Closes an initialized host context
  264. //
  265. // Parameters:
  266. // host_context_handle
  267. // Handle to the initialized host context
  268. //
  269. // Return value:
  270. // The error code result.
  271. //
  272. typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_close_fn)(const hostfxr_handle host_context_handle);
  273. struct hostfxr_dotnet_environment_sdk_info
  274. {
  275. size_t size;
  276. const char_t* version;
  277. const char_t* path;
  278. };
  279. typedef void(HOSTFXR_CALLTYPE* hostfxr_get_dotnet_environment_info_result_fn)(
  280. const struct hostfxr_dotnet_environment_info* info,
  281. void* result_context);
  282. struct hostfxr_dotnet_environment_framework_info
  283. {
  284. size_t size;
  285. const char_t* name;
  286. const char_t* version;
  287. const char_t* path;
  288. };
  289. struct hostfxr_dotnet_environment_info
  290. {
  291. size_t size;
  292. const char_t* hostfxr_version;
  293. const char_t* hostfxr_commit_hash;
  294. size_t sdk_count;
  295. const struct hostfxr_dotnet_environment_sdk_info* sdks;
  296. size_t framework_count;
  297. const struct hostfxr_dotnet_environment_framework_info* frameworks;
  298. };
  299. #endif //__HOSTFXR_H__