|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.IO; |
| 8 | +using System.Reflection; |
| 9 | + |
| 10 | +namespace System.Runtime.InteropServices |
| 11 | +{ |
| 12 | + [ComImport] |
| 13 | + [ComVisible(false)] |
| 14 | + [Guid("00000001-0000-0000-C000-000000000046")] |
| 15 | + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 16 | + internal interface IClassFactory |
| 17 | + { |
| 18 | + void CreateInstance( |
| 19 | + [MarshalAs(UnmanagedType.Interface)] object pUnkOuter, |
| 20 | + ref Guid riid, |
| 21 | + [MarshalAs(UnmanagedType.Interface)] out object ppvObject); |
| 22 | + |
| 23 | + void LockServer([MarshalAs(UnmanagedType.Bool)] bool fLock); |
| 24 | + } |
| 25 | + |
| 26 | + [StructLayout(LayoutKind.Sequential)] |
| 27 | + internal struct LICINFO |
| 28 | + { |
| 29 | + public int cbLicInfo; |
| 30 | + |
| 31 | + [MarshalAs(UnmanagedType.Bool)] |
| 32 | + public bool fRuntimeKeyAvail; |
| 33 | + |
| 34 | + [MarshalAs(UnmanagedType.Bool)] |
| 35 | + public bool fLicVerified; |
| 36 | + } |
| 37 | + |
| 38 | + [ComImport] |
| 39 | + [ComVisible(false)] |
| 40 | + [Guid("B196B28F-BAB4-101A-B69C-00AA00341D07")] |
| 41 | + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 42 | + internal interface IClassFactory2 : IClassFactory |
| 43 | + { |
| 44 | + new void CreateInstance( |
| 45 | + [MarshalAs(UnmanagedType.Interface)] object pUnkOuter, |
| 46 | + ref Guid riid, |
| 47 | + [MarshalAs(UnmanagedType.Interface)] out object ppvObject); |
| 48 | + |
| 49 | + new void LockServer([MarshalAs(UnmanagedType.Bool)] bool fLock); |
| 50 | + |
| 51 | + void GetLicInfo(ref LICINFO pLicInfo); |
| 52 | + |
| 53 | + void RequestLicKey( |
| 54 | + int dwReserved, |
| 55 | + [MarshalAs(UnmanagedType.BStr)] out string pBstrKey); |
| 56 | + |
| 57 | + void CreateInstanceLic( |
| 58 | + [MarshalAs(UnmanagedType.Interface)] object pUnkOuter, |
| 59 | + [MarshalAs(UnmanagedType.Interface)] object pUnkReserved, |
| 60 | + ref Guid riid, |
| 61 | + [MarshalAs(UnmanagedType.BStr)] string bstrKey, |
| 62 | + [MarshalAs(UnmanagedType.Interface)] out object ppvObject); |
| 63 | + } |
| 64 | + |
| 65 | + [StructLayout(LayoutKind.Sequential)] |
| 66 | + public struct ComActivationContext |
| 67 | + { |
| 68 | + public Guid ClassId; |
| 69 | + public Guid InterfaceId; |
| 70 | + public string AssemblyName; |
| 71 | + public string TypeName; |
| 72 | + } |
| 73 | + |
| 74 | + [StructLayout(LayoutKind.Sequential)] |
| 75 | + public struct ComActivationContextInternal |
| 76 | + { |
| 77 | + public Guid ClassId; |
| 78 | + public Guid InterfaceId; |
| 79 | + public IntPtr AssemblyNameBuffer; |
| 80 | + public IntPtr TypeNameBuffer; |
| 81 | + public IntPtr ClassFactoryDest; |
| 82 | + } |
| 83 | + |
| 84 | + public static class ComActivator |
| 85 | + { |
| 86 | + /// <summary> |
| 87 | + /// Entry point for unmanaged COM activation API from managed code |
| 88 | + /// </summary> |
| 89 | + /// <param name="cxt">Reference to a <see cref="ComActivationContext"/> instance</param> |
| 90 | + public static object GetClassFactoryForType(ComActivationContext cxt) |
| 91 | + { |
| 92 | + if (cxt.InterfaceId != typeof(IClassFactory).GUID |
| 93 | + && cxt.InterfaceId != typeof(IClassFactory2).GUID) |
| 94 | + { |
| 95 | + throw new NotSupportedException(); |
| 96 | + } |
| 97 | + |
| 98 | + Type classType = FindClassType(cxt.ClassId, cxt.AssemblyName, cxt.TypeName); |
| 99 | + return new BasicClassFactory(cxt.ClassId, classType); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Internal entry point for unmanaged COM activation API from native code |
| 104 | + /// </summary> |
| 105 | + /// <param name="cxtInt">Reference to a <see cref="ComActivationContextInternal"/> instance</param> |
| 106 | + public static int GetClassFactoryForTypeInternal(ref ComActivationContextInternal cxtInt) |
| 107 | + { |
| 108 | + if (IsLoggingEnabled()) |
| 109 | + { |
| 110 | + Log( |
| 111 | +$@"{nameof(GetClassFactoryForTypeInternal)} arguments: |
| 112 | + {cxtInt.ClassId} |
| 113 | + {cxtInt.InterfaceId} |
| 114 | + 0x{cxtInt.AssemblyNameBuffer.ToInt64():x} |
| 115 | + 0x{cxtInt.TypeNameBuffer.ToInt64():x} |
| 116 | + 0x{cxtInt.ClassFactoryDest.ToInt64():x}"); |
| 117 | + } |
| 118 | + |
| 119 | + try |
| 120 | + { |
| 121 | + var cxt = new ComActivationContext() |
| 122 | + { |
| 123 | + ClassId = cxtInt.ClassId, |
| 124 | + InterfaceId = cxtInt.InterfaceId, |
| 125 | + AssemblyName = Marshal.PtrToStringUTF8(cxtInt.AssemblyNameBuffer), |
| 126 | + TypeName = Marshal.PtrToStringUTF8(cxtInt.TypeNameBuffer) |
| 127 | + }; |
| 128 | + |
| 129 | + object cf = GetClassFactoryForType(cxt); |
| 130 | + IntPtr nativeIUnknown = Marshal.GetIUnknownForObject(cf); |
| 131 | + Marshal.WriteIntPtr(cxtInt.ClassFactoryDest, nativeIUnknown); |
| 132 | + } |
| 133 | + catch (Exception e) |
| 134 | + { |
| 135 | + return e.HResult; |
| 136 | + } |
| 137 | + |
| 138 | + return 0; |
| 139 | + } |
| 140 | + |
| 141 | + private static bool IsLoggingEnabled() |
| 142 | + { |
| 143 | +#if COM_ACTIVATOR_DEBUG |
| 144 | + return true; |
| 145 | +#else |
| 146 | + return false; |
| 147 | +#endif |
| 148 | + } |
| 149 | + |
| 150 | + private static void Log(string fmt, params object[] args) |
| 151 | + { |
| 152 | + // [TODO] Use FrameworkEventSource in release builds |
| 153 | + |
| 154 | + Debug.WriteLine(fmt, args); |
| 155 | + } |
| 156 | + |
| 157 | + private static Type FindClassType(Guid clsid, string assemblyName, string typeName) |
| 158 | + { |
| 159 | + try |
| 160 | + { |
| 161 | + Assembly assem = Assembly.LoadFrom(assemblyName); |
| 162 | + Type t = assem.GetType(typeName); |
| 163 | + if (t != null) |
| 164 | + { |
| 165 | + return t; |
| 166 | + } |
| 167 | + } |
| 168 | + catch (Exception e) |
| 169 | + { |
| 170 | + if (IsLoggingEnabled()) |
| 171 | + { |
| 172 | + Log($"COM Activation of {clsid} failed. {e}"); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + const int CLASS_E_CLASSNOTAVAILABLE = unchecked((int)0x80040111); |
| 177 | + throw new COMException(string.Empty, CLASS_E_CLASSNOTAVAILABLE); |
| 178 | + } |
| 179 | + |
| 180 | + [ComVisible(true)] |
| 181 | + internal class BasicClassFactory : IClassFactory2 |
| 182 | + { |
| 183 | + private readonly Guid classId; |
| 184 | + private readonly Type classType; |
| 185 | + |
| 186 | + public BasicClassFactory(Guid clsid, Type classType) |
| 187 | + { |
| 188 | + this.classId = clsid; |
| 189 | + this.classType = classType; |
| 190 | + } |
| 191 | + |
| 192 | + public void CreateInstance( |
| 193 | + [MarshalAs(UnmanagedType.Interface)] object pUnkOuter, |
| 194 | + ref Guid riid, |
| 195 | + [MarshalAs(UnmanagedType.Interface)] out object ppvObject) |
| 196 | + { |
| 197 | + if (riid != Marshal.IID_IUnknown) |
| 198 | + { |
| 199 | + bool found = false; |
| 200 | + |
| 201 | + // Verify the class implements the desired interface |
| 202 | + foreach (Type i in this.classType.GetInterfaces()) |
| 203 | + { |
| 204 | + if (i.GUID == riid) |
| 205 | + { |
| 206 | + found = true; |
| 207 | + break; |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + if (!found) |
| 212 | + { |
| 213 | + // E_NOINTERFACE |
| 214 | + throw new InvalidCastException(); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + ppvObject = Activator.CreateInstance(this.classType); |
| 219 | + if (pUnkOuter != null) |
| 220 | + { |
| 221 | + try |
| 222 | + { |
| 223 | + IntPtr outerPtr = Marshal.GetIUnknownForObject(pUnkOuter); |
| 224 | + IntPtr innerPtr = Marshal.CreateAggregatedObject(outerPtr, ppvObject); |
| 225 | + ppvObject = Marshal.GetObjectForIUnknown(innerPtr); |
| 226 | + } |
| 227 | + finally |
| 228 | + { |
| 229 | + // Decrement the above 'Marshal.GetIUnknownForObject()' |
| 230 | + Marshal.ReleaseComObject(pUnkOuter); |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + public void LockServer([MarshalAs(UnmanagedType.Bool)] bool fLock) |
| 236 | + { |
| 237 | + // nop |
| 238 | + } |
| 239 | + |
| 240 | + public void GetLicInfo(ref LICINFO pLicInfo) |
| 241 | + { |
| 242 | + throw new NotImplementedException(); |
| 243 | + } |
| 244 | + |
| 245 | + public void RequestLicKey(int dwReserved, [MarshalAs(UnmanagedType.BStr)] out string pBstrKey) |
| 246 | + { |
| 247 | + throw new NotImplementedException(); |
| 248 | + } |
| 249 | + |
| 250 | + public void CreateInstanceLic( |
| 251 | + [MarshalAs(UnmanagedType.Interface)] object pUnkOuter, |
| 252 | + [MarshalAs(UnmanagedType.Interface)] object pUnkReserved, |
| 253 | + ref Guid riid, |
| 254 | + [MarshalAs(UnmanagedType.BStr)] string bstrKey, |
| 255 | + [MarshalAs(UnmanagedType.Interface)] out object ppvObject) |
| 256 | + { |
| 257 | + throw new NotImplementedException(); |
| 258 | + } |
| 259 | + } |
| 260 | + } |
| 261 | +} |
0 commit comments