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);//可以根据你自己的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;}}
}

执行结果

电脑上的设备

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/669905.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

升级Oracle 单实例数据库19.3到19.22

需求 我的Oracle Database Vagrant Box初始版本为19.3,需要升级到最新的RU,当前为19.22。 以下操作时间为为2024年2月5日。 补丁下载 补丁下载文档参见MOS文档:Primary Note for Database Proactive Patch Program (Doc ID 888.1)。 补丁…

企业级Spring boot项目 配置清单

目录 一、服务基础配置 二、配置数据库数据源 三、配置缓存 四、配置日志 五、配置统一异常处理 六、配置swagger文档 七、配置用户登录模块 八、配置websocket 九、配置定时任务 十、配置文件服务器 十一、配置Nacos 十二、配置项目启动数据库默认初始化(liquibas…

Bootstrap5 图片轮播

Bootstrap5 轮播样式表使用的是CDN资源 <title>亚丁号</title><!-- 自定义样式表 --><link href"static/front/css/front.css" rel"stylesheet" /><!-- 新 Bootstrap5 核心 CSS 文件 --><link rel"stylesheet"…

Meta开源大模型LLaMA2的部署使用

LLaMA2的部署使用 LLaMA2申请下载下载模型启动运行Llama2模型文本补全任务实现聊天任务LLaMA2编程Web UI操作 LLaMA2 申请下载 访问meta ai申请模型下载&#xff0c;注意有地区限制&#xff0c;建议选其他国家 申请后会收到邮件&#xff0c;内含一个下载URL地址&#xff0c;…

【翻译】Processing安卓模式的安装使用及打包发布(内含中文版截图)

原文链接在下面的每一章的最前面。 原文有三篇&#xff0c;译者不知道贴哪篇了&#xff0c;这篇干脆标了原创。。 译者声明&#xff1a;本文原文来自于GNU协议支持下的项目&#xff0c;具备开源二改授权&#xff0c;可翻译后公开。 文章目录 Install&#xff08;安装&#xff0…

1041.困于环中的机器人(Java)

题目描述&#xff1a; 在无限的平面上&#xff0c;机器人最初位于 (0, 0) 处&#xff0c;面朝北方。注意: 北方向 是y轴的正方向。 南方向 是y轴的负方向。 东方向 是x轴的正方向。 西方向 是x轴的负方向。 机器人可以接受下列三条指令之一&#xff1a; “G”&#xff1a;直走 …

42、WEB攻防——通用漏洞文件包含LFIRFI伪协议编码算法代码审计

文章目录 文件包含文件包含原理攻击思路文件包含分类 sessionPHP伪协议进行文件包含 文件包含 文件包含原理 文件包含其实就是引用&#xff0c;相当于C语言中的include <stdio.h>。文件包含漏洞常出现于php脚本中&#xff0c;当include($file)中的$file变量用户可控&am…

88 docker 环境下面 前端A连到后端B + 前端B连到后端A

前言 呵呵 最近出现了这样的一个问题, 我们有多个前端服务, 分别连接了对应的后端服务, 前端A -> 后端A, 前端B -> 后端B 但是 最近的时候 却会出现一种情况就是, 有些时候 前端A 连接到了 后端B, 前端B 连接到了 后端A 我们 前端服务使用 nginx 提供前端 html, js…

嵌入式软件bug分析基本要求

摘要&#xff1a;软件从来不是一次就能完美的&#xff0c;需要以包容的眼光看待它的残缺。那问题究竟为何产生&#xff0c;如何去除呢&#xff1f; 1、软件问题从哪来 软件缺陷问题千千万万&#xff0c;主要是需求、实现、和运行环境三方面。 1.1 需求描述偏差 客户角度的描…

Autovue R21.1 发布

作者: justin.jin 2023年9月, Oracle发布了最新版的Autovue R21.1, 它包括了原来21.0.1 和 21.0.2的全部补丁. Autovue R21.1的关键更新如下: 1, 升级为64位: 性能有较大提升, 不会有32位应用单一线程最大使用2G的内存限制. 2, 紧随IT基础架构的变化, 支持Windows Se…

Excel设置单元格下拉框(poi)

前言 年关在即&#xff0c;还在最后的迭代处理&#xff0c;还分了个其他同事的单&#xff0c;说是导出的Excel模版的2列要修改为下拉选项&#xff0c;过程很曲折&#xff0c;不说&#xff0c;以下其实就是一个笔记而已&#xff01; 其实之前分享过阿里的EasyExcel设置单…

jsp页面,让alert弹出信息换行显示

第一种方式&#xff1a;后端拼接上换行符前端显示 1&#xff0c;java后端将信息封装成字符串时&#xff0c;在需要换行的地方拼接上一个换行符&#xff0c; 显示在HTML中的换行&#xff0c;通常需要用<br>标签替代\n&#xff0c;如下&#xff1a; String javaString &…

【教程】ESP32-CAM使用I2C驱动0.91寸OLED

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhang.cn] 先安装库 1. 打开 Arduino IDE&#xff0c;然后转到 Sketch > Include Library > Manage Libraries。 2. 在搜索框中输入“SSD1306”&#xff0c;然后从 Adafruit 安装 SSD1306 库。 3. 选择“install al…

各种编程语言送祝福:2024龙年大吉

我是码农一枚&#xff0c;在这里用不同编程语言中祝福大家"2024&#xff0c;龙年大吉"~ Python print("2024&#xff0c;龙年大吉")Java public class Main {public static void main(String[] args) {System.out.println("2024&#xff0c;龙年大…

文心一言 VS 讯飞星火 VS chatgpt (195)-- 算法导论14.3 3题

三、用go语言&#xff0c;请给出一个有效的算法&#xff0c;对一个给定的区间i&#xff0c;返回一个与i重叠且具有最小低端点的区间;或者当这样的区间不存在时返回T.nil。 文心一言&#xff0c;去掉math&#xff0c;代码正常运行&#xff1a; 在Go语言中&#xff0c;你可以使…

Opencc4j 开源中文繁简体使用介绍

Opencc4j Opencc4j 支持中文繁简体转换&#xff0c;考虑到词组级别。 Features 特点 严格区分「一简对多繁」和「一简对多异」。 完全兼容异体字&#xff0c;可以实现动态替换。 严格审校一简对多繁词条&#xff0c;原则为「能分则不合」。 词库和函数库完全分离&#xff0c…

【技术预研】StarRocks官方文档浅析(4)

背景说明 基于starRocks官方文档&#xff0c;对其内容进行一定解析&#xff0c;方便大家理解和使用。 若无特殊标注&#xff0c;startRocks版本是3.2。 下面的章节和官方文档保持一致。 参考文档 产品简介 | StarRocks StarRocks StarRocks 是一款高性能分析型数据仓库&…

JenkinsGitLab完成自动化构建部署

关于GitLab安装:GitLab安装-CSDN博客 Docker中安装GitLab:Docker下安装GitLab-CSDN博客 安装JenKins Jenkins官网:Jenkins 中文版:Jenkins 安装时候中文页面的war包下不来 在英文页面 记得装JDK8以上 JenKins使用java写的 运行JenKins需要JDK环境 我这里已经装好了 将下…

python制作恶意软件删除工具

今天&#xff0c;来教大家用python制作一个恶意软件删除工具 查杀流程图 对&#xff0c;就这些&#xff0c;已经具备了杀毒软件的功能 判断文件是否为病毒 要查杀病毒&#xff0c;先要判断文件是不是病毒&#xff08;不然删错了咋办&#xff09;&#xff0c;这里我们用获取文…

【RK3288 Android10 C30 支持sim卡拔掉不弹窗,及热插拔】

文章目录 【RK3288 Android10 C30 支持sim卡拔掉不弹窗&#xff0c;及热插拔】需求方案patchframework 【RK3288 Android10 C30 支持sim卡拔掉不弹窗&#xff0c;及热插拔】 需求 由于3288 硬件上的sim卡座不支持热插拔&#xff0c;是没有顶针来识别sim卡是否被拔掉的。所以在…