1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use crate::{
bindings::{
char_t,
hostfxr::{hostfxr_handle, hostfxr_initialize_parameters, HostfxrLib},
},
nethost,
pdcstring::PdCStr,
Error,
};
use dlopen::wrapper::Container;
use std::{ffi::OsStr, mem::MaybeUninit, ptr};
use super::{
HostExitCode, HostfxrContext, HostfxrHandle, InitializedForCommandLine,
InitializedForRuntimeConfig,
};
/// A struct representing a loaded hostfxr library.
pub struct Hostfxr {
pub(crate) lib: Container<HostfxrLib>,
}
impl !Sync for Hostfxr {}
impl !Send for Hostfxr {}
impl Hostfxr {
/// Loads the hostfxr library from the given path.
pub fn load_from_path(path: impl AsRef<OsStr>) -> Result<Self, Error> {
Ok(Self {
lib: unsafe { Container::load(path)? },
})
}
/// Locates the hostfxr library using [`nethost`] and loads it.
pub fn load_with_nethost() -> Result<Self, Error> {
nethost::load_hostfxr()
}
/// Initializes the hosting components for a dotnet command line running an application
///
/// Like all the other `initialize` functions, this function will
/// * Process the `.runtimeconfig.json`
/// * Resolve framework references and find actual frameworks
/// * Find the root framework (`Microsoft.NETCore.App`) and load the hostpolicy from it
/// * The hostpolicy will then process all relevant `.deps.json` files and produce the list of assemblies, native search paths and other artifacts needed to initialize the runtime.
///
/// The functions will **NOT** load the CoreCLR runtime. They just prepare everything to the point where it can be loaded.
///
/// # Arguments
/// * `app_path`:
/// The path to the target application.
///
/// # Remarks
/// This function parses the specified command-line arguments to determine the application to run. It will
/// then find the corresponding `.runtimeconfig.json` and `.deps.json` with which to resolve frameworks and
/// dependencies and prepare everything needed to load the runtime.
pub fn initialize_for_dotnet_command_line(
&self,
app_path: impl AsRef<PdCStr>,
) -> Result<HostfxrContext<InitializedForCommandLine>, Error> {
self.initialize_for_dotnet_command_line_with_args(&[app_path.as_ref()])
}
/// Initializes the hosting components for a dotnet command line running an application
///
/// Like all the other `initialize` functions, this function will
/// * Process the `.runtimeconfig.json`
/// * Resolve framework references and find actual frameworks
/// * Find the root framework (`Microsoft.NETCore.App`) and load the hostpolicy from it
/// * The hostpolicy will then process all relevant `.deps.json` files and produce the list of assemblies, native search paths and other artifacts needed to initialize the runtime.
///
/// The functions will **NOT** load the CoreCLR runtime. They just prepare everything to the point where it can be loaded.
///
/// # Arguments
/// * `app_path`:
/// The path to the target application.
/// * `host_path`:
/// Path to the native host (typically the `.exe`).
/// This value is not used for anything by the hosting components.
/// It's just passed to the CoreCLR as the path to the executable.
/// It can point to a file which is not executable itself, if such file doesn't exist (for example in COM activation scenarios this points to the `comhost.dll`).
/// This is used by PAL to initialize internal command line structures, process name and so on.
///
/// # Remarks
/// This function parses the specified command-line arguments to determine the application to run. It will
/// then find the corresponding `.runtimeconfig.json` and `.deps.json` with which to resolve frameworks and
/// dependencies and prepare everything needed to load the runtime.
pub fn initialize_for_dotnet_command_line_with_host_path(
&self,
app_path: impl AsRef<PdCStr>,
host_path: impl AsRef<PdCStr>,
) -> Result<HostfxrContext<InitializedForCommandLine>, Error> {
self.initialize_for_dotnet_command_line_with_args_and_host_path(
&[app_path.as_ref()],
host_path,
)
}
/// Initializes the hosting components for a dotnet command line running an application
///
/// Like all the other `initialize` functions, this function will
/// * Process the `.runtimeconfig.json`
/// * Resolve framework references and find actual frameworks
/// * Find the root framework (`Microsoft.NETCore.App`) and load the hostpolicy from it
/// * The hostpolicy will then process all relevant `.deps.json` files and produce the list of assemblies, native search paths and other artifacts needed to initialize the runtime.
///
/// The functions will **NOT** load the CoreCLR runtime. They just prepare everything to the point where it can be loaded.
///
/// # Arguments
/// * `app_path`:
/// The path to the target application.
/// * `dotnet_root`:
/// Path to the root of the .NET Core installation in use.
/// This typically points to the install location from which the hostfxr has been loaded.
/// For example on Windows this would typically point to `C:\Program Files\dotnet`.
/// The path is used to search for shared frameworks and potentially SDKs.
///
/// # Remarks
/// This function parses the specified command-line arguments to determine the application to run. It will
/// then find the corresponding `.runtimeconfig.json` and `.deps.json` with which to resolve frameworks and
/// dependencies and prepare everything needed to load the runtime.
pub fn initialize_for_dotnet_command_line_with_dotnet_root(
&self,
app_path: impl AsRef<PdCStr>,
dotnet_root: impl AsRef<PdCStr>,
) -> Result<HostfxrContext<InitializedForCommandLine>, Error> {
self.initialize_for_dotnet_command_line_with_args_and_dotnet_root(
&[app_path.as_ref()],
dotnet_root,
)
}
/// Initializes the hosting components for a dotnet command line running an application
///
/// Like all the other `initialize` functions, this function will
/// * Process the `.runtimeconfig.json`
/// * Resolve framework references and find actual frameworks
/// * Find the root framework (`Microsoft.NETCore.App`) and load the hostpolicy from it
/// * The hostpolicy will then process all relevant `.deps.json` files and produce the list of assemblies, native search paths and other artifacts needed to initialize the runtime.
///
/// The functions will **NOT** load the CoreCLR runtime. They just prepare everything to the point where it can be loaded.
///
/// # Arguments
/// * `args`:
/// The command line for running a managed application.
/// These represent the arguments which would have been passed to the muxer if the app was being run from the command line.
/// Note that the first argument has to be the path of the target app.
///
/// # Remarks
/// This function parses the specified command-line arguments to determine the application to run. It will
/// then find the corresponding `.runtimeconfig.json` and `.deps.json` with which to resolve frameworks and
/// dependencies and prepare everything needed to load the runtime.
pub fn initialize_for_dotnet_command_line_with_args(
&self,
args: &[&PdCStr],
) -> Result<HostfxrContext<InitializedForCommandLine>, Error> {
unsafe {
self.initialize_for_dotnet_command_line_with_parameters(args.as_ref(), ptr::null())
}
}
/// Initializes the hosting components for a dotnet command line running an application
///
/// Like all the other `initialize` functions, this function will
/// * Process the `.runtimeconfig.json`
/// * Resolve framework references and find actual frameworks
/// * Find the root framework (`Microsoft.NETCore.App`) and load the hostpolicy from it
/// * The hostpolicy will then process all relevant `.deps.json` files and produce the list of assemblies, native search paths and other artifacts needed to initialize the runtime.
///
/// The functions will **NOT** load the CoreCLR runtime. They just prepare everything to the point where it can be loaded.
///
/// # Arguments
/// * `args`:
/// The command line for running a managed application.
/// These represent the arguments which would have been passed to the muxer if the app was being run from the command line.
/// Note that the first argument has to be the path of the target app.
/// * `host_path`:
/// Path to the native host (typically the `.exe`).
/// This value is not used for anything by the hosting components.
/// It's just passed to the CoreCLR as the path to the executable.
/// It can point to a file which is not executable itself, if such file doesn't exist (for example in COM activation scenarios this points to the `comhost.dll`).
/// This is used by PAL to initialize internal command line structures, process name and so on.
///
/// # Remarks
/// This function parses the specified command-line arguments to determine the application to run. It will
/// then find the corresponding `.runtimeconfig.json` and `.deps.json` with which to resolve frameworks and
/// dependencies and prepare everything needed to load the runtime.
pub fn initialize_for_dotnet_command_line_with_args_and_host_path(
&self,
args: &[&PdCStr],
host_path: impl AsRef<PdCStr>,
) -> Result<HostfxrContext<InitializedForCommandLine>, Error> {
let parameters = hostfxr_initialize_parameters::with_host_path(host_path.as_ref().as_ptr());
unsafe { self.initialize_for_dotnet_command_line_with_parameters(args, ¶meters) }
}
/// Initializes the hosting components for a dotnet command line running an application
///
/// Like all the other `initialize` functions, this function will
/// * Process the `.runtimeconfig.json`
/// * Resolve framework references and find actual frameworks
/// * Find the root framework (`Microsoft.NETCore.App`) and load the hostpolicy from it
/// * The hostpolicy will then process all relevant `.deps.json` files and produce the list of assemblies, native search paths and other artifacts needed to initialize the runtime.
///
/// The functions will **NOT** load the CoreCLR runtime. They just prepare everything to the point where it can be loaded.
///
/// # Arguments
/// * `args`:
/// The command line for running a managed application.
/// These represent the arguments which would have been passed to the muxer if the app was being run from the command line.
/// Note that the first argument has to be the path of the target app.
/// * `dotnet_root`:
/// Path to the root of the .NET Core installation in use.
/// This typically points to the install location from which the hostfxr has been loaded.
/// For example on Windows this would typically point to `C:\Program Files\dotnet`.
/// The path is used to search for shared frameworks and potentially SDKs.
///
/// # Remarks
/// This function parses the specified command-line arguments to determine the application to run. It will
/// then find the corresponding `.runtimeconfig.json` and `.deps.json` with which to resolve frameworks and
/// dependencies and prepare everything needed to load the runtime.
pub fn initialize_for_dotnet_command_line_with_args_and_dotnet_root(
&self,
args: &[&PdCStr],
dotnet_root: impl AsRef<PdCStr>,
) -> Result<HostfxrContext<InitializedForCommandLine>, Error> {
let parameters =
hostfxr_initialize_parameters::with_dotnet_root(dotnet_root.as_ref().as_ptr());
unsafe { self.initialize_for_dotnet_command_line_with_parameters(args, ¶meters) }
}
unsafe fn initialize_for_dotnet_command_line_with_parameters(
&self,
args: &[&PdCStr],
parameters: *const hostfxr_initialize_parameters,
) -> Result<HostfxrContext<InitializedForCommandLine>, Error> {
let mut hostfxr_handle = MaybeUninit::<hostfxr_handle>::uninit();
let result = unsafe {
self.lib.hostfxr_initialize_for_dotnet_command_line(
args.len() as i32,
args.as_ptr() as *const *const char_t,
parameters,
hostfxr_handle.as_mut_ptr(),
)
};
HostExitCode::from(result).to_result()?;
Ok(HostfxrContext::new(
unsafe { HostfxrHandle::new_unchecked(hostfxr_handle.assume_init()) },
self,
))
}
/// This function loads the specified `.runtimeconfig.json`, resolve all frameworks, resolve all the assets from those frameworks and
/// then prepare runtime initialization where the TPA contains only frameworks.
/// Note that this case does **NOT** consume any `.deps.json` from the app/component (only processes the framework's `.deps.json`).
///
/// Like all the other `initialize` functions, this function will
/// * Process the `.runtimeconfig.json`
/// * Resolve framework references and find actual frameworks
/// * Find the root framework (`Microsoft.NETCore.App`) and load the hostpolicy from it
/// * The hostpolicy will then process all relevant `.deps.json` files and produce the list of assemblies, native search paths and other artifacts needed to initialize the runtime.
///
/// The functions will **NOT** load the CoreCLR runtime. They just prepare everything to the point where it can be loaded.
///
/// # Arguments
/// * `runtime_config_path`:
/// Path to the `.runtimeconfig.json` file to process.
/// Unlike with [`initialize_for_dotnet_command_line`], any `.deps.json` from the app/component will not be processed by the hosting layers.
///
/// [`initialize_for_dotnet_command_line`]: Hostfxr::initialize_for_dotnet_command_line
pub fn initialize_for_runtime_config(
&self,
runtime_config_path: impl AsRef<PdCStr>,
) -> Result<HostfxrContext<InitializedForRuntimeConfig>, Error> {
unsafe {
self.initialize_for_runtime_config_with_parameters(runtime_config_path, ptr::null())
}
}
/// This function loads the specified `.runtimeconfig.json`, resolve all frameworks, resolve all the assets from those frameworks and
/// then prepare runtime initialization where the TPA contains only frameworks.
/// Note that this case does **NOT** consume any `.deps.json` from the app/component (only processes the framework's `.deps.json`).
///
/// Like all the other `initialize` functions, this function will
/// * Process the `.runtimeconfig.json`
/// * Resolve framework references and find actual frameworks
/// * Find the root framework (`Microsoft.NETCore.App`) and load the hostpolicy from it
/// * The hostpolicy will then process all relevant `.deps.json` files and produce the list of assemblies, native search paths and other artifacts needed to initialize the runtime.
///
/// The functions will **NOT** load the CoreCLR runtime. They just prepare everything to the point where it can be loaded.
///
/// # Arguments
/// * `runtime_config_path`:
/// Path to the `.runtimeconfig.json` file to process.
/// Unlike with [`initialize_for_dotnet_command_line`], any `.deps.json` from the app/component will not be processed by the hosting layers.
/// * `host_path`:
/// Path to the native host (typically the `.exe`).
/// This value is not used for anything by the hosting components.
/// It's just passed to the CoreCLR as the path to the executable.
/// It can point to a file which is not executable itself, if such file doesn't exist (for example in COM activation scenarios this points to the `comhost.dll`).
/// This is used by PAL to initialize internal command line structures, process name and so on.
///
/// [`initialize_for_dotnet_command_line`]: Hostfxr::initialize_for_dotnet_command_line
pub fn initialize_for_runtime_config_with_host_path(
&self,
runtime_config_path: impl AsRef<PdCStr>,
host_path: impl AsRef<PdCStr>,
) -> Result<HostfxrContext<InitializedForRuntimeConfig>, Error> {
let parameters = hostfxr_initialize_parameters::with_host_path(host_path.as_ref().as_ptr());
unsafe {
self.initialize_for_runtime_config_with_parameters(runtime_config_path, ¶meters)
}
}
/// This function loads the specified `.runtimeconfig.json`, resolve all frameworks, resolve all the assets from those frameworks and
/// then prepare runtime initialization where the TPA contains only frameworks.
/// Note that this case does **NOT** consume any `.deps.json` from the app/component (only processes the framework's `.deps.json`).
///
/// Like all the other `initialize` functions, this function will
/// * Process the `.runtimeconfig.json`
/// * Resolve framework references and find actual frameworks
/// * Find the root framework (`Microsoft.NETCore.App`) and load the hostpolicy from it
/// * The hostpolicy will then process all relevant `.deps.json` files and produce the list of assemblies, native search paths and other artifacts needed to initialize the runtime.
///
/// The functions will **NOT** load the CoreCLR runtime. They just prepare everything to the point where it can be loaded.
///
/// # Arguments
/// * `runtime_config_path`:
/// Path to the `.runtimeconfig.json` file to process.
/// Unlike with [`initialize_for_dotnet_command_line`], any `.deps.json` from the app/component will not be processed by the hosting layers.
/// * `dotnet_root`:
/// Path to the root of the .NET Core installation in use.
/// This typically points to the install location from which the hostfxr has been loaded.
/// For example on Windows this would typically point to `C:\Program Files\dotnet`.
/// The path is used to search for shared frameworks and potentially SDKs.
///
/// [`initialize_for_dotnet_command_line`]: Hostfxr::initialize_for_dotnet_command_line
pub fn initialize_for_runtime_config_with_dotnet_root(
&self,
runtime_config_path: impl AsRef<PdCStr>,
dotnet_root: impl AsRef<PdCStr>,
) -> Result<HostfxrContext<InitializedForRuntimeConfig>, Error> {
let parameters =
hostfxr_initialize_parameters::with_dotnet_root(dotnet_root.as_ref().as_ptr());
unsafe {
self.initialize_for_runtime_config_with_parameters(runtime_config_path, ¶meters)
}
}
unsafe fn initialize_for_runtime_config_with_parameters(
&self,
runtime_config_path: impl AsRef<PdCStr>,
parameters: *const hostfxr_initialize_parameters,
) -> Result<HostfxrContext<InitializedForRuntimeConfig>, Error> {
let mut hostfxr_handle = MaybeUninit::uninit();
let result = unsafe {
self.lib.hostfxr_initialize_for_runtime_config(
runtime_config_path.as_ref().as_ptr(),
parameters,
hostfxr_handle.as_mut_ptr(),
)
};
HostExitCode::from(result).to_result()?;
Ok(HostfxrContext::new(
unsafe { HostfxrHandle::new_unchecked(hostfxr_handle.assume_init()) },
self,
))
}
}