Skip to content

Commit 9bc62a0

Browse files
author
Dilawar Singh
committed
chore: Adds friendly names
1 parent 219e28d commit 9bc62a0

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/common.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ pub struct UsbDevice {
1010
pub vendor_id: u16,
1111
/// Product ID
1212
pub product_id: u16,
13+
/// Friendly name.
14+
pub friendly_name: Option<String>,
1315
/// Optional device description
1416
pub description: Option<String>,
1517
/// Optional serial number
1618
pub serial_number: Option<String>,
1719
/// Class (bDeviceBaseClass) of device.
1820
pub base_class: Option<DeviceBaseClass>,
21+
/// Manufacturer
22+
pub manufacturer: Option<String>,
1923
}
2024

2125
/// See <https://www.usb.org/defined-class-codes>

src/windows.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ use windows_sys::{
99
Win32::Devices::DeviceAndDriverInstallation::{
1010
SetupDiDestroyDeviceInfoList, SetupDiEnumDeviceInfo, SetupDiGetClassDevsW,
1111
SetupDiGetDeviceInstanceIdW, SetupDiGetDeviceRegistryPropertyW, DIGCF_ALLCLASSES,
12-
DIGCF_PRESENT, SPDRP_CLASS, SPDRP_DEVICEDESC, SPDRP_HARDWAREID, SP_DEVINFO_DATA,
12+
DIGCF_PRESENT, SPDRP_CLASS, SPDRP_DEVICEDESC, SPDRP_FRIENDLYNAME, SPDRP_HARDWAREID,
13+
SPDRP_MFG, SP_DEVINFO_DATA,
1314
},
1415
};
1516

1617
pub fn enumerate_platform(vid: Option<u16>, pid: Option<u16>) -> Vec<UsbDevice> {
1718
let mut output: Vec<UsbDevice> = Vec::new();
18-
// let usb: Vec<u16> = OsStr::new("USB\0").encode_wide().collect();
1919
let usb = w!("USB\0");
2020
let dev_info =
2121
unsafe { SetupDiGetClassDevsW(null(), usb, -1, DIGCF_ALLCLASSES | DIGCF_PRESENT) };
@@ -77,6 +77,42 @@ pub fn enumerate_platform(vid: Option<u16>, pid: Option<u16>) -> Vec<UsbDevice>
7777
class = Some(string_from_buf_u8(buf));
7878
}
7979

80+
// manufactor
81+
buf = vec![0; 1000];
82+
let mut manufacturer = None;
83+
if unsafe {
84+
SetupDiGetDeviceRegistryPropertyW(
85+
dev_info,
86+
&mut dev_info_data,
87+
SPDRP_MFG,
88+
null_mut(),
89+
buf.as_mut_ptr(),
90+
buf.len() as u32,
91+
null_mut(),
92+
)
93+
} > 0
94+
{
95+
manufacturer = Some(string_from_buf_u8(buf));
96+
}
97+
98+
// friendly name
99+
buf = vec![0; 1000];
100+
let mut friendly_name = None;
101+
if unsafe {
102+
SetupDiGetDeviceRegistryPropertyW(
103+
dev_info,
104+
&mut dev_info_data,
105+
SPDRP_FRIENDLYNAME,
106+
null_mut(),
107+
buf.as_mut_ptr(),
108+
buf.len() as u32,
109+
null_mut(),
110+
)
111+
} > 0
112+
{
113+
friendly_name = Some(string_from_buf_u8(buf));
114+
}
115+
80116
buf = vec![0; 1000];
81117

82118
if unsafe {
@@ -111,6 +147,8 @@ pub fn enumerate_platform(vid: Option<u16>, pid: Option<u16>) -> Vec<UsbDevice>
111147
id,
112148
vendor_id,
113149
product_id,
150+
friendly_name,
151+
manufacturer,
114152
description: Some(description),
115153
serial_number,
116154
base_class: class.map(|cls| cls.into()),

0 commit comments

Comments
 (0)