private static List <Device> EnumerateDevices(IntPtr devInfo) { List <Device> devices = new List <Device> (); Dictionary <string, Device> devicesByHwId = new Dictionary <string, Device> (); for (uint i = 0; ; i++) { WinApi.SP_DEVINFO_DATA devInfoData = new WinApi.SP_DEVINFO_DATA(); devInfoData.cbSize = (uint)Marshal.SizeOf(devInfoData); if (!WinApi.SetupDiEnumDeviceInfo(devInfo, i, ref devInfoData)) { break; } Device device = Device.FromDevInfo(devInfo, devInfoData); if (device != null) { if (!devicesByHwId.ContainsKey(device.HardwareId)) { devicesByHwId[device.HardwareId] = device; devices.Add(device); } else { // TODO: should solve this differently, the hardware // IDs aren't unique... } } } return(devices); }
private Device(IntPtr devInfo, WinApi.SP_DEVINFO_DATA devInfoData, string name, string hardwareId, string physicalDeviceObjectName, UInt32 capabilities) { this.devInfo = devInfo; this.devInfoData = devInfoData; this.name = name; this.hardwareId = hardwareId; this.physicalDeviceObjectName = physicalDeviceObjectName; this.capabilities = capabilities; }
public static Device FromDevInfo(IntPtr devInfo, WinApi.SP_DEVINFO_DATA devInfoData) { Device device = null; string name = null; string hardwareId = null; string physicalDeviceObjectName = null; UInt32 capabilities = 0; byte[] buf = new byte[1024]; uint reqBufSize; if (WinApi.SetupDiGetDeviceRegistryProperty(devInfo, ref devInfoData, WinApi.SPDRP_FRIENDLYNAME, IntPtr.Zero, buf, (uint)buf.Length, out reqBufSize)) { name = WinApi.ByteArrayToString(buf); } if (name == null) { if (WinApi.SetupDiGetDeviceRegistryProperty(devInfo, ref devInfoData, WinApi.SPDRP_DEVICEDESC, IntPtr.Zero, buf, (uint)buf.Length, out reqBufSize)) { name = WinApi.ByteArrayToString(buf); } } if (WinApi.SetupDiGetDeviceRegistryProperty(devInfo, ref devInfoData, WinApi.SPDRP_HARDWAREID, IntPtr.Zero, buf, (uint)buf.Length, out reqBufSize)) { string[] tokens = WinApi.MarshalMultiStringToStringArray(buf); hardwareId = String.Join(";", tokens); } if (WinApi.SetupDiGetDeviceRegistryProperty(devInfo, ref devInfoData, WinApi.SPDRP_PHYSICAL_DEVICE_OBJECT_NAME, IntPtr.Zero, buf, (uint)buf.Length, out reqBufSize)) { physicalDeviceObjectName = WinApi.ByteArrayToString(buf); } if (WinApi.SetupDiGetDeviceRegistryProperty(devInfo, ref devInfoData, WinApi.SPDRP_CAPABILITIES, IntPtr.Zero, buf, (uint)buf.Length, out reqBufSize)) { capabilities = BitConverter.ToUInt32(buf, 0); } if (name != null && hardwareId != null) { device = new Device(devInfo, devInfoData, name, hardwareId, physicalDeviceObjectName, capabilities); } return(device); }
private static List<Device> EnumerateDevices (IntPtr devInfo) { List<Device> devices = new List<Device> (); Dictionary<string, Device> devicesByHwId = new Dictionary<string, Device> (); for (uint i = 0; ; i++) { WinApi.SP_DEVINFO_DATA devInfoData = new WinApi.SP_DEVINFO_DATA (); devInfoData.cbSize = (uint)Marshal.SizeOf (devInfoData); if (!WinApi.SetupDiEnumDeviceInfo (devInfo, i, ref devInfoData)) break; Device device = Device.FromDevInfo (devInfo, devInfoData); if (device != null) { if (!devicesByHwId.ContainsKey (device.HardwareId)) { devicesByHwId[device.HardwareId] = device; devices.Add (device); } else { // TODO: should solve this differently, the hardware // IDs aren't unique... } } } return devices; }
private Device (IntPtr devInfo, WinApi.SP_DEVINFO_DATA devInfoData, string name, string hardwareId, string physicalDeviceObjectName, UInt32 capabilities) { this.devInfo = devInfo; this.devInfoData = devInfoData; this.name = name; this.hardwareId = hardwareId; this.physicalDeviceObjectName = physicalDeviceObjectName; this.capabilities = capabilities; }