-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathActivationFactory.cs
More file actions
429 lines (389 loc) · 16.3 KB
/
Copy pathActivationFactory.cs
File metadata and controls
429 lines (389 loc) · 16.3 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using WinRT.Interop;
namespace WinRT
{
internal unsafe sealed class DllModule
{
private static readonly string _currentModuleDirectory = AppContext.BaseDirectory;
private static readonly Dictionary<string, DllModule> _cache = new Dictionary<string, DllModule>(StringComparer.Ordinal);
private readonly string _fileName;
private readonly IntPtr _moduleHandle;
private readonly delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int> _GetActivationFactory;
private readonly delegate* unmanaged[Stdcall]<int> _CanUnloadNow; // TODO: Eventually periodically call
public static bool TryLoad(string fileName, out DllModule module)
{
lock (_cache)
{
if (_cache.TryGetValue(fileName, out module))
{
return true;
}
else if (TryCreate(fileName, out module))
{
_cache[fileName] = module;
return true;
}
return false;
}
}
private static bool TryCreate(string fileName, out DllModule module)
{
// Explicitly look for module in the same directory as this one, and
// use altered search path to ensure any dependencies in the same directory are found.
IntPtr moduleHandle = IntPtr.Zero;
moduleHandle = Platform.LoadLibraryExW(System.IO.Path.Combine(_currentModuleDirectory, fileName), IntPtr.Zero, /* LOAD_WITH_ALTERED_SEARCH_PATH */ 8);
#if NET
if (moduleHandle == IntPtr.Zero)
{
NativeLibrary.TryLoad(fileName, typeof(DllModule).Assembly, null, out moduleHandle);
}
#endif
if (moduleHandle == IntPtr.Zero)
{
module = null;
return false;
}
void* getActivationFactory = null;
ReadOnlySpan<byte> functionName =
#if NET7_0_OR_GREATER || CsWinRT_LANG_11_FEATURES
"DllGetActivationFactory"u8;
#else
Encoding.ASCII.GetBytes("DllGetActivationFactory");
#endif
getActivationFactory = (void*)Platform.TryGetProcAddress(moduleHandle, functionName);
if (getActivationFactory == null)
{
module = null;
return false;
}
module = new DllModule(
fileName,
moduleHandle,
getActivationFactory);
return true;
}
private DllModule(string fileName, IntPtr moduleHandle, void* getActivationFactory)
{
_fileName = fileName;
_moduleHandle = moduleHandle;
_GetActivationFactory = (delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int>)getActivationFactory;
void* canUnloadNow = null;
ReadOnlySpan<byte> functionName =
#if NET7_0_OR_GREATER || CsWinRT_LANG_11_FEATURES
"DllCanUnloadNow"u8;
#else
Encoding.ASCII.GetBytes("DllCanUnloadNow");
#endif
canUnloadNow = (void*)Platform.TryGetProcAddress(_moduleHandle, functionName);
if (canUnloadNow != null)
{
_CanUnloadNow = (delegate* unmanaged[Stdcall]<int>)canUnloadNow;
}
}
public (ObjectReference<IUnknownVftbl> obj, int hr) GetActivationFactory(string runtimeClassId)
{
IntPtr instancePtr = IntPtr.Zero;
try
{
MarshalString.Pinnable __runtimeClassId = new(runtimeClassId);
fixed (void* ___runtimeClassId = __runtimeClassId)
{
int hr = _GetActivationFactory(MarshalString.GetAbi(ref __runtimeClassId), &instancePtr);
if (hr == 0)
{
var objRef = ObjectReference<IUnknownVftbl>.Attach(ref instancePtr, IID.IID_IActivationFactory);
return (objRef, hr);
}
else
{
return (null, hr);
}
}
}
finally
{
MarshalInspectable<object>.DisposeAbi(instancePtr);
}
}
~DllModule()
{
System.Diagnostics.Debug.Assert(_CanUnloadNow == null || _CanUnloadNow() == 0); // S_OK
lock (_cache)
{
_cache.Remove(_fileName);
}
if ((_moduleHandle != IntPtr.Zero) && !Platform.FreeLibrary(_moduleHandle))
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
}
}
internal sealed class WinRTModule
{
private static volatile WinRTModule _instance;
private readonly IntPtr _mtaCookie;
private static WinRTModule MakeWinRTModule()
{
global::System.Threading.Interlocked.CompareExchange(ref _instance, new WinRTModule(), null);
return _instance;
}
public static WinRTModule Instance => _instance ?? MakeWinRTModule();
public unsafe WinRTModule()
{
IntPtr mtaCookie;
Marshal.ThrowExceptionForHR(Platform.CoIncrementMTAUsage(&mtaCookie));
_mtaCookie = mtaCookie;
}
public static unsafe (ObjectReference<I> obj, int hr) GetActivationFactory<I>(string runtimeClassId, Guid iid)
{
var module = Instance; // Ensure COM is initialized
IntPtr instancePtr = IntPtr.Zero;
try
{
MarshalString.Pinnable __runtimeClassId = new(runtimeClassId);
fixed (void* ___runtimeClassId = __runtimeClassId)
{
int hr = Platform.RoGetActivationFactory(MarshalString.GetAbi(ref __runtimeClassId), &iid, &instancePtr);
if (hr == 0)
{
var objRef = ObjectReference<I>.Attach(ref instancePtr, iid);
return (objRef, hr);
}
else
{
return (null, hr);
}
}
}
finally
{
MarshalInspectable<object>.DisposeAbi(instancePtr);
}
}
~WinRTModule()
{
Marshal.ThrowExceptionForHR(Platform.CoDecrementMTAUsage(_mtaCookie));
}
}
#nullable enable
/// <summary>
/// Provides support for activating WinRT types.
/// </summary>
#if EMBED
internal
#else
public
#endif
static class ActivationFactory
{
#if NET
/// <summary>
/// This provides a hook into activation to hook/mock activation of WinRT types.
/// </summary>
public static Func<string, Guid, IntPtr>? ActivationHandler { get; set; }
#endif
/// <summary>
/// Activates a WinRT type with the specified runtime class name.
/// </summary>
/// <param name="typeName">The ID of the activatable class (the fully qualified type name).</param>
/// <returns>An <see cref="IObjectReference"/> instance wrapping an instance of the activated WinRT type.</returns>
/// <exception cref="NotSupportedException">Thrown if <paramref name="typeName"/> is not registered, <c>CsWinRTEnableManifestFreeActivation</c> is disabled, and <c>CsWinRTManifestFreeActivationReportOriginalException</c> is not set.</exception>
/// <exception cref="Exception">Thrown for any failure to activate the specified type (the exact exception type might be a derived type).</exception>
/// <remarks>
/// This method will try to activate the target type as follows:
/// <list type="bullet">
/// <item>If <see cref="ActivationHandler"/> is set, it will be used first.</item>
/// <item>Otherwise, <a href="https://learn.microsoft.com/windows/win32/api/roapi/nf-roapi-rogetactivationfactory"><c>RoGetActivationFactory</c></a> will be used.</item>
/// <item>Otherwise, the manifest-free fallback path will be used to try to resolve the target .dll to load based on <paramref name="typeName"/>.</item>
/// </list>
/// </remarks>
public static IObjectReference Get(string typeName)
{
#if NET
// Check hook first
if (ActivationHandler != null)
{
var factoryFromhandler = GetFromActivationHandler(typeName, IID.IID_IActivationFactory);
if (factoryFromhandler != null)
{
return factoryFromhandler;
}
}
#endif
// Prefer the RoGetActivationFactory HRESULT failure over the LoadLibrary/etc. failure
int hr;
ObjectReference<IUnknownVftbl> factory;
(factory, hr) = WinRTModule.GetActivationFactory<IUnknownVftbl>(typeName, IID.IID_IActivationFactory);
if (factory != null)
{
return factory;
}
ThrowIfClassNotRegisteredAndManifestFreeActivationDisabled(typeName, hr);
return ManifestFreeGet(typeName, hr);
}
/// <summary>
/// Activates a WinRT type with the specified runtime class name and interface ID.
/// </summary>
/// <param name="typeName">The ID of the activatable class (the fully qualified type name).</param>
/// <param name="iid">The interface ID to use to dispatch the activated type.</param>
/// <returns>An <see cref="IObjectReference"/> instance wrapping an instance of the activated WinRT type, dispatched with the specified interface ID.</returns>
/// <inheritdoc cref="Get(string)"/>
#if NET
public static IObjectReference Get(string typeName, Guid iid)
#else
public static ObjectReference<I> Get<I>(string typeName, Guid iid)
#endif
{
#if NET
// Check hook first
if (ActivationHandler != null)
{
var factoryFromhandler = GetFromActivationHandler(typeName, iid);
if (factoryFromhandler != null)
{
return factoryFromhandler;
}
}
#endif
// Prefer the RoGetActivationFactory HRESULT failure over the LoadLibrary/etc. failure
int hr;
#if NET
ObjectReference<IUnknownVftbl> factory;
(factory, hr) = WinRTModule.GetActivationFactory<IUnknownVftbl>(typeName, iid);
if (factory != null)
{
return factory;
}
ThrowIfClassNotRegisteredAndManifestFreeActivationDisabled(typeName, hr);
return ManifestFreeGet(typeName, iid, hr);
#else
ObjectReference<I> factory;
(factory, hr) = WinRTModule.GetActivationFactory<I>(typeName, iid);
if (factory != null)
{
return factory;
}
ThrowIfClassNotRegisteredAndManifestFreeActivationDisabled(typeName, hr);
return ManifestFreeGet<I>(typeName, iid, hr);
#endif
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static IObjectReference ManifestFreeGet(string typeName, int hr)
{
var moduleName = typeName;
while (true)
{
var lastSegment = moduleName.LastIndexOf(".", StringComparison.Ordinal);
if (lastSegment <= 0)
{
Marshal.ThrowExceptionForHR(hr);
}
moduleName = moduleName.Remove(lastSegment);
DllModule? module = null;
if (DllModule.TryLoad(moduleName + ".dll", out module))
{
(ObjectReference<IUnknownVftbl> factory, hr) = module.GetActivationFactory(typeName);
if (factory != null)
{
return factory;
}
}
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
#if NET
private static IObjectReference ManifestFreeGet(string typeName, Guid iid, int hr)
#else
private static ObjectReference<I> ManifestFreeGet<I>(string typeName, Guid iid, int hr)
#endif
{
var moduleName = typeName;
while (true)
{
var lastSegment = moduleName.LastIndexOf(".", StringComparison.Ordinal);
if (lastSegment <= 0)
{
Marshal.ThrowExceptionForHR(hr);
}
moduleName = moduleName.Remove(lastSegment);
if (DllModule.TryLoad(moduleName + ".dll", out DllModule module))
{
ObjectReference<IUnknownVftbl> activationFactory;
(activationFactory, hr) = module.GetActivationFactory(typeName);
if (activationFactory != null)
{
using (activationFactory)
{
#if NET
return activationFactory.As<IUnknownVftbl>(iid);
#else
return activationFactory.As<I>(iid);
#endif
}
}
}
}
}
private static void ThrowIfClassNotRegisteredAndManifestFreeActivationDisabled(string typeName, int hr)
{
// If manifest free activation is enabled, we never throw here.
// Callers will always try the fallback path if we didn't succeed.
if (FeatureSwitches.EnableManifestFreeActivation)
{
return;
}
// Throw the exception directly, if the user requested to preserve it
if (FeatureSwitches.ManifestFreeActivationReportOriginalException)
{
Marshal.ThrowExceptionForHR(hr);
}
[MethodImpl(MethodImplOptions.NoInlining)]
static Exception GetException(string typeName, int hr)
{
Exception exception = Marshal.GetExceptionForHR(hr)!;
if (hr == ExceptionHelpers.REGDB_E_CLASSNOTREG)
{
throw new NotSupportedException(
$"Failed to activate type with runtime class name '{typeName}' with 'RoGetActivationFactory' (it returned 0x80040154, ie. 'REGDB_E_CLASSNOTREG'). Make sure to add the activatable class id for the type " +
"to the APPX manifest, or enable the manifest free activation fallback path by setting the 'CsWinRTEnableManifestFreeActivation' property (note: the fallback path incurs a performance hit).", exception);
}
return exception;
}
// Explicit throw so the JIT can see it and correctly optimize for always throwing paths.
// The exception instantiation is in a separate method so that codegen remains separate.
throw GetException(typeName, hr);
}
#if NET
private static IObjectReference? GetFromActivationHandler(string typeName, Guid iid)
{
var activationHandler = ActivationHandler;
if (activationHandler != null)
{
IntPtr instancePtr = IntPtr.Zero;
try
{
instancePtr = activationHandler(typeName, iid);
if (instancePtr != IntPtr.Zero)
{
return ObjectReference<IUnknownVftbl>.Attach(ref instancePtr, iid);
}
}
finally
{
MarshalInspectable<object>.DisposeAbi(instancePtr);
}
}
return null;
}
#endif
}
#nullable restore
}