Skip to content

Commit

Permalink
[Windows] Improve USB device hardware ID triplet collection
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark committed May 12, 2024
1 parent 5ae6804 commit ea74214
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions windows/QMK Toolbox/Usb/UsbDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public UsbDevice(ManagementBaseObject d)
ManufacturerString = (string)WmiDevice.GetPropertyValue("Manufacturer");
ProductString = (string)WmiDevice.GetPropertyValue("Name");

var hardwareIdTriplet = HardwareIdTripletRegex.Match(GetHardwareId(WmiDevice));
var hardwareIdTriplet = GetHardwareId(WmiDevice);
VendorId = Convert.ToUInt16(hardwareIdTriplet.Groups[1].ToString(), 16);
ProductId = Convert.ToUInt16(hardwareIdTriplet.Groups[2].ToString(), 16);
RevisionBcd = Convert.ToUInt16(hardwareIdTriplet.Groups[3].ToString(), 16);
Expand All @@ -42,12 +42,19 @@ public override string ToString()
return $"{ManufacturerString} {ProductString} ({VendorId:X4}:{ProductId:X4}:{RevisionBcd:X4})";
}

private static string GetHardwareId(ManagementBaseObject d)
private static Match GetHardwareId(ManagementBaseObject d)
{
var hardwareIds = (string[])d.GetPropertyValue("HardwareID");
if (hardwareIds != null && hardwareIds.Length > 0)
if (hardwareIds != null)
{
return hardwareIds[0];
foreach (string hardwareId in hardwareIds)
{
Match match = HardwareIdTripletRegex.Match(hardwareId);
if (match.Success)
{
return match;
}
}
}

return null;
Expand Down

0 comments on commit ea74214

Please sign in to comment.