|
| 1 | +// Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Runtime.CompilerServices; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using WinRT; |
| 7 | + |
| 8 | +namespace Microsoft.WSL.Containers; |
| 9 | + |
| 10 | +internal static class WinRTActivation |
| 11 | +{ |
| 12 | + [UnmanagedFunctionPointer(CallingConvention.StdCall)] |
| 13 | + private delegate int DllGetActivationFactoryFn(IntPtr classId, out IntPtr factory); |
| 14 | + |
| 15 | + private static DllGetActivationFactoryFn s_getDllFactory; |
| 16 | + |
| 17 | + // Overrides the WinRT activation to route activation of our types through our native DLL. |
| 18 | + #pragma warning disable CA2255 |
| 19 | + // CA2255 warns against using ModuleInitializer to discourage using it without thinking through some possible issues. |
| 20 | + // For example, unintuitive timing (it runs the first time a type is used, not when the module is loaded or inspected), |
| 21 | + // and limiting possible optimizations (if the module is loaded, but nothing that depended on the initializer is ever used). |
| 22 | + // In this case, we only need it to run before using any of our types, and everything in the assembly needs this |
| 23 | + // initialization, so it's fine. |
| 24 | + [ModuleInitializer] |
| 25 | + #pragma warning restore CA2255 |
| 26 | + internal static void Initialize() |
| 27 | + { |
| 28 | + // Get a pointer to the function in the DLL that creates activation factories. |
| 29 | + s_getDllFactory = Marshal.GetDelegateForFunctionPointer<DllGetActivationFactoryFn>( |
| 30 | + NativeLibrary.GetExport( |
| 31 | + NativeLibrary.Load("wslcsdk.dll", typeof(WinRTActivation).Assembly, DllImportSearchPath.AssemblyDirectory), |
| 32 | + "DllGetActivationFactory")); |
| 33 | + |
| 34 | + // Custom WinRT activation handler: |
| 35 | + // If it is one of our types, we resolve it with our native DLL. Otherwise, we defer to the previous handler if one exists. |
| 36 | + var previousHandler = ActivationFactory.ActivationHandler; |
| 37 | + ActivationFactory.ActivationHandler = (typeName, iid) => |
| 38 | + { |
| 39 | + if (typeName.StartsWith("Microsoft.WSL.Containers.", StringComparison.Ordinal)) |
| 40 | + { |
| 41 | + return GetActivationFactory(typeName, iid); |
| 42 | + } |
| 43 | + |
| 44 | + if (previousHandler != null) |
| 45 | + { |
| 46 | + return previousHandler(typeName, iid); |
| 47 | + } |
| 48 | + |
| 49 | + return IntPtr.Zero; |
| 50 | + }; |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + private static IntPtr GetActivationFactory(string typeName, Guid iid) |
| 55 | + { |
| 56 | + // Convert the type name to HSTRING |
| 57 | + WindowsCreateString(typeName, (uint)typeName.Length, out var hstring); |
| 58 | + try |
| 59 | + { |
| 60 | + if (s_getDllFactory(hstring, out var factory) < 0) |
| 61 | + { |
| 62 | + return IntPtr.Zero; |
| 63 | + } |
| 64 | + |
| 65 | + if (iid == IID_IActivationFactory) |
| 66 | + { |
| 67 | + return factory; |
| 68 | + } |
| 69 | + |
| 70 | + try |
| 71 | + { |
| 72 | + if (Marshal.QueryInterface(factory, ref iid, out var queried) >= 0) |
| 73 | + { |
| 74 | + return queried; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + return IntPtr.Zero; |
| 79 | + } |
| 80 | + } |
| 81 | + finally |
| 82 | + { |
| 83 | + Marshal.Release(factory); |
| 84 | + } |
| 85 | + } |
| 86 | + finally |
| 87 | + { |
| 88 | + WindowsDeleteString(hstring); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static readonly Guid IID_IActivationFactory = new(0x00000035, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46); |
| 93 | + |
| 94 | + [DllImport("combase.dll", CharSet = CharSet.Unicode)] |
| 95 | + private static extern int WindowsCreateString(string sourceString, uint length, out IntPtr hstring); |
| 96 | + |
| 97 | + [DllImport("combase.dll")] |
| 98 | + private static extern int WindowsDeleteString(IntPtr hstring); |
| 99 | +} |
0 commit comments