public override unsafe int TryAs <U>(Guid iid, out ObjectReference <U> objRef)
        {
            objRef = null;
            int hr = VftblIUnknown.QueryInterface(ThisPtr, ref iid, out IntPtr thatPtr);

            if (hr >= 0)
            {
                if (IsAggregated)
                {
                    Marshal.Release(thatPtr);
                }
                AddRefFromTrackerSource();

                using (var contextCallbackReference = ObjectReference <ABI.WinRT.Interop.IContextCallback.Vftbl> .FromAbi(_contextCallbackPtr))
                {
                    objRef = new ObjectReferenceWithContext <U>(thatPtr, contextCallbackReference.GetRef())
                    {
                        IsAggregated            = IsAggregated,
                        PreventReleaseOnDispose = IsAggregated,
                        ReferenceTrackerPtr     = ReferenceTrackerPtr
                    };
                }
            }
            return(hr);
        }
Exemple #2
0
        public virtual unsafe int TryAs <T>(Guid iid, out ObjectReference <T> objRef)
        {
            objRef = null;
            ThrowIfDisposed();
            int hr = VftblIUnknown.QueryInterface(ThisPtr, ref iid, out IntPtr thatPtr);

            if (hr >= 0)
            {
                objRef = ObjectReference <T> .Attach(ref thatPtr);
            }
            return(hr);
        }
Exemple #3
0
        // Used only as part of the GetInterface implementation where the
        // result is an reference passed across the ABI and doesn't need to
        // be tracked as an internal reference.  This is separate to handle
        // tear off aggregate scenario where releasing an reference can end up
        // deleting the tear off interface.
        public virtual unsafe int TryAs(Guid iid, out IntPtr ppv)
        {
            ppv = IntPtr.Zero;
            ThrowIfDisposed();
            int hr = VftblIUnknown.QueryInterface(ThisPtr, ref iid, out IntPtr thatPtr);

            if (hr >= 0)
            {
                ppv = thatPtr;
            }
            return(hr);
        }
        public override unsafe int TryAs <U>(Guid iid, out ObjectReference <U> objRef)
        {
            objRef = null;
            int hr = VftblIUnknown.QueryInterface(ThisPtr, ref iid, out IntPtr thatPtr);

            if (hr >= 0)
            {
                using (var contextCallbackReference = ObjectReference <ABI.WinRT.Interop.IContextCallback.Vftbl> .FromAbi(_contextCallbackPtr))
                {
                    objRef = new ObjectReferenceWithContext <U>(thatPtr, contextCallbackReference.GetRef());
                }
            }
            return(hr);
        }
Exemple #5
0
        public virtual unsafe int TryAs <T>(Guid iid, out ObjectReference <T> objRef)
        {
            objRef = null;
            ThrowIfDisposed();
            int hr = VftblIUnknown.QueryInterface(ThisPtr, ref iid, out IntPtr thatPtr);

            if (hr >= 0)
            {
                if (IsAggregated)
                {
                    Marshal.Release(thatPtr);
                }
                AddRefFromTrackerSource();

                objRef = ObjectReference <T> .Attach(ref thatPtr);

                objRef.IsAggregated            = IsAggregated;
                objRef.PreventReleaseOnDispose = IsAggregated;
                objRef.ReferenceTrackerPtr     = ReferenceTrackerPtr;
            }
            return(hr);
        }
Exemple #6
0
        public override unsafe int TryAs <U>(Guid iid, out ObjectReference <U> objRef)
        {
            objRef = null;

            int hr = VftblIUnknown.QueryInterface(ThisPtr, ref iid, out IntPtr thatPtr);

            if (hr >= 0)
            {
                if (IsAggregated)
                {
                    Marshal.Release(thatPtr);
                }
                AddRefFromTrackerSource();

                objRef = new ObjectReferenceWithContext <U>(thatPtr, Context.GetContextCallback(), Context.GetContextToken(), _agileReference, iid)
                {
                    IsAggregated            = IsAggregated,
                    PreventReleaseOnDispose = IsAggregated,
                    ReferenceTrackerPtr     = ReferenceTrackerPtr
                };
            }

            return(hr);
        }
Exemple #7
0
        public unsafe TInterface AsInterface <TInterface>()
        {
            if (typeof(TInterface).GetCustomAttribute(typeof(System.Runtime.InteropServices.ComImportAttribute)) is object)
            {
                Guid iid = typeof(TInterface).GUID;
                Marshal.ThrowExceptionForHR(VftblIUnknown.QueryInterface(ThisPtr, ref iid, out IntPtr comPtr));
                try
                {
                    return((TInterface)Marshal.GetObjectForIUnknown(comPtr));
                }
                finally
                {
                    var vftblPtr      = Unsafe.AsRef <WinRT.VftblPtr>(comPtr.ToPointer());
                    var vftblIUnknown = Marshal.PtrToStructure <WinRT.Interop.IUnknownVftbl>(vftblPtr.Vftbl);
                    vftblIUnknown.Release(comPtr);
                }
            }

#if NETSTANDARD2_0
            return((TInterface)typeof(TInterface).GetHelperType().GetConstructor(new[] { typeof(IObjectReference) }).Invoke(new object[] { this }));
#else
            return((TInterface)(object)new WinRT.IInspectable(this));
#endif
        }
Exemple #8
0
 public ObjectReference <T> As <T>(Guid iid)
 {
     ThrowIfDisposed();
     Marshal.ThrowExceptionForHR(VftblIUnknown.QueryInterface(ThisPtr, ref iid, out IntPtr thatPtr));
     return(ObjectReference <T> .Attach(ref thatPtr));
 }