-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
Copy pathnativehost.cpp
352 lines (306 loc) · 11.5 KB
/
nativehost.cpp
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// Standard headers
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
// Provided by the AppHost NuGet package and installed as an SDK pack
#include <nethost.h>
// Header files copied from https://github.com/dotnet/core-setup
#include <coreclr_delegates.h>
#include <hostfxr.h>
#ifdef WINDOWS
#include <Windows.h>
#define STR(s) L ## s
#define CH(c) L ## c
#define DIR_SEPARATOR L'\\'
#define string_compare wcscmp
#else
#include <dlfcn.h>
#include <limits.h>
#define STR(s) s
#define CH(c) c
#define DIR_SEPARATOR '/'
#define MAX_PATH PATH_MAX
#define string_compare strcmp
#endif
using string_t = std::basic_string<char_t>;
namespace
{
// Globals to hold hostfxr exports
hostfxr_initialize_for_dotnet_command_line_fn init_for_cmd_line_fptr;
hostfxr_initialize_for_runtime_config_fn init_for_config_fptr;
hostfxr_get_runtime_delegate_fn get_delegate_fptr;
hostfxr_run_app_fn run_app_fptr;
hostfxr_close_fn close_fptr;
// Forward declarations
bool load_hostfxr(const char_t *app);
load_assembly_and_get_function_pointer_fn get_dotnet_load_assembly(const char_t *assembly);
int run_component_example(const string_t& root_path);
int run_app_example(const string_t& root_path);
}
#if defined(WINDOWS)
int __cdecl wmain(int argc, wchar_t *argv[])
#else
int main(int argc, char *argv[])
#endif
{
// Get the current executable's directory
// This sample assumes the managed assembly to load and its runtime configuration file are next to the host
char_t host_path[MAX_PATH];
#if WINDOWS
auto size = ::GetFullPathNameW(argv[0], sizeof(host_path) / sizeof(char_t), host_path, nullptr);
assert(size != 0);
#else
auto resolved = realpath(argv[0], host_path);
assert(resolved != nullptr);
#endif
string_t root_path = host_path;
auto pos = root_path.find_last_of(DIR_SEPARATOR);
assert(pos != string_t::npos);
root_path = root_path.substr(0, pos + 1);
if (argc > 1 && string_compare(argv[1], STR("app")) == 0)
{
return run_app_example(root_path);
}
else
{
return run_component_example(root_path);
}
}
namespace
{
int run_component_example(const string_t& root_path)
{
//
// STEP 1: Load HostFxr and get exported hosting functions
//
if (!load_hostfxr(nullptr))
{
assert(false && "Failure: load_hostfxr()");
return EXIT_FAILURE;
}
//
// STEP 2: Initialize and start the .NET Core runtime
//
const string_t config_path = root_path + STR("DotNetLib.runtimeconfig.json");
load_assembly_and_get_function_pointer_fn load_assembly_and_get_function_pointer = nullptr;
load_assembly_and_get_function_pointer = get_dotnet_load_assembly(config_path.c_str());
assert(load_assembly_and_get_function_pointer != nullptr && "Failure: get_dotnet_load_assembly()");
//
// STEP 3: Load managed assembly and get function pointer to a managed method
//
const string_t dotnetlib_path = root_path + STR("DotNetLib.dll");
const char_t *dotnet_type = STR("DotNetLib.Lib, DotNetLib");
const char_t *dotnet_type_method = STR("Hello");
// <SnippetLoadAndGet>
// Function pointer to managed delegate
component_entry_point_fn hello = nullptr;
int rc = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnet_type,
dotnet_type_method,
nullptr /*delegate_type_name*/,
nullptr,
(void**)&hello);
// </SnippetLoadAndGet>
assert(rc == 0 && hello != nullptr && "Failure: load_assembly_and_get_function_pointer()");
//
// STEP 4: Run managed code
//
struct lib_args
{
const char_t *message;
int number;
};
for (int i = 0; i < 3; ++i)
{
// <SnippetCallManaged>
lib_args args
{
STR("from host!"),
i
};
hello(&args, sizeof(args));
// </SnippetCallManaged>
}
// Function pointer to managed delegate with non-default signature
typedef void (CORECLR_DELEGATE_CALLTYPE *custom_entry_point_fn)(lib_args args);
custom_entry_point_fn custom = nullptr;
lib_args args
{
STR("from host!"),
-1
};
// UnmanagedCallersOnly
rc = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnet_type,
STR("CustomEntryPointUnmanagedCallersOnly") /*method_name*/,
UNMANAGEDCALLERSONLY_METHOD,
nullptr,
(void**)&custom);
assert(rc == 0 && custom != nullptr && "Failure: load_assembly_and_get_function_pointer()");
custom(args);
// Custom delegate type
rc = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnet_type,
STR("CustomEntryPoint") /*method_name*/,
STR("DotNetLib.Lib+CustomEntryPointDelegate, DotNetLib") /*delegate_type_name*/,
nullptr,
(void**)&custom);
assert(rc == 0 && custom != nullptr && "Failure: load_assembly_and_get_function_pointer()");
custom(args);
return EXIT_SUCCESS;
}
int run_app_example(const string_t& root_path)
{
const string_t app_path = root_path + STR("App.dll");
if (!load_hostfxr(app_path.c_str()))
{
assert(false && "Failure: load_hostfxr()");
return EXIT_FAILURE;
}
// Load .NET Core
hostfxr_handle cxt = nullptr;
std::vector<const char_t*> args { app_path.c_str(), STR("app_arg_1"), STR("app_arg_2") };
int rc = init_for_cmd_line_fptr(args.size(), args.data(), nullptr, &cxt);
if (rc != 0 || cxt == nullptr)
{
std::cerr << "Init failed: " << std::hex << std::showbase << rc << std::endl;
close_fptr(cxt);
return EXIT_FAILURE;
}
// Get the function pointer to get function pointers
get_function_pointer_fn get_function_pointer;
rc = get_delegate_fptr(
cxt,
hdt_get_function_pointer,
(void**)&get_function_pointer);
if (rc != 0 || get_function_pointer == nullptr)
std::cerr << "Get delegate failed: " << std::hex << std::showbase << rc << std::endl;
// Function pointer to App.IsWaiting
typedef unsigned char (CORECLR_DELEGATE_CALLTYPE* is_waiting_fn)();
is_waiting_fn is_waiting;
rc = get_function_pointer(
STR("App, App"),
STR("IsWaiting"),
UNMANAGEDCALLERSONLY_METHOD,
nullptr, nullptr, (void**)&is_waiting);
assert(rc == 0 && is_waiting != nullptr && "Failure: get_function_pointer()");
// Function pointer to App.Hello
typedef void (CORECLR_DELEGATE_CALLTYPE* hello_fn)(const char*);
hello_fn hello;
rc = get_function_pointer(
STR("App, App"),
STR("Hello"),
UNMANAGEDCALLERSONLY_METHOD,
nullptr, nullptr, (void**)&hello);
assert(rc == 0 && hello != nullptr && "Failure: get_function_pointer()");
// Invoke the functions in a different thread from the main app
std::thread t([&]
{
while (is_waiting() != 1)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
for (int i = 0; i < 3; ++i)
hello("from host!");
});
// Run the app
run_app_fptr(cxt);
t.join();
close_fptr(cxt);
return EXIT_SUCCESS;
}
}
/********************************************************************************************
* Function used to load and activate .NET Core
********************************************************************************************/
namespace
{
// Forward declarations
void *load_library(const char_t *);
void *get_export(void *, const char *);
#ifdef WINDOWS
void *load_library(const char_t *path)
{
HMODULE h = ::LoadLibraryW(path);
assert(h != nullptr);
return (void*)h;
}
void *get_export(void *h, const char *name)
{
void *f = ::GetProcAddress((HMODULE)h, name);
assert(f != nullptr);
return f;
}
#else
void *load_library(const char_t *path)
{
void *h = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
assert(h != nullptr);
return h;
}
void *get_export(void *h, const char *name)
{
void *f = dlsym(h, name);
assert(f != nullptr);
return f;
}
#endif
// <SnippetLoadHostFxr>
// Using the nethost library, discover the location of hostfxr and get exports
bool load_hostfxr(const char_t *assembly_path)
{
get_hostfxr_parameters params { sizeof(get_hostfxr_parameters), assembly_path, nullptr };
// Pre-allocate a large buffer for the path to hostfxr
char_t buffer[MAX_PATH];
size_t buffer_size = sizeof(buffer) / sizeof(char_t);
int rc = get_hostfxr_path(buffer, &buffer_size, ¶ms);
if (rc != 0)
return false;
// Load hostfxr and get desired exports
// NOTE: The .NET Runtime does not support unloading any of its native libraries. Running
// dlclose/FreeLibrary on any .NET libraries produces undefined behavior.
void *lib = load_library(buffer);
init_for_cmd_line_fptr = (hostfxr_initialize_for_dotnet_command_line_fn)get_export(lib, "hostfxr_initialize_for_dotnet_command_line");
init_for_config_fptr = (hostfxr_initialize_for_runtime_config_fn)get_export(lib, "hostfxr_initialize_for_runtime_config");
get_delegate_fptr = (hostfxr_get_runtime_delegate_fn)get_export(lib, "hostfxr_get_runtime_delegate");
run_app_fptr = (hostfxr_run_app_fn)get_export(lib, "hostfxr_run_app");
close_fptr = (hostfxr_close_fn)get_export(lib, "hostfxr_close");
return (init_for_config_fptr && get_delegate_fptr && close_fptr);
}
// </SnippetLoadHostFxr>
// <SnippetInitialize>
// Load and initialize .NET Core and get desired function pointer for scenario
load_assembly_and_get_function_pointer_fn get_dotnet_load_assembly(const char_t *config_path)
{
// Load .NET Core
void *load_assembly_and_get_function_pointer = nullptr;
hostfxr_handle cxt = nullptr;
int rc = init_for_config_fptr(config_path, nullptr, &cxt);
if (rc != 0 || cxt == nullptr)
{
std::cerr << "Init failed: " << std::hex << std::showbase << rc << std::endl;
close_fptr(cxt);
return nullptr;
}
// Get the load assembly function pointer
rc = get_delegate_fptr(
cxt,
hdt_load_assembly_and_get_function_pointer,
&load_assembly_and_get_function_pointer);
if (rc != 0 || load_assembly_and_get_function_pointer == nullptr)
std::cerr << "Get delegate failed: " << std::hex << std::showbase << rc << std::endl;
close_fptr(cxt);
return (load_assembly_and_get_function_pointer_fn)load_assembly_and_get_function_pointer;
}
// </SnippetInitialize>
}