From fd35f79e22c3e905f314f797af89dc540953ca01 Mon Sep 17 00:00:00 2001 From: dahall Date: Sat, 6 Jun 2020 13:44:00 -0600 Subject: [PATCH] Removed IComEnumerator references - doesn't work due to Reflection/COM object limitations --- PInvoke/Ole/Ole32/ComCat.cs | 4 +- PInvoke/Ole/Ole32/ObjIdl.cs | 4 +- PInvoke/Ole/Ole32/OleIdl.cs | 2 +- PInvoke/Ole/Ole32/PropIdl.cs | 4 +- PInvoke/Ole/OleAut32/Wia_xp.cs | 4 +- PInvoke/Shared/Collections/ComEnumerator.cs | 89 ----------------------------- 6 files changed, 9 insertions(+), 98 deletions(-) delete mode 100644 PInvoke/Shared/Collections/ComEnumerator.cs diff --git a/PInvoke/Ole/Ole32/ComCat.cs b/PInvoke/Ole/Ole32/ComCat.cs index 977511d3..22a0b810 100644 --- a/PInvoke/Ole/Ole32/ComCat.cs +++ b/PInvoke/Ole/Ole32/ComCat.cs @@ -199,7 +199,7 @@ namespace Vanara.PInvoke /// Enumerates component categories registered in the system. // https://docs.microsoft.com/en-us/windows/win32/api/comcat/nn-comcat-ienumcategoryinfo [ComImport, Guid("0002E011-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - public interface IEnumCATEGORYINFO : IComEnumerator + public interface IEnumCATEGORYINFO { /// Retrieves the specified number of items in the enumeration sequence. /// @@ -256,7 +256,7 @@ namespace Vanara.PInvoke // https://docs.microsoft.com/en-us/windows/win32/api/comcat/nn-comcat-ienumguid [PInvokeData("comcat.h", MSDNShortId = "NN:comcat.IEnumGUID")] [ComImport, Guid("0002E000-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - public interface IEnumGUID : IComEnumerator + public interface IEnumGUID { /// Retrieves the specified number of items in the enumeration sequence. /// diff --git a/PInvoke/Ole/Ole32/ObjIdl.cs b/PInvoke/Ole/Ole32/ObjIdl.cs index edbdd667..e02ae6c9 100644 --- a/PInvoke/Ole/Ole32/ObjIdl.cs +++ b/PInvoke/Ole/Ole32/ObjIdl.cs @@ -1085,7 +1085,7 @@ namespace Vanara.PInvoke // https://docs.microsoft.com/en-us/windows/desktop/api/objidl/nn-objidl-ienumcontextprops [PInvokeData("objidl.h", MSDNShortId = "64591e45-5478-4360-8c1f-08b09b5aef8e")] [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000001c1-0000-0000-C000-000000000046")] - public interface IEnumContextProps : IComEnumerator + public interface IEnumContextProps { /// Retrieves the specified number of items in the enumeration sequence. /// @@ -1156,7 +1156,7 @@ namespace Vanara.PInvoke /// [ComImport, Guid("0000000D-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] [PInvokeData("Objidl.h", MSDNShortId = "aa379217")] - public interface IEnumSTATSTG : IComEnumerator + public interface IEnumSTATSTG { /// /// The Next method retrieves a specified number of STATSTG structures, that follow in the enumeration sequence. If there are diff --git a/PInvoke/Ole/Ole32/OleIdl.cs b/PInvoke/Ole/Ole32/OleIdl.cs index a5ad0c34..bba9688c 100644 --- a/PInvoke/Ole/Ole32/OleIdl.cs +++ b/PInvoke/Ole/Ole32/OleIdl.cs @@ -894,7 +894,7 @@ namespace Vanara.PInvoke // https://docs.microsoft.com/en-us/windows/win32/api/oleidl/nn-oleidl-ienumoleverb [PInvokeData("oleidl.h", MSDNShortId = "fc9b3474-6f56-4274-af7d-72e0920c0457")] [ComImport, Guid("00000104-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - public interface IEnumOLEVERB : IComEnumerator + public interface IEnumOLEVERB { /// Retrieves the specified number of items in the enumeration sequence. /// diff --git a/PInvoke/Ole/Ole32/PropIdl.cs b/PInvoke/Ole/Ole32/PropIdl.cs index 9fcf182e..80c6bda8 100644 --- a/PInvoke/Ole/Ole32/PropIdl.cs +++ b/PInvoke/Ole/Ole32/PropIdl.cs @@ -115,7 +115,7 @@ namespace Vanara.PInvoke // https://docs.microsoft.com/en-us/windows/desktop/api/propidl/nn-propidl-ienumstatpropsetstg [PInvokeData("propidl.h", MSDNShortId = "0000013B-0000-0000-C000-000000000046")] [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("0e6d4d92-6738-11cf-9608-00aa00680db4")] - public interface IEnumSTATPROPSETSTG : IComEnumerator + public interface IEnumSTATPROPSETSTG { /// /// The Next method retrieves a specified number of STATPROPSETSTG structures that follow subsequently in the enumeration @@ -201,7 +201,7 @@ namespace Vanara.PInvoke // https://docs.microsoft.com/en-us/windows/desktop/api/propidlbase/nn-propidlbase-ienumstatpropstg [PInvokeData("propidlbase.h", MSDNShortId = "e625e52a-5628-4d18-9282-aa1c141c83af")] [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000139-0000-0000-C000-000000000046")] - public interface IEnumSTATPROPSTG : IComEnumerator + public interface IEnumSTATPROPSTG { /// /// The Next method retrieves a specified number of STATPROPSTG structures, that follow subsequently in the enumeration diff --git a/PInvoke/Ole/OleAut32/Wia_xp.cs b/PInvoke/Ole/OleAut32/Wia_xp.cs index 131b7ffc..9d000e7d 100644 --- a/PInvoke/Ole/OleAut32/Wia_xp.cs +++ b/PInvoke/Ole/OleAut32/Wia_xp.cs @@ -1225,7 +1225,7 @@ namespace Vanara.PInvoke // https://docs.microsoft.com/en-us/windows/win32/api/wia_xp/nn-wia_xp-ienumwia_dev_caps [PInvokeData("wia_xp.h")] [ComImport, Guid("1fcc4287-aca6-11d2-a093-00c04f72dc3c"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - public interface IEnumWIA_DEV_CAPS : IComEnumerator + public interface IEnumWIA_DEV_CAPS { /// The IEnumWIA_DEV_CAPS::Next method fills an array of pointers to WIA_DEV_CAP structures. /// Specifies the number of array elements in the array indicated by the rgelt parameter. @@ -1380,7 +1380,7 @@ namespace Vanara.PInvoke // https://docs.microsoft.com/en-us/windows/win32/api/wia_xp/nn-wia_xp-ienumwia_format_info [PInvokeData("wia_xp.h")] [ComImport, Guid("81BEFC5B-656D-44f1-B24C-D41D51B4DC81"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - public interface IEnumWIA_FORMAT_INFO : IComEnumerator + public interface IEnumWIA_FORMAT_INFO { /// The IEnumWIA_FORMAT_INFO::Next method returns an array of WIA_FORMAT_INFO structures. /// Specifies the number of elements requested. diff --git a/PInvoke/Shared/Collections/ComEnumerator.cs b/PInvoke/Shared/Collections/ComEnumerator.cs deleted file mode 100644 index d6a157ef..00000000 --- a/PInvoke/Shared/Collections/ComEnumerator.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Runtime.InteropServices; - -namespace Vanara.PInvoke -{ - /// An interface matching most COM enumerator interfaces. This should be specified as a base for any IEnumXX interfaces. - /// The type of the value enumeratored by the Next function. - public interface IComEnumerator - { - } - - /// Extension methods to get generic enumerations from COM interfaces. - public static class ComEnumeratorExtentions - { - /// Gets an instance from an interface or class deriving from . - /// The type of the enumerated value. - /// The instance of an interface or class deriving from . - /// An instance that will iterate over . - public static IEnumerator GetEnumerator(this IComEnumerator cenum) where T : struct => new ComEnumeratorStruct(cenum); - } - - internal class ComEnumeratorStruct : IEnumerator where T : struct - { - private T? cur; - private IComEnumerator instance; - private IComEnumerator_Next next; - private MethodInfo resetMethod; - - public ComEnumeratorStruct(IComEnumerator cenum) - { - instance = cenum; - - //var mi = cenum.GetType().GetMethod("Next"); - //if (!IsMethodCompatibleWithDelegate(mi)) - // throw new ArgumentException("The instance does not support the correct 'Next' method format."); - next = (IComEnumerator_Next)Delegate.CreateDelegate(typeof(IComEnumerator_Next), instance, "Next", false, false) ?? throw new ArgumentException("The instance does not support the correct 'Next' method format."); - - resetMethod = cenum.GetType().GetMethod("Reset", Type.EmptyTypes) ?? throw new ArgumentException("The instance does not support the correct 'Reset' method format."); - resetMethod.Invoke(instance, null); - } - - /// Retrieves the specified number of items in the enumeration sequence. - /// - /// The number of items to be retrieved. If there are fewer than the requested number of items left in the sequence, this method - /// retrieves the remaining elements. - /// - /// An array of enumerated items. - /// - /// The number of items that were retrieved. This parameter is always less than or equal to the number of items requested. - /// - /// If the method retrieves the number of items requested, the return value is S_OK. Otherwise, it is S_FALSE. - public delegate HRESULT IComEnumerator_Next(uint celt, T[] rgelt, out uint pceltFetched); - - public T Current => cur.HasValue ? cur.Value : throw new InvalidOperationException("The index is invalid."); - - object IEnumerator.Current => Current; - - public static bool IsMethodCompatibleWithDelegate(MethodInfo method) where TDel : Delegate - { - var delegateSignature = typeof(TDel).GetMethod("Invoke"); - return delegateSignature.ReturnType == method.ReturnType && delegateSignature.GetParameters().Select(x => x.ParameterType).SequenceEqual(method.GetParameters().Select(x => x.ParameterType)); - } - - public void Dispose() - { - if (!(instance is null)) - { - if (instance.GetType().IsCOMObject) - Marshal.ReleaseComObject(instance); - instance = null; - } - } - - public bool MoveNext() - { - if (instance is null) return false; - var i = new T[] { default }; - var hr = next.Invoke(1, i, out var cnt); - cur = hr == HRESULT.S_OK && cnt == 1 ? i[0] : (T?)null; - return cur.HasValue; - } - - public void Reset() => resetMethod.Invoke(instance, null); - } -} \ No newline at end of file