HidApiNet / HidApiNet.HidDeviceInfoCollection.cs
Code · 35 lines · 714 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace HidApiNet
{
	class HidDeviceInfoCollection : List<HidDeviceInfo>, IDisposable
	{
		private IntPtr devs = IntPtr.Zero;

		public HidDeviceInfoCollection()
		{
			var res = Interop.hid_init();
			if (res != 0)
				return;

			this.devs = Interop.hid_enumerate(0x00, 0x00);
			var curdev = new IntPtr(this.devs.ToInt32());
			while (curdev != IntPtr.Zero)
			{
				var info = (HidDeviceInfo)Marshal.PtrToStructure(curdev, typeof(HidDeviceInfo));
				this.Add(info);
				curdev = info.next;
			}
		}

		public void Dispose()
		{
			if (this.devs != IntPtr.Zero)
				Interop.hid_free_enumeration(this.devs);
		}

	}

}