C# 根据USB设备VID和PID 获取设备总线已报告设备描述

2024-02-02 00:12

本文主要是介绍C# 根据USB设备VID和PID 获取设备总线已报告设备描述,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

总线已报告设备描述   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);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);}}}
}

执行结果

电脑上的设备

这篇关于C# 根据USB设备VID和PID 获取设备总线已报告设备描述的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/668905

相关文章

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

C#高效实现在Word文档中自动化创建图表的可视化方案

《C#高效实现在Word文档中自动化创建图表的可视化方案》本文将深入探讨如何利用C#,结合一款功能强大的第三方库,实现在Word文档中自动化创建图表,为你的数据呈现和报告生成提供一套实用且高效的解决方... 目录Word文档图表自动化:为什么选择C#?从零开始:C#实现Word文档图表的基本步骤深度优化:C

在C#中分离饼图的某个区域的操作指南

《在C#中分离饼图的某个区域的操作指南》在处理Excel饼图时,我们可能需要将饼图的各个部分分离出来,以使它们更加醒目,Spire.XLS提供了Series.DataFormat.Percent属性,... 目录引言如何设置饼图各分片之间分离宽度的代码示例:从整个饼图中分离单个分片的代码示例:引言在处理

C#借助Spire.XLS for .NET实现在Excel中添加文档属性

《C#借助Spire.XLSfor.NET实现在Excel中添加文档属性》在日常的数据处理和项目管理中,Excel文档扮演着举足轻重的角色,本文将深入探讨如何在C#中借助强大的第三方库Spire.... 目录为什么需要程序化添加Excel文档属性使用Spire.XLS for .NET库实现文档属性管理Sp

springboot的controller中如何获取applicatim.yml的配置值

《springboot的controller中如何获取applicatim.yml的配置值》本文介绍了在SpringBoot的Controller中获取application.yml配置值的四种方式,... 目录1. 使用@Value注解(最常用)application.yml 配置Controller 中

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C# 预处理指令(# 指令)的具体使用

《C#预处理指令(#指令)的具体使用》本文主要介绍了C#预处理指令(#指令)的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1、预处理指令的本质2、条件编译指令2.1 #define 和 #undef2.2 #if, #el

C#实现将Excel工作表拆分为多个窗格

《C#实现将Excel工作表拆分为多个窗格》在日常工作中,我们经常需要处理包含大量数据的Excel文件,本文将深入探讨如何在C#中利用强大的Spire.XLSfor.NET自动化实现Excel工作表的... 目录为什么需要拆分 Excel 窗格借助 Spire.XLS for .NET 实现冻结窗格(Fro

C# Semaphore与SemaphoreSlim区别小结

《C#Semaphore与SemaphoreSlim区别小结》本文主要介绍了C#Semaphore与SemaphoreSlim区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录一、核心区别概览二、详细对比说明1.跨进程支持2.异步支持(关键区别!)3.性能差异4.API 差

C# List.Sort四种重载总结

《C#List.Sort四种重载总结》本文详细分析了C#中List.Sort()方法的四种重载形式及其实现原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友... 目录1. Sort方法的四种重载2. 具体使用- List.Sort();- IComparable