///<summary> /// Query Interface for a particular interface support and attach to the given instance. ///</summary> ///<typeparam name="T"></typeparam> ///<returns></returns> protected void QueryInterfaceFrom <T>(T fromObject) where T : ComObject { IntPtr parentPtr; fromObject.QueryInterface(Utilities.GetGuidFromType(this.GetType()), out parentPtr); NativePointer = parentPtr; }
///<summary> /// Query this instance for a particular COM interface support. ///</summary> ///<typeparam name="T">The type of the COM interface to query</typeparam> ///<returns>An instance of the queried interface</returns> /// <exception cref="SharpDXException">If this object doesn't support the interface</exception> /// <msdn-id>ms682521</msdn-id> /// <unmanaged>IUnknown::QueryInterface</unmanaged> /// <unmanaged-short>IUnknown::QueryInterface</unmanaged-short> internal virtual T QueryInterfaceUnsafe <T>() { IntPtr parentPtr; this.QueryInterface(Utilities.GetGuidFromType(typeof(T)), out parentPtr); return(FromPointerUnsafe <T>(parentPtr)); }
///<summary> /// Query this instance for a particular COM interface support. ///</summary> ///<typeparam name="T">The type of the COM interface to query</typeparam> ///<returns>An instance of the queried interface</returns> /// <exception cref="SharpDXException">If this object doesn't support the interface</exception> /// <msdn-id>ms682521</msdn-id> /// <unmanaged>IUnknown::QueryInterface</unmanaged> /// <unmanaged-short>IUnknown::QueryInterface</unmanaged-short> public virtual T QueryInterface <T>() where T : ComObject { IntPtr parentPtr; this.QueryInterface(Utilities.GetGuidFromType(typeof(T)), out parentPtr); return(FromPointer <T>(parentPtr)); }
internal virtual T QueryInterfaceUnsafe <T>() { IntPtr outPtr; this.QueryInterface(Utilities.GetGuidFromType(typeof(T)), out outPtr); return(CppObject.FromPointerUnsafe <T>(outPtr)); }
/// <summary> /// Queries a managed object for a particular COM interface support. /// </summary> ///<typeparam name="T">The type of the COM interface to query</typeparam> /// <param name="comPointer">A pointer to a COM object.</param> ///<returns>An instance of the queried interface</returns> /// <msdn-id>ms682521</msdn-id> /// <unmanaged>IUnknown::QueryInterface</unmanaged> /// <unmanaged-short>IUnknown::QueryInterface</unmanaged-short> public static T QueryInterfaceOrNull <T>(IntPtr comPointer) where T : ComObject { if (comPointer == IntPtr.Zero) { return(null); } var guid = Utilities.GetGuidFromType(typeof(T)); IntPtr pointerT; var result = (Result)Marshal.QueryInterface(comPointer, ref guid, out pointerT); return((result.Failure) ? null : FromPointerUnsafe <T>(pointerT)); }
public void Initialize(ICallbackable callbackable) { callbackable.Shadow = (IDisposable)this; Type type1 = callbackable.GetType(); List <Type> list; lock (ShadowContainer.typeToShadowTypes) { if (!ShadowContainer.typeToShadowTypes.TryGetValue(type1, out list)) { Type[] local_2 = type1.GetInterfaces(); list = new List <Type>(); list.AddRange((IEnumerable <Type>)local_2); ShadowContainer.typeToShadowTypes.Add(type1, list); foreach (Type item_1 in local_2) { if (ShadowAttribute.Get(item_1) == null) { list.Remove(item_1); } else { foreach (Type item_0 in item_1.GetInterfaces()) { list.Remove(item_0); } } } } } CppObjectShadow cppObjectShadow1 = (CppObjectShadow)null; foreach (Type type2 in list) { CppObjectShadow cppObjectShadow2 = (CppObjectShadow)Activator.CreateInstance(ShadowAttribute.Get(type2).Type); cppObjectShadow2.Initialize(callbackable); if (cppObjectShadow1 == null) { cppObjectShadow1 = cppObjectShadow2; this.guidToShadow.Add(ComObjectShadow.IID_IUnknown, cppObjectShadow1); } this.guidToShadow.Add(Utilities.GetGuidFromType(type2), cppObjectShadow2); foreach (Type type3 in type2.GetInterfaces()) { if (ShadowAttribute.Get(type3) != null) { this.guidToShadow.Add(Utilities.GetGuidFromType(type3), cppObjectShadow2); } } } }
public void Initialize(ICallbackable callbackable) { callbackable.Shadow = this; var type = callbackable.GetType(); List <Type> slimInterfaces; // Cache reflection on COM interface inheritance lock (typeToShadowTypes) { if (!typeToShadowTypes.TryGetValue(type, out slimInterfaces)) { #if BEFORE_NET45 var interfaces = type.GetTypeInfo().GetInterfaces(); #else var interfaces = type.GetTypeInfo().ImplementedInterfaces; #endif slimInterfaces = new List <Type>(); slimInterfaces.AddRange(interfaces); typeToShadowTypes.Add(type, slimInterfaces); // First pass to identify most detailed interfaces foreach (var item in interfaces) { // Only process interfaces that are using shadow var shadowAttribute = ShadowAttribute.Get(item); if (shadowAttribute == null) { slimInterfaces.Remove(item); continue; } // Keep only final interfaces and not intermediate. #if BEFORE_NET45 var inheritList = item.GetTypeInfo().GetInterfaces(); #else var inheritList = item.GetTypeInfo().ImplementedInterfaces; #endif foreach (var inheritInterface in inheritList) { slimInterfaces.Remove(inheritInterface); } } } } CppObjectShadow iunknownShadow = null; // Second pass to instantiate shadow foreach (var item in slimInterfaces) { // Only process interfaces that are using shadow var shadowAttribute = ShadowAttribute.Get(item); // Initialize the shadow with the callback var shadow = (CppObjectShadow)Activator.CreateInstance(shadowAttribute.Type); shadow.Initialize(callbackable); // Take the first shadow as the main IUnknown if (iunknownShadow == null) { iunknownShadow = shadow; // Add IUnknown as a supported interface guidToShadow.Add(ComObjectShadow.IID_IUnknown, iunknownShadow); } guidToShadow.Add(Utilities.GetGuidFromType(item), shadow); // Associate also inherited interface to this shadow #if BEFORE_NET45 var inheritList = item.GetTypeInfo().GetInterfaces(); #else var inheritList = item.GetTypeInfo().ImplementedInterfaces; #endif foreach (var inheritInterface in inheritList) { var inheritShadowAttribute = ShadowAttribute.Get(inheritInterface); if (inheritShadowAttribute == null) { continue; } // Use same shadow as derived guidToShadow.Add(Utilities.GetGuidFromType(inheritInterface), shadow); } } // Precalculate the list of GUID without IUnknown and IInspectable // Used for WinRT int countGuids = 0; foreach (var guidKey in guidToShadow.Keys) { if (guidKey != Utilities.GetGuidFromType(typeof(IInspectable)) && guidKey != Utilities.GetGuidFromType(typeof(IUnknown))) { countGuids++; } } guidPtr = Marshal.AllocHGlobal(Utilities.SizeOf <Guid>() * countGuids); Guids = new IntPtr[countGuids]; int i = 0; unsafe { var pGuid = (Guid *)guidPtr; foreach (var guidKey in guidToShadow.Keys) { if (guidKey == Utilities.GetGuidFromType(typeof(IInspectable)) || guidKey == Utilities.GetGuidFromType(typeof(IUnknown))) { continue; } pGuid[i] = guidKey; // Store the pointer Guids[i] = new IntPtr(pGuid + i); i++; } } }
internal IntPtr Find(Type type) { return(Find(Utilities.GetGuidFromType(type))); }
///<summary> /// Query Interface for a particular interface support. ///</summary> ///<returns>An instance of the queried interface or null if it is not supported</returns> ///<returns></returns> /// <msdn-id>ms682521</msdn-id> /// <unmanaged>IUnknown::QueryInterface</unmanaged> /// <unmanaged-short>IUnknown::QueryInterface</unmanaged-short> public virtual T QueryInterfaceOrNull <T>() where T : ComObject { return(FromPointer <T>(QueryInterfaceOrNull(Utilities.GetGuidFromType(typeof(T))))); }