Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Improve USB device hardware ID triplet collection #476

Merged
merged 2 commits into from
May 15, 2024
Merged
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
25 changes: 18 additions & 7 deletions windows/QMK Toolbox/Usb/UsbDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public UsbDevice(ManagementBaseObject d)
ManufacturerString = (string)WmiDevice.GetPropertyValue("Manufacturer");
ProductString = (string)WmiDevice.GetPropertyValue("Name");

var hardwareIdTriplet = HardwareIdTripletRegex.Match(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);
var hardwareIdTriplet = GetHardwareId(WmiDevice).Value;
VendorId = hardwareIdTriplet.Item1;
ProductId = hardwareIdTriplet.Item2;
RevisionBcd = hardwareIdTriplet.Item3;

Driver = GetDriverName(WmiDevice);
}
Expand All @@ -42,12 +42,23 @@ public override string ToString()
return $"{ManufacturerString} {ProductString} ({VendorId:X4}:{ProductId:X4}:{RevisionBcd:X4})";
}

private static string GetHardwareId(ManagementBaseObject d)
private static (ushort, ushort, ushort)? 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 (
Convert.ToUInt16(match.Groups[1].ToString(), 16),
Convert.ToUInt16(match.Groups[2].ToString(), 16),
Convert.ToUInt16(match.Groups[3].ToString(), 16)
);
}
}
}

return null;
Expand Down
Loading