diff --git a/src/WinRT.Runtime/ActivationFactory.cs b/src/WinRT.Runtime/ActivationFactory.cs index f923f360b..6ffa0026f 100644 --- a/src/WinRT.Runtime/ActivationFactory.cs +++ b/src/WinRT.Runtime/ActivationFactory.cs @@ -12,14 +12,14 @@ namespace WinRT { internal unsafe sealed class DllModule { - readonly string _fileName; - readonly IntPtr _moduleHandle; - readonly delegate* unmanaged[Stdcall] _GetActivationFactory; - readonly delegate* unmanaged[Stdcall] _CanUnloadNow; // TODO: Eventually periodically call + private readonly string _fileName; + private readonly IntPtr _moduleHandle; + private readonly delegate* unmanaged[Stdcall] _GetActivationFactory; + private readonly delegate* unmanaged[Stdcall] _CanUnloadNow; // TODO: Eventually periodically call - static readonly string _currentModuleDirectory = AppContext.BaseDirectory; + private static readonly string _currentModuleDirectory = AppContext.BaseDirectory; - static Dictionary _cache = new System.Collections.Generic.Dictionary(StringComparer.Ordinal); + private static Dictionary _cache = new Dictionary(StringComparer.Ordinal); public static bool TryLoad(string fileName, out DllModule module) { @@ -38,7 +38,7 @@ public static bool TryLoad(string fileName, out DllModule module) } } - private static unsafe bool TryCreate(string fileName, out DllModule module) + 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. @@ -97,7 +97,7 @@ private DllModule(string fileName, IntPtr moduleHandle, void* getActivationFacto } } - public unsafe (ObjectReference obj, int hr) GetActivationFactory(string runtimeClassId) + public (ObjectReference obj, int hr) GetActivationFactory(string runtimeClassId) { IntPtr instancePtr = IntPtr.Zero; try @@ -137,18 +137,18 @@ public unsafe (ObjectReference obj, int hr) GetActivati } } - internal sealed class WinrtModule + internal sealed class WinRTModule { - readonly IntPtr _mtaCookie; - volatile static WinrtModule _instance; - private static WinrtModule MakeWinRTModule() + private static volatile WinRTModule _instance; + private readonly IntPtr _mtaCookie; + private static WinRTModule MakeWinRTModule() { - global::System.Threading.Interlocked.CompareExchange(ref _instance, new WinrtModule(), null); + global::System.Threading.Interlocked.CompareExchange(ref _instance, new WinRTModule(), null); return _instance; } - public static WinrtModule Instance => _instance ?? MakeWinRTModule(); + public static WinRTModule Instance => _instance ?? MakeWinRTModule(); - public unsafe WinrtModule() + public unsafe WinRTModule() { IntPtr mtaCookie; Marshal.ThrowExceptionForHR(Platform.CoIncrementMTAUsage(&mtaCookie)); @@ -182,25 +182,37 @@ public static unsafe (ObjectReference obj, int hr) GetActivationFactory(st } } - ~WinrtModule() + ~WinRTModule() { Marshal.ThrowExceptionForHR(Platform.CoDecrementMTAUsage(_mtaCookie)); } } -#if EMBED - internal -#else - public -#endif - static class ActivationFactory + internal static class IActivationFactoryMethods + { + public static unsafe ObjectReference ActivateInstance(IObjectReference obj) + { + IntPtr instancePtr; + global::WinRT.ExceptionHelpers.ThrowExceptionForHR((*(delegate* unmanaged[Stdcall]**)obj.ThisPtr)[6](obj.ThisPtr, &instancePtr)); + try + { + return ComWrappersSupport.GetObjectReferenceForInterface(instancePtr); + } + finally + { + MarshalInspectable.DisposeAbi(instancePtr); + } + } + } + + internal static class ActivationFactory { - public static IObjectReference Get(string typeName) + public static ObjectReference Get(string typeName) { // Prefer the RoGetActivationFactory HRESULT failure over the LoadLibrary/etc. failure int hr; ObjectReference factory; - (factory, hr) = WinrtModule.GetActivationFactory(typeName, InterfaceIIDs.IActivationFactory_IID); + (factory, hr) = WinRTModule.GetActivationFactory(typeName, InterfaceIIDs.IActivationFactory_IID); if (factory != null) { return factory; @@ -237,7 +249,7 @@ public static ObjectReference Get< // Prefer the RoGetActivationFactory HRESULT failure over the LoadLibrary/etc. failure int hr; ObjectReference factory; - (factory, hr) = WinrtModule.GetActivationFactory(typeName, iid); + (factory, hr) = WinRTModule.GetActivationFactory(typeName, iid); if (factory != null) { return factory; diff --git a/src/WinRT.Runtime/Context.cs b/src/WinRT.Runtime/Context.cs index 5a5d39eed..3c7c8d650 100644 --- a/src/WinRT.Runtime/Context.cs +++ b/src/WinRT.Runtime/Context.cs @@ -7,7 +7,7 @@ namespace WinRT { - static partial class Context + internal static partial class Context { [DllImport("api-ms-win-core-com-l1-1-0.dll")] private static extern unsafe int CoGetContextToken(IntPtr* contextToken);