总线已报告设备描述 DEVPKEY_Device_BusReportedDeviceDesc
模式 winform 语言 c#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace testusb
{public partial class Form1 : Form{[DllImport("setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]static extern IntPtr SetupDiGetClassDevs(ref Guid classGuid, string enumerator, IntPtr hwndParent, uint flags);[DllImport("setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, uint memberIndex, ref SP_DEVINFO_DATA deviceInfoData);[DllImport("setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]static extern bool SetupDiGetDeviceProperty(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, ref DEVPROPKEY propertyKey, out int propertyType, IntPtr propertyBuffer, int propertyBufferSize, out int requiredSize, int flags);[DllImport("setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]static extern bool SetupDiDestroyDeviceInfoList(IntPtr deviceInfoSet);[StructLayout(LayoutKind.Sequential)]struct SP_DEVINFO_DATA{public int cbSize;public Guid classGuid;public int devInst;public IntPtr reserved;}[StructLayout(LayoutKind.Sequential)]struct DEVPROPKEY{public Guid fmtid;public uint pid;}public Form1(){InitializeComponent();}//按钮事件private void button1_Click(object sender, EventArgs e){// USB 设备的 Vendor ID (VID) 和 Product ID (PID)string vid = "0xFFFF"; //这边写你usb设备的vidstring pid = "0x0100"; //这边写你usb设备的pidGuid guid = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED"); // USB 设备类的 GUIDIntPtr deviceInfoSet = SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, 0x12);if (deviceInfoSet.ToInt64() != -1){SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA();devInfoData.cbSize = Marshal.SizeOf(devInfoData);for (uint i = 0; SetupDiEnumDeviceInfo(deviceInfoSet, i, ref devInfoData); i++){DEVPROPKEY propertyKey = new DEVPROPKEY();propertyKey.fmtid = new Guid("540b947e-8b40-45bc-a8a2-6a0b894cbda2"); // DEVPKEY_Device_BusReportedDeviceDesc 的格式 IDpropertyKey.pid = 4; // DEVPKEY_Device_BusReportedDeviceDesc 的属性 IDint propertyType;int requiredSize;// 获取设备属性值的大小SetupDiGetDeviceProperty(deviceInfoSet, ref devInfoData, ref propertyKey, out propertyType, IntPtr.Zero, 0, out requiredSize, 0);IntPtr propertyBuffer = Marshal.AllocHGlobal(requiredSize);//可以根据你自己的vid pid 来进行判断 //string deviceVid = GetDeviceVID(deviceInfoSet, devInfoData); //获取vid//string devicePid = GetDevicePID(deviceInfoSet, devInfoData); //获取pid if (SetupDiGetDeviceProperty(deviceInfoSet, ref devInfoData, ref propertyKey, out propertyType, propertyBuffer, requiredSize, out requiredSize, 0)){string propertyValue = Marshal.PtrToStringUni(propertyBuffer);Console.WriteLine("Device Description: " + propertyValue);}Marshal.FreeHGlobal(propertyBuffer);}SetupDiDestroyDeviceInfoList(deviceInfoSet);}}//获取vidprivate static string GetDeviceVID(IntPtr deviceInfoSet, SP_DEVINFO_DATA devInfoData){DEVPROPKEY devPropKey = new DEVPROPKEY();devPropKey.fmtid = new Guid("A45C254E-DF1C-4EFD-8020-67D146A850E0"); // DEVPKEY_Device_HardwareIds 的格式 IDdevPropKey.pid = 3; // DEVPKEY_Device_HardwareIds 的属性 IDint propertyType;int requiredSize;// 获取设备属性值的大小SetupDiGetDeviceProperty(deviceInfoSet, ref devInfoData, ref devPropKey, out propertyType, IntPtr.Zero, 0, out requiredSize, 0);IntPtr propertyBuffer = Marshal.AllocHGlobal(requiredSize);if (SetupDiGetDeviceProperty(deviceInfoSet, ref devInfoData, ref devPropKey, out propertyType, propertyBuffer, requiredSize, out requiredSize, 0)){byte[] propertyValueBytes = new byte[requiredSize];Marshal.Copy(propertyBuffer, propertyValueBytes, 0, requiredSize);Marshal.FreeHGlobal(propertyBuffer);string propertyValue = System.Text.Encoding.Unicode.GetString(propertyValueBytes).Trim('\0');// 从属性值中提取 VIDstring[] hardwareIds = propertyValue.Split(new string[] { "\0" }, StringSplitOptions.RemoveEmptyEntries);foreach (string id in hardwareIds){if (id.StartsWith("USB\\VID_", StringComparison.InvariantCultureIgnoreCase)){string vid = id.Substring(8, 4); // 返回 VID 值return vid;}}}Marshal.FreeHGlobal(propertyBuffer);return null;}//获取pidprivate string GetDevicePID(IntPtr deviceInfoSet, SP_DEVINFO_DATA devInfoData){DEVPROPKEY devPropKey = new DEVPROPKEY();devPropKey.fmtid = new Guid("A45C254E-DF1C-4EFD-8020-67D146A850E0"); // DEVPKEY_Device_HardwareIds 的格式 IDdevPropKey.pid = 3; // DEVPKEY_Device_HardwareIds 的属性 IDint propertyType;int requiredSize;// 获取设备属性值的大小SetupDiGetDeviceProperty(deviceInfoSet, ref devInfoData, ref devPropKey, out propertyType, IntPtr.Zero, 0, out requiredSize, 0);IntPtr propertyBuffer = Marshal.AllocHGlobal(requiredSize);if (SetupDiGetDeviceProperty(deviceInfoSet, ref devInfoData, ref devPropKey, out propertyType, propertyBuffer, requiredSize, out requiredSize, 0)){string propertyValue = Marshal.PtrToStringUni(propertyBuffer);Marshal.FreeHGlobal(propertyBuffer);// 从属性值中提取 PIDstring[] hardwareIds = propertyValue.Split('&');foreach (string id in hardwareIds){if (id.StartsWith("PID_")){return id.Substring(4, 4); // 返回 PID 值}}}Marshal.FreeHGlobal(propertyBuffer);return null;}}
}
执行结果
电脑上的设备