// Licensed to the .NET Foundation under one or more agreements.1// The .NET Foundation licenses this file to you under the MIT license.23#ifndef __HOSTFXR_H__4#define __HOSTFXR_H__56#include <stddef.h>7#include <stdint.h>89#if defined(_WIN32)10#define HOSTFXR_CALLTYPE __cdecl11#ifdef _WCHAR_T_DEFINED12typedef wchar_t char_t;13#else14typedef unsigned short char_t;15#endif16#else17#define HOSTFXR_CALLTYPE18typedef char char_t;19#endif2021enum hostfxr_delegate_type22{23hdt_com_activation,24hdt_load_in_memory_assembly,25hdt_winrt_activation,26hdt_com_register,27hdt_com_unregister,28hdt_load_assembly_and_get_function_pointer,29hdt_get_function_pointer,30};3132typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_main_fn)(const int argc, const char_t **argv);33typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_main_startupinfo_fn)(34const int argc,35const char_t **argv,36const char_t *host_path,37const char_t *dotnet_root,38const char_t *app_path);39typedef int32_t(HOSTFXR_CALLTYPE* hostfxr_main_bundle_startupinfo_fn)(40const int argc,41const char_t** argv,42const char_t* host_path,43const char_t* dotnet_root,44const char_t* app_path,45int64_t bundle_header_offset);4647typedef void(HOSTFXR_CALLTYPE *hostfxr_error_writer_fn)(const char_t *message);4849//50// Sets a callback which is to be used to write errors to.51//52// Parameters:53// error_writer54// A callback function which will be invoked every time an error is to be reported.55// Or nullptr to unregister previously registered callback and return to the default behavior.56// Return value:57// The previously registered callback (which is now unregistered), or nullptr if no previous callback58// was registered59//60// The error writer is registered per-thread, so the registration is thread-local. On each thread61// only one callback can be registered. Subsequent registrations overwrite the previous ones.62//63// By default no callback is registered in which case the errors are written to stderr.64//65// Each call to the error writer is sort of like writing a single line (the EOL character is omitted).66// Multiple calls to the error writer may occur for one failure.67//68// If the hostfxr invokes functions in hostpolicy as part of its operation, the error writer69// will be propagated to hostpolicy for the duration of the call. This means that errors from70// both hostfxr and hostpolicy will be reporter through the same error writer.71//72typedef hostfxr_error_writer_fn(HOSTFXR_CALLTYPE *hostfxr_set_error_writer_fn)(hostfxr_error_writer_fn error_writer);7374typedef void* hostfxr_handle;75struct hostfxr_initialize_parameters76{77size_t size;78const char_t *host_path;79const char_t *dotnet_root;80};8182//83// Initializes the hosting components for a dotnet command line running an application84//85// Parameters:86// argc87// Number of argv arguments88// argv89// Command-line arguments for running an application (as if through the dotnet executable).90// Only command-line arguments which are accepted by runtime installation are supported, SDK/CLI commands are not supported.91// For example 'app.dll app_argument_1 app_argument_2`.92// parameters93// Optional. Additional parameters for initialization94// host_context_handle95// On success, this will be populated with an opaque value representing the initialized host context96//97// Return value:98// Success - Hosting components were successfully initialized99// HostInvalidState - Hosting components are already initialized100//101// This function parses the specified command-line arguments to determine the application to run. It will102// then find the corresponding .runtimeconfig.json and .deps.json with which to resolve frameworks and103// dependencies and prepare everything needed to load the runtime.104//105// This function only supports arguments for running an application. It does not support SDK commands.106//107// This function does not load the runtime.108//109typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_initialize_for_dotnet_command_line_fn)(110int argc,111const char_t **argv,112const struct hostfxr_initialize_parameters *parameters,113/*out*/ hostfxr_handle *host_context_handle);114115//116// Initializes the hosting components using a .runtimeconfig.json file117//118// Parameters:119// runtime_config_path120// Path to the .runtimeconfig.json file121// parameters122// Optional. Additional parameters for initialization123// host_context_handle124// On success, this will be populated with an opaque value representing the initialized host context125//126// Return value:127// Success - Hosting components were successfully initialized128// Success_HostAlreadyInitialized - Config is compatible with already initialized hosting components129// Success_DifferentRuntimeProperties - Config has runtime properties that differ from already initialized hosting components130// CoreHostIncompatibleConfig - Config is incompatible with already initialized hosting components131//132// This function will process the .runtimeconfig.json to resolve frameworks and prepare everything needed133// to load the runtime. It will only process the .deps.json from frameworks (not any app/component that134// may be next to the .runtimeconfig.json).135//136// This function does not load the runtime.137//138// If called when the runtime has already been loaded, this function will check if the specified runtime139// config is compatible with the existing runtime.140//141// Both Success_HostAlreadyInitialized and Success_DifferentRuntimeProperties codes are considered successful142// initializations. In the case of Success_DifferentRuntimeProperties, it is left to the consumer to verify that143// the difference in properties is acceptable.144//145typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_initialize_for_runtime_config_fn)(146const char_t *runtime_config_path,147const struct hostfxr_initialize_parameters *parameters,148/*out*/ hostfxr_handle *host_context_handle);149150//151// Gets the runtime property value for an initialized host context152//153// Parameters:154// host_context_handle155// Handle to the initialized host context156// name157// Runtime property name158// value159// Out parameter. Pointer to a buffer with the property value.160//161// Return value:162// The error code result.163//164// The buffer pointed to by value is owned by the host context. The lifetime of the buffer is only165// guaranteed until any of the below occur:166// - a 'run' method is called for the host context167// - properties are changed via hostfxr_set_runtime_property_value168// - the host context is closed via 'hostfxr_close'169//170// If host_context_handle is nullptr and an active host context exists, this function will get the171// property value for the active host context.172//173typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_get_runtime_property_value_fn)(174const hostfxr_handle host_context_handle,175const char_t *name,176/*out*/ const char_t **value);177178//179// Sets the value of a runtime property for an initialized host context180//181// Parameters:182// host_context_handle183// Handle to the initialized host context184// name185// Runtime property name186// value187// Value to set188//189// Return value:190// The error code result.191//192// Setting properties is only supported for the first host context, before the runtime has been loaded.193//194// If the property already exists in the host context, it will be overwritten. If value is nullptr, the195// property will be removed.196//197typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_set_runtime_property_value_fn)(198const hostfxr_handle host_context_handle,199const char_t *name,200const char_t *value);201202//203// Gets all the runtime properties for an initialized host context204//205// Parameters:206// host_context_handle207// Handle to the initialized host context208// count209// [in] Size of the keys and values buffers210// [out] Number of properties returned (size of keys/values buffers used). If the input value is too211// small or keys/values is nullptr, this is populated with the number of available properties212// keys213// Array of pointers to buffers with runtime property keys214// values215// Array of pointers to buffers with runtime property values216//217// Return value:218// The error code result.219//220// The buffers pointed to by keys and values are owned by the host context. The lifetime of the buffers is only221// guaranteed until any of the below occur:222// - a 'run' method is called for the host context223// - properties are changed via hostfxr_set_runtime_property_value224// - the host context is closed via 'hostfxr_close'225//226// If host_context_handle is nullptr and an active host context exists, this function will get the227// properties for the active host context.228//229typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_get_runtime_properties_fn)(230const hostfxr_handle host_context_handle,231/*inout*/ size_t * count,232/*out*/ const char_t **keys,233/*out*/ const char_t **values);234235//236// Load CoreCLR and run the application for an initialized host context237//238// Parameters:239// host_context_handle240// Handle to the initialized host context241//242// Return value:243// If the app was successfully run, the exit code of the application. Otherwise, the error code result.244//245// The host_context_handle must have been initialized using hostfxr_initialize_for_dotnet_command_line.246//247// This function will not return until the managed application exits.248//249typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_run_app_fn)(const hostfxr_handle host_context_handle);250251//252// Gets a typed delegate from the currently loaded CoreCLR or from a newly created one.253//254// Parameters:255// host_context_handle256// Handle to the initialized host context257// type258// Type of runtime delegate requested259// delegate260// An out parameter that will be assigned the delegate.261//262// Return value:263// The error code result.264//265// If the host_context_handle was initialized using hostfxr_initialize_for_runtime_config,266// then all delegate types are supported.267// If the host_context_handle was initialized using hostfxr_initialize_for_dotnet_command_line,268// then only the following delegate types are currently supported:269// hdt_load_assembly_and_get_function_pointer270// hdt_get_function_pointer271//272typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_get_runtime_delegate_fn)(273const hostfxr_handle host_context_handle,274enum hostfxr_delegate_type type,275/*out*/ void **delegate);276277//278// Closes an initialized host context279//280// Parameters:281// host_context_handle282// Handle to the initialized host context283//284// Return value:285// The error code result.286//287typedef int32_t(HOSTFXR_CALLTYPE *hostfxr_close_fn)(const hostfxr_handle host_context_handle);288289struct hostfxr_dotnet_environment_sdk_info290{291size_t size;292const char_t* version;293const char_t* path;294};295296typedef void(HOSTFXR_CALLTYPE* hostfxr_get_dotnet_environment_info_result_fn)(297const struct hostfxr_dotnet_environment_info* info,298void* result_context);299300struct hostfxr_dotnet_environment_framework_info301{302size_t size;303const char_t* name;304const char_t* version;305const char_t* path;306};307308struct hostfxr_dotnet_environment_info309{310size_t size;311312const char_t* hostfxr_version;313const char_t* hostfxr_commit_hash;314315size_t sdk_count;316const struct hostfxr_dotnet_environment_sdk_info* sdks;317318size_t framework_count;319const struct hostfxr_dotnet_environment_framework_info* frameworks;320};321322#endif //__HOSTFXR_H__323324325