/
host.cpp
369 lines (320 loc) · 12.3 KB
/
host.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright (c) Govert van Drimmelen. All rights reserved.
// xllhost.cpp : Defines the entry point for the XLL host
#include "host.h"
#include "exports.h"
// Include the official nethost API and indicate
// consumption should be as a static library.
#define NETHOST_USE_AS_STATIC
#include <nethost.h>
#include <coreclr_delegates.h>
#include <hostfxr.h>
#include <error_codes.h>
#include <cassert>
#include <iostream>
#include <string>
#include <filesystem>
#include "TempDir.h"
#include "utils.h"
#include "dnainfo.h"
using string_t = std::basic_string<char_t>;
#if _WIN64
const std::wstring requiredBitness = L"x64";
#else
const std::wstring requiredBitness = L"x86";
#endif
// Globals to hold hostfxr exports
hostfxr_initialize_for_runtime_config_fn init_fptr;
hostfxr_get_runtime_delegate_fn get_delegate_fptr;
hostfxr_get_runtime_property_value_fn get_property_fptr;
hostfxr_close_fn close_fptr;
// Forward declarations
bool load_hostfxr(int& rc, std::wstring& loadError);
int write_resource_to_file(HMODULE hModuleXll, const std::wstring& resourceName, const std::wstring& resourceType, const std::wstring& filePath);
load_assembly_and_get_function_pointer_fn get_dotnet_load_assembly(HMODULE hModuleXll, int majorRuntimeVersion, const std::wstring& rollForward);
// Provide a callback for any catastrophic failures.
// The provided callback will be the last call prior to a rude-abort of the process.
// void __stdcall set_failure_callback(failure_fn cb) {}
// Preload the runtime.
// The runtime is lazily loaded whenever the first export is called. This function
// preloads the runtime independent of calling any export and avoids the startup
// cost associated with calling an export for the first time.
void __stdcall preload_runtime(void) {}
TempDir tempDir(L"ExcelDna.Host");
// TODO: Might return the fn*
int load_runtime_and_run(const std::wstring& basePath, XlAddInExportInfo* pExportInfo, HMODULE hModuleXll, LPCWSTR pathXll)
{
//
// STEP 1: Load HostFxr and get exported hosting functions
//
int rc = 0;
std::wstring loadError;
if (!load_hostfxr(rc, loadError))
{
std::wstring msg;
if (rc == CoreHostLibMissingFailure)
{
std::wstring requiredRuntime = L".NET Desktop Runtime 6.0.2+ " + requiredBitness;
msg = std::format(L"{0} is not installed, corrupted or incomplete.\n\nYou can download {0} from https://dotnet.microsoft.com/en-us/download/dotnet/6.0", requiredRuntime);
}
else
{
msg = loadError;
}
ShowHostError(msg);
return EXIT_FAILURE;
}
//
// STEP 2: Initialize and start the .NET Core runtime
//
int majorRuntimeVersion;
HRESULT hr = GetMajorRuntimeVersion(majorRuntimeVersion);
if (FAILED(hr))
majorRuntimeVersion = 6;
std::wstring rollForward;
hr = GetRollForward(rollForward);
if (FAILED(hr))
rollForward = L"";
load_assembly_and_get_function_pointer_fn load_assembly_and_get_function_pointer = get_dotnet_load_assembly(hModuleXll, majorRuntimeVersion, rollForward);
if (load_assembly_and_get_function_pointer == nullptr)
return EXIT_FAILURE;
//
// STEP 3: Copy managed assembly from resources to some temp file if needed
//
std::wstring hostFile = PathCombine(basePath, L"ExcelDna.ManagedHost.dll");
if (!std::filesystem::exists(hostFile))
{
hostFile = PathCombine(tempDir.GetPath(), L"ExcelDna.ManagedHost.dll");
if (!std::filesystem::exists(hostFile))
{
int r = write_resource_to_file(hModuleXll, L"EXCELDNA.MANAGEDHOST", L"ASSEMBLY", hostFile);
if (r != EXIT_SUCCESS)
return r;
}
}
//
// STEP 4: Load managed assembly and get function pointer to a managed method
//
const string_t dotnetlib_path = hostFile;
const char_t* dotnet_type = L"ExcelDna.ManagedHost.AddInInitialize, ExcelDna.ManagedHost";
const char_t* dotnet_type_method = L"Initialize";
// Function pointer to managed delegate with non-default signature
typedef short (CORECLR_DELEGATE_CALLTYPE* xladdin_initialize_fn)(void* xlAddInExportInfo, void* hModuleXLL, void* pPathXLL, BYTE disableAssemblyContextUnload, void* pTempDirPath);
xladdin_initialize_fn init = nullptr;
rc = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnet_type,
dotnet_type_method,
UNMANAGEDCALLERSONLY_METHOD,
nullptr,
(void**)&init);
if (rc != 0 || init == nullptr)
{
std::wstringstream stream;
stream << L"Loading ExcelDna.ManagedHost failed: " << std::hex << std::showbase << rc;
ShowHostError(stream.str());
return EXIT_FAILURE;
}
bool disableAssemblyContextUnload;
hr = GetDisableAssemblyContextUnload(disableAssemblyContextUnload);
if (FAILED(hr))
disableAssemblyContextUnload = false;
std::wstring tempDirPath = tempDir.GetPath();
short res = init(pExportInfo, hModuleXll, (void*)pathXll, disableAssemblyContextUnload, (void*)tempDirPath.c_str());
return res == 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
/********************************************************************************************
* Function used to load and activate .NET Core
********************************************************************************************/
void* load_library(const char_t* path)
{
HMODULE h = ::LoadLibraryW(path);
return (void*)h;
}
void* get_export(void* h, const char* name)
{
void* f = ::GetProcAddress((HMODULE)h, name);
return f;
}
// Using the nethost library, discover the location of hostfxr and get exports
bool load_hostfxr(int& rc, std::wstring& loadError)
{
// Pre-allocate a large buffer for the path to hostfxr
char_t buffer[MAX_PATH];
size_t buffer_size = sizeof(buffer) / sizeof(char_t);
rc = get_hostfxr_path(buffer, &buffer_size, nullptr);
if (rc != 0)
{
std::wstringstream stream;
stream << "Getting hostfxr path failed.\n\nError code: " << std::hex << std::showbase << rc << ".";
loadError = stream.str();
return false;
}
// Load hostfxr and get desired exports
void* lib = load_library(buffer);
if (lib == nullptr)
{
loadError = std::format(L"Loading library {} failed.", buffer);
return false;
}
init_fptr = (hostfxr_initialize_for_runtime_config_fn)get_export(lib, "hostfxr_initialize_for_runtime_config");
if (init_fptr == nullptr)
{
loadError = L"Retrieving the address of the exported function hostfxr_initialize_for_runtime_config failed.";
return false;
}
get_delegate_fptr = (hostfxr_get_runtime_delegate_fn)get_export(lib, "hostfxr_get_runtime_delegate");
if (get_delegate_fptr == nullptr)
{
loadError = L"Retrieving the address of the exported function hostfxr_get_runtime_delegate failed.";
return false;
}
get_property_fptr = (hostfxr_get_runtime_property_value_fn)get_export(lib, "hostfxr_get_runtime_property_value");
if (get_property_fptr == nullptr)
{
loadError = L"Retrieving the address of the exported function hostfxr_get_runtime_property_value failed.";
return false;
}
close_fptr = (hostfxr_close_fn)get_export(lib, "hostfxr_close");
if (close_fptr == nullptr)
{
loadError = L"Retrieving the address of the exported function hostfxr_close failed.";
return false;
}
return true;
}
std::wstring get_runtime_property(const hostfxr_handle host_context_handle, const std::wstring& name)
{
const wchar_t* value = NULL;
int rc = get_property_fptr(host_context_handle, name.c_str(), &value);
if (STATUS_CODE_SUCCEEDED(rc) && value != NULL)
{
return value;
}
return L"";
}
std::wstring get_loaded_runtime_version()
{
return GetDirectoryName(get_runtime_property(NULL, L"FX_DEPS_FILE"));
}
// Load and initialize .NET Core and get desired function pointer for scenario
load_assembly_and_get_function_pointer_fn get_dotnet_load_assembly(HMODULE hModuleXll, int majorRuntimeVersion, const std::wstring& rollForward)
{
std::wstring configFile = PathCombine(tempDir.GetPath(), L"ExcelDna.Host.runtimeconfig.json");
std::string version(majorRuntimeVersion >= 7 ? std::format("{0}.0.0", majorRuntimeVersion) : "6.0.2");
std::wstring customRuntimeConfiguration;
bool useCustomRuntimeConfiguration = (SUCCEEDED(GetCustomRuntimeConfiguration(customRuntimeConfiguration)) && !customRuntimeConfiguration.empty());
if (!useCustomRuntimeConfiguration)
{
std::string tfm(std::format("net{0}.0", majorRuntimeVersion));
std::string rollForwardOption = rollForward.length() > 0 ? std::format(R"("rollForward": "{0}",)", ANSIWStringToString(rollForward)) : "";
std::string configText = std::format(R"({{
"runtimeOptions": {{
"tfm": "{0}",
{2}
"framework": {{
"name": "Microsoft.WindowsDesktop.App",
"version": "{1}"
}}
}}
}})", tfm, version, rollForwardOption);
HRESULT hr = WriteAllBytes(configFile, (void*)configText.c_str(), static_cast<DWORD>(configText.length()));
if (FAILED(hr))
{
std::wstringstream stream;
stream << "Saving ExcelDna.Host.runtimeconfig.json failed: " << std::hex << std::showbase << hr;
ShowHostError(stream.str());
return nullptr;
}
}
else
{
std::wstring customRuntimeConfigurationFilePath = PathCombine(GetDirectory(GetAddInFullPath()), customRuntimeConfiguration);
if (std::filesystem::exists(customRuntimeConfigurationFilePath))
{
configFile = customRuntimeConfigurationFilePath;
}
else
{
int r = write_resource_to_file(hModuleXll, L"__CUSTOM_RUNTIMECONFIG__", L"SOURCE", configFile);
if (r != EXIT_SUCCESS)
return nullptr;
}
}
// Load .NET Core
void* load_assembly_and_get_function_pointer = nullptr;
hostfxr_handle cxt = nullptr;
int rc = init_fptr(configFile.c_str(), nullptr, &cxt);
if (!STATUS_CODE_SUCCEEDED(rc) || cxt == nullptr)
{
std::wstring requiredRuntime = (useCustomRuntimeConfiguration ? L"custom .NET runtime" : std::format(L".NET Desktop Runtime {0} {1}", std::wstring(version.begin(), version.end()), requiredBitness));
if (rc == CoreHostIncompatibleConfig)
{
std::wstring msg = L"The required " + requiredRuntime + L" is incompatible with the runtime " + get_loaded_runtime_version() + L" already loaded in the process.\n\nYou can try to disable other Excel add-ins to resolve the conflict.";
ShowHostError(msg);
}
else if (rc == FrameworkMissingFailure)
{
std::wstring msg = std::format(L"It was not possible to find a compatible framework version for {0}.\n\nYou can download {0} from https://dotnet.microsoft.com/en-us/download/dotnet/", requiredRuntime);
ShowHostError(msg);
}
else
{
std::wstringstream stream;
stream << L".NET runtime initialization failed: " << std::hex << std::showbase << rc << std::endl << std::endl;
stream << L"You can find more information about this error at https://github.com/dotnet/runtime/blob/main/docs/design/features/host-error-codes.md";
ShowHostError(stream.str());
}
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 (!STATUS_CODE_SUCCEEDED(rc) || load_assembly_and_get_function_pointer == nullptr)
{
std::wstringstream stream;
stream << "Get .NET runtime delegate failed: " << std::hex << std::showbase << rc << std::endl << std::endl;
stream << L"You can find more information about this error at https://github.com/dotnet/runtime/blob/main/docs/design/features/host-error-codes.md";
ShowHostError(stream.str());
close_fptr(cxt);
return nullptr;
}
close_fptr(cxt);
return (load_assembly_and_get_function_pointer_fn)load_assembly_and_get_function_pointer;
}
int write_resource_to_file(HMODULE hModuleXll, const std::wstring& resourceName, const std::wstring& resourceType, const std::wstring& filePath)
{
HRSRC hResManagedHost = FindResource(hModuleXll, resourceName.c_str(), resourceType.c_str());
if (hResManagedHost == NULL)
{
ShowHostError(L"Failure to find resource " + resourceName);
return EXIT_FAILURE;
}
HGLOBAL hManagedHost = LoadResource(hModuleXll, hResManagedHost);
if (hManagedHost == NULL)
{
ShowHostError(L"Failure to load resource " + resourceName);
return EXIT_FAILURE;
}
void* buf = LockResource(hManagedHost);
if (buf == NULL)
{
ShowHostError(L"Failure to lock resource " + resourceName);
return EXIT_FAILURE;
}
DWORD resSize = SizeofResource(hModuleXll, hResManagedHost);
SafeByteArray safeBytes(buf, resSize);
byte* pData;
int nSize = safeBytes.AccessData(&pData);
HRESULT hr = WriteAllBytes(filePath, pData, nSize);
if (FAILED(hr))
{
std::wstringstream stream;
stream << L"Saving " << resourceName << L" failed: " << std::hex << std::showbase << hr;
ShowHostError(stream.str());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}