using System; using System.Runtime.InteropServices; using System.Security; namespace Vanara.PInvoke { public static partial class Shell32 { /// /// Exposes methods that enable clients to access items in a collection of objects that support IUnknown. /// /// /// When to Implement /// Clients do not need to implement this interface. /// When to Use /// Use this interface to access generic objects in an array. /// // https://docs.microsoft.com/en-us/windows/desktop/api/objectarray/nn-objectarray-iobjectarray [PInvokeData("objectarray.h", MSDNShortId = "ab0bb213-dc9c-4853-98d7-668e7ca76583")] [SuppressUnmanagedCodeSecurity] [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("92CA9DCD-5622-4bba-A805-5E9F541BD8C9")] public interface IObjectArray { /// Provides a count of the objects in the collection. /// The number of objects in the collection. uint GetCount(); /// /// Provides a pointer to a specified object's interface. The object and interface are specified by index and interface ID. /// /// The index of the object /// Reference to the desired interface ID. /// Receives the interface pointer requested in riid. [return: MarshalAs(UnmanagedType.IUnknown)] object GetAt([In] uint uiIndex, in Guid riid); } /// /// /// Extends the IObjectArray interface by providing methods that enable clients to add and remove objects that support IUnknown in a collection. /// /// /// /// When to Use /// Use this interface to interact with a collection of generic objects. /// // https://docs.microsoft.com/en-us/windows/desktop/api/objectarray/nn-objectarray-iobjectcollection [PInvokeData("objectarray.h", MSDNShortId = "d7665b26-5839-4b08-a099-ef25a68c65db")] [SuppressUnmanagedCodeSecurity] [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("5632b1a4-e38a-400a-928a-d4cd63230295"), CoClass(typeof(CEnumerableObjectCollection))] public interface IObjectCollection : IObjectArray { /// Provides a count of the objects in the collection. /// The number of objects in the collection. new uint GetCount(); /// /// Provides a pointer to a specified object's interface. The object and interface are specified by index and interface ID. /// /// The index of the object /// Reference to the desired interface ID. /// Receives the interface pointer requested in riid. [return: MarshalAs(UnmanagedType.IUnknown)] new object GetAt([In] uint uiIndex, in Guid riid); /// Adds a single object to the collection. /// Pointer to the IUnknown of the object to be added to the collection. void AddObject([In, MarshalAs(UnmanagedType.IUnknown)] object punk); /// Adds the objects contained in an IObjectArray to the collection. /// Pointer to the IObjectArray whose contents are to be added to the collection. void AddFromArray(IObjectArray poaSource); /// Removes a single, specified object from the collection. /// A pointer to the index of the object within the collection. void RemoveObjectAt(uint uiIndex); /// Removes all objects from the collection. void Clear(); } /// Extension method to simplify using the method. /// Type of the interface to get. /// An instance. /// The index of the object /// Receives the interface pointer requested in . public static T GetAt(this IObjectArray a, uint uiIndex) where T : class => (T)a.GetAt(uiIndex, typeof(T).GUID); /// Extension method to convert an instance to an array of . /// Type of the interface to get. Supplying a type will get the IUnknown reference. /// An instance. /// An array of . public static T[] ToArray(this IObjectArray a) where T : class { const string IID_IUnknown = "00000000-0000-0000-C000-000000000046"; var gIUnk = typeof(T) == typeof(object) ? new Guid(IID_IUnknown) : typeof(T).GUID; var c = a.GetCount(); var ret = new T[c]; for (var i = 0U; i < c; i++) ret[i] = (T)a.GetAt(i, gIUnk); return ret; } } }