Removed ICOMEnum<object> from IEnumObjects (it doesn't follow Next method pattern) and added an extension method for IEnumObjects.Enumerate that will enumerate the interfaces exposed by IEnumObject. Fixes #400

pull/411/head
David Hall 2023-05-16 22:09:54 -06:00
parent 7a6b3ce391
commit 736c4f6534
1 changed files with 14 additions and 1 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Vanara.PInvoke
@ -9,7 +10,7 @@ namespace Vanara.PInvoke
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ienumobjects
[PInvokeData("shobjidl_core.h", MSDNShortId = "NN:shobjidl_core.IEnumObjects")]
[ComImport, Guid("2c1c7e2e-2d0e-4059-831e-1e6f82335c2e"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IEnumObjects : Vanara.Collections.ICOMEnum<object>
public interface IEnumObjects
{
/// <summary>Gets the next specified number and type of objects.</summary>
/// <param name="celt">
@ -74,5 +75,17 @@ namespace Vanara.PInvoke
// IEnumObjects **ppenum );
IEnumObjects Clone();
}
/// <summary>Enumerates the objects exposed by a <see cref="IEnumObjects"/> instance.</summary>
/// <typeparam name="TInterface">The type of the interface to enumerate.</typeparam>
/// <param name="obj">The <see cref="IEnumObjects"/> instance.</param>
/// <returns>A sequence of <typeparamref name="TInterface"/> instances.</returns>
public static IEnumerable<TInterface> Enumerate<TInterface>(this IEnumObjects obj) where TInterface : class
{
var iid = typeof(TInterface).GUID;
var arr = new object[1];
while (obj.Next(1, iid, arr, out var fetched) == HRESULT.S_OK && fetched == 1)
yield return arr[0] as TInterface;
}
}
}