Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/HidApi.Net/Hid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public static void Exit()
}

/// <summary>
/// Enuemrate available HID devices.
/// Enumerate available HID devices.
/// </summary>
/// <param name="vendorId">Vendor id of devices to open or 0 to match any vendor</param>
/// <param name="productId">Product id of devices to open or 0 to match any product</param>
/// <returns>Enumerable of <seealso cref="DeviceInfo"/></returns>
public static IEnumerable<DeviceInfo> 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);
}

/// <summary>
Expand Down
46 changes: 26 additions & 20 deletions src/HidApi.Net/Internal/Enumerator.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
namespace HidApi;

internal static class Enumerator
public unsafe struct HidEnumerator : IDisposable
{
public static IEnumerable<DeviceInfo> 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<DeviceInfo>();
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);
}
}