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
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
#include "precomp.h"
static DNCSTATE vstate = { };
// internal function declarations
static HRESULT LoadModulePaths(
__in DNCSTATE* pState
);
static HRESULT LoadDncConfiguration(
__in DNCSTATE* pState,
__in const BOOTSTRAPPER_CREATE_ARGS* pArgs
);
static HRESULT LoadRuntime(
__in DNCSTATE* pState
);
static HRESULT LoadManagedBootstrapperApplicationFactory(
__in DNCSTATE* pState
);
// function definitions
extern "C" BOOL WINAPI DllMain(
IN HINSTANCE hInstance,
IN DWORD dwReason,
IN LPVOID /* pvReserved */
)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
::DisableThreadLibraryCalls(hInstance);
vstate.hInstance = hInstance;
break;
case DLL_PROCESS_DETACH:
vstate.hInstance = NULL;
break;
}
return TRUE;
}
extern "C" HRESULT WINAPI BootstrapperApplicationCreate(
__in const BOOTSTRAPPER_CREATE_ARGS* pArgs,
__inout BOOTSTRAPPER_CREATE_RESULTS* pResults
)
{
HRESULT hr = S_OK;
IBootstrapperEngine* pEngine = NULL;
// coreclr.dll doesn't support unloading, so the rest of the .NET Core hosting stack doesn't support it either.
// This means we also can't unload.
pResults->fDisableUnloading = TRUE;
hr = BalInitializeFromCreateArgs(pArgs, &pEngine);
ExitOnFailure(hr, "Failed to initialize Bal.");
if (!vstate.fInitialized)
{
hr = XmlInitialize();
BalExitOnFailure(hr, "Failed to initialize XML.");
hr = LoadModulePaths(&vstate);
BalExitOnFailure(hr, "Failed to get the host base path.");
hr = LoadDncConfiguration(&vstate, pArgs);
BalExitOnFailure(hr, "Failed to get the dnc configuration.");
vstate.fInitialized = TRUE;
}
if (!vstate.fInitializedRuntime)
{
hr = LoadRuntime(&vstate);
BalExitOnFailure(hr, "Failed to load .NET Core runtime.");
vstate.fInitializedRuntime = TRUE;
hr = LoadManagedBootstrapperApplicationFactory(&vstate);
BalExitOnFailure(hr, "Failed to create the .NET Core bootstrapper application factory.");
}
BalLog(BOOTSTRAPPER_LOG_LEVEL_STANDARD, "Loading .NET Core SCD bootstrapper application.");
hr = vstate.pAppFactory->Create(pArgs, pResults);
BalExitOnFailure(hr, "Failed to create the .NET Core bootstrapper application.");
LExit:
ReleaseNullObject(pEngine);
return hr;
}
extern "C" void WINAPI BootstrapperApplicationDestroy()
{
BalUninitialize();
}
static HRESULT LoadModulePaths(
__in DNCSTATE* pState
)
{
HRESULT hr = S_OK;
hr = PathForCurrentProcess(&pState->sczModuleFullPath, pState->hInstance);
BalExitOnFailure(hr, "Failed to get the full host path.");
hr = PathGetDirectory(pState->sczModuleFullPath, &pState->sczAppBase);
BalExitOnFailure(hr, "Failed to get the directory of the full process path.");
hr = PathConcat(pState->sczAppBase, DNC_ASSEMBLY_FILE_NAME, &pState->sczManagedHostPath);
BalExitOnFailure(hr, "Failed to create managed host path.");
LExit:
return hr;
}
static HRESULT LoadDncConfiguration(
__in DNCSTATE* pState,
__in const BOOTSTRAPPER_CREATE_ARGS* pArgs
)
{
HRESULT hr = S_OK;
IXMLDOMDocument* pixdManifest = NULL;
IXMLDOMNode* pixnHost = NULL;
IXMLDOMNode* pixnPayload = NULL;
LPWSTR sczPayloadId = NULL;
LPWSTR sczPayloadXPath = NULL;
LPWSTR sczPayloadName = NULL;
hr = XmlLoadDocumentFromFile(pArgs->pCommand->wzBootstrapperApplicationDataPath, &pixdManifest);
BalExitOnFailure(hr, "Failed to load BalManifest '%ls'", pArgs->pCommand->wzBootstrapperApplicationDataPath);
hr = XmlSelectSingleNode(pixdManifest, L"/BootstrapperApplicationData/WixBalBAFactoryAssembly", &pixnHost);
BalExitOnFailure(hr, "Failed to get WixBalBAFactoryAssembly element.");
if (S_FALSE == hr)
{
hr = E_NOTFOUND;
BalExitOnRootFailure(hr, "Failed to find WixBalBAFactoryAssembly element in bootstrapper application config.");
}
hr = XmlGetAttributeEx(pixnHost, L"PayloadId", &sczPayloadId);
BalExitOnFailure(hr, "Failed to get WixBalBAFactoryAssembly/@PayloadId.");
hr = StrAllocFormatted(&sczPayloadXPath, L"/BootstrapperApplicationData/WixPayloadProperties[@Payload='%ls']", sczPayloadId);
BalExitOnFailure(hr, "Failed to format BAFactoryAssembly payload XPath.");
hr = XmlSelectSingleNode(pixdManifest, sczPayloadXPath, &pixnPayload);
if (S_FALSE == hr)
{
hr = E_NOTFOUND;
}
BalExitOnFailure(hr, "Failed to find WixPayloadProperties node for BAFactoryAssembly PayloadId: %ls.", sczPayloadId);
hr = XmlGetAttributeEx(pixnPayload, L"Name", &sczPayloadName);
BalExitOnFailure(hr, "Failed to get BAFactoryAssembly payload Name.");
hr = PathConcat(pArgs->pCommand->wzBootstrapperWorkingFolder, sczPayloadName, &pState->sczBaFactoryAssemblyPath);
BalExitOnFailure(hr, "Failed to create BaFactoryAssemblyPath.");
LPCWSTR wzFileName = PathFile(pState->sczBaFactoryAssemblyPath);
LPCWSTR wzExtension = PathExtension(pState->sczBaFactoryAssemblyPath);
if (!wzExtension)
{
BalExitOnFailure(hr = E_FAIL, "BaFactoryAssemblyPath has no extension.");
}
hr = StrAllocString(&pState->sczBaFactoryAssemblyName, wzFileName, wzExtension - wzFileName);
BalExitOnFailure(hr, "Failed to copy BAFactoryAssembly payload Name.");
hr = StrAllocString(&pState->sczBaFactoryDepsJsonPath, pState->sczBaFactoryAssemblyPath, wzExtension - pState->sczBaFactoryAssemblyPath);
BalExitOnFailure(hr, "Failed to initialize deps json path.");
hr = StrAllocString(&pState->sczBaFactoryRuntimeConfigPath, pState->sczBaFactoryDepsJsonPath, 0);
BalExitOnFailure(hr, "Failed to initialize runtime config path.");
hr = StrAllocConcat(&pState->sczBaFactoryDepsJsonPath, L".deps.json", 0);
BalExitOnFailure(hr, "Failed to concat extension to deps json path.");
hr = StrAllocConcat(&pState->sczBaFactoryRuntimeConfigPath, L".runtimeconfig.json", 0);
BalExitOnFailure(hr, "Failed to concat extension to runtime config path.");
LExit:
ReleaseStr(sczPayloadName);
ReleaseObject(pixnPayload);
ReleaseStr(sczPayloadXPath);
ReleaseStr(sczPayloadId);
ReleaseObject(pixnHost);
ReleaseObject(pixdManifest);
return hr;
}
static HRESULT LoadRuntime(
__in DNCSTATE* pState
)
{
HRESULT hr = S_OK;
hr = DnchostLoadRuntime(
&pState->hostfxrState,
pState->sczModuleFullPath,
pState->sczManagedHostPath,
pState->sczBaFactoryDepsJsonPath,
pState->sczBaFactoryRuntimeConfigPath);
return hr;
}
static HRESULT LoadManagedBootstrapperApplicationFactory(
__in DNCSTATE* pState
)
{
HRESULT hr = S_OK;
hr = DnchostCreateFactory(
&pState->hostfxrState,
pState->sczBaFactoryAssemblyName,
pState->sczBaFactoryAssemblyPath,
&pState->pAppFactory);
return hr;
}
|