diff --git a/src/cswinrt/strings/WinRT.cs b/src/cswinrt/strings/WinRT.cs index 7d6a134bb..85a486b58 100644 --- a/src/cswinrt/strings/WinRT.cs +++ b/src/cswinrt/strings/WinRT.cs @@ -488,7 +488,18 @@ internal sealed class FactoryObjectReference< { private readonly IntPtr _contextToken; - internal FactoryObjectReference(IntPtr thisPtr) : + public static FactoryObjectReference Attach(ref IntPtr thisPtr) + { + if (thisPtr == IntPtr.Zero) + { + return null; + } + var obj = new FactoryObjectReference(thisPtr); + thisPtr = IntPtr.Zero; + return obj; + } + + internal FactoryObjectReference(IntPtr thisPtr) : base(thisPtr) { if (!IsFreeThreaded(this)) @@ -626,14 +637,16 @@ public static ObjectReference Get< if (activationFactory != null) { using (activationFactory) -#if NET - using (var objRef = activationFactory.As(iid)) { - return FactoryObjectReference.FromAbi(objRef.ThisPtr); - } +#if NET + if (activationFactory.TryAs(iid, out IntPtr iidPtr) >= 0) + { + return FactoryObjectReference.Attach(ref iidPtr); + } #else - return activationFactory.As(iid); + return activationFactory.As(iid); #endif + } } } }