diff --git a/src/HidApi.Net/Hid.cs b/src/HidApi.Net/Hid.cs
index d487312..aae5d5d 100644
--- a/src/HidApi.Net/Hid.cs
+++ b/src/HidApi.Net/Hid.cs
@@ -33,14 +33,14 @@ public static void Exit()
}
///
- /// Enuemrate available HID devices.
+ /// Enumerate available HID devices.
///
/// Vendor id of devices to open or 0 to match any vendor
/// Product id of devices to open or 0 to match any product
/// Enumerable of
- public static IEnumerable Enumerate(ushort vendorId = 0, ushort productId = 0)
+ public static HidEnumerator Enumerate(ushort vendorId = 0, ushort productId = 0)
{
- return Enumerator.Enumerate(vendorId, productId);
+ return new HidEnumerator(vendorId, productId);
}
///
diff --git a/src/HidApi.Net/Internal/Enumerator.cs b/src/HidApi.Net/Internal/Enumerator.cs
index 7574c34..6d5ad1a 100644
--- a/src/HidApi.Net/Internal/Enumerator.cs
+++ b/src/HidApi.Net/Internal/Enumerator.cs
@@ -1,29 +1,35 @@
namespace HidApi;
-internal static class Enumerator
+public unsafe struct HidEnumerator : IDisposable
{
- public static IEnumerable Enumerate(ushort vendorId, ushort productId)
+ private NativeDeviceInfo* _currentNativeInfo;
+ public DeviceInfo Current { get; private set; }
+
+ public HidEnumerator GetEnumerator() => this;
+
+ public HidEnumerator(ushort vendorId, ushort productId)
{
- var deviceInfos = new List();
- unsafe
- {
- var deviceInfoPointer = NativeMethods.Enumerate(vendorId, productId);
- try
- {
- var currentDeviceInfoPointer = deviceInfoPointer;
+ _currentNativeInfo = NativeMethods.Enumerate(vendorId, productId);
+ Current = DeviceInfo.From(_currentNativeInfo);
+ }
+
+ public bool MoveNext()
+ {
+ _currentNativeInfo = _currentNativeInfo->Next;
- while ((IntPtr) currentDeviceInfoPointer != IntPtr.Zero)
- {
- deviceInfos.Add(DeviceInfo.From(currentDeviceInfoPointer));
- currentDeviceInfoPointer = currentDeviceInfoPointer->Next;
- }
- }
- finally
- {
- NativeMethods.FreeEnumeration(deviceInfoPointer);
- }
+ if ((nint) _currentNativeInfo != nint.Zero)
+ {
+ Current = DeviceInfo.From(_currentNativeInfo);
+ return true;
+ }
+ else
+ {
+ return false;
}
+ }
- return deviceInfos;
+ public void Dispose()
+ {
+ NativeMethods.FreeEnumeration(_currentNativeInfo);
}
}