在linux获取并打印usb移动硬盘vid和pid
来源:互联网 发布:烈火战神 源码分析 编辑:程序博客网 时间:2024/09/21 09:29
代码:
#include <libudev.h>#include <stdio.h>#include <stdlib.h>#include <locale.h>#include <unistd.h>int main (void){ struct udev *udev; struct udev_enumerate *enumerate; struct udev_list_entry *devices, *dev_list_entry; struct udev_device *dev; /* Create the udev object */ udev = udev_new(); if (!udev) { printf("Can't create udev\n"); exit(1); } /* Create a list of the devices in the 'hidraw' subsystem. */ enumerate = udev_enumerate_new(udev); udev_enumerate_add_match_subsystem(enumerate, "block"); udev_enumerate_scan_devices(enumerate); devices = udev_enumerate_get_list_entry(enumerate); /* For each item enumerated, print out its information. udev_list_entry_foreach is a macro which expands to a loop. The loop will be executed for each member in devices, setting dev_list_entry to a list entry which contains the device's path in /sys. */ udev_list_entry_foreach(dev_list_entry, devices) { const char *path; /* Get the filename of the /sys entry for the device and create a udev_device object (dev) representing it */ path = udev_list_entry_get_name(dev_list_entry); dev = udev_device_new_from_syspath(udev, path); /* usb_device_get_devnode() returns the path to the device node itself in /dev. */ printf("Device Node Path: %s\n", udev_device_get_devnode(dev)); /* The device pointed to by dev contains information about the hidraw device. In order to get information about the USB device, get the parent device with the subsystem/devtype pair of "usb"/"usb_device". This will be several levels up the tree, but the function will find it.*/ dev = udev_device_get_parent_with_subsystem_devtype( dev, "usb", "usb_device"); if (!dev) { printf("Unable to find parent usb device."); exit(1); } /* From here, we can call get_sysattr_value() for each file in the device's /sys entry. The strings passed into these functions (idProduct, idVendor, serial, etc.) correspond directly to the files in the directory which represents the USB device. Note that USB strings are Unicode, UCS2 encoded, but the strings returned from udev_device_get_sysattr_value() are UTF-8 encoded. */ printf(" VID/PID: %s %s\n", udev_device_get_sysattr_value(dev,"idVendor"), udev_device_get_sysattr_value(dev, "idProduct")); printf(" %s\n %s\n", udev_device_get_sysattr_value(dev,"manufacturer"), udev_device_get_sysattr_value(dev,"product")); printf(" serial: %s\n", udev_device_get_sysattr_value(dev, "serial")); udev_device_unref(dev); } /* Free the enumerator object */ udev_enumerate_unref(enumerate); udev_unref(udev); return 0; }
编译:g++ test.cpp -o test -ludev
运行:sudo ./test
输出:
Device Node Path: /dev/sdc
VID/PID: 13fd 0840
Generic
External
serial: 53423245343148374747414C
Device Node Path: /dev/sdc1
VID/PID: 13fd 0840
Generic
External
serial: 53423245343148374747414C
Device Node Path: /dev/sdc2
VID/PID: 13fd 0840
Generic
External
serial: 53423245343148374747414C
Device Node Path: /dev/sdc3
VID/PID: 13fd 0840
Generic
External
serial: 53423245343148374747414C
Device Node Path: /dev/sdc4
VID/PID: 13fd 0840
Generic
External
serial: 53423245343148374747414C
Device Node Path: /dev/sda
- 在linux获取并打印usb移动硬盘vid和pid
- USB VID和PID
- 通过Windows注册表获取U盘、移动硬盘和USB读卡器等设备的PID、VID和序列号
- 通过Windows注册表获取U盘、移动硬盘和USB读卡器等设备的PID、VID和序列号
- usb的pid和vid
- usb的pid和vid
- USB的VID和PID
- usb的pid和vid
- 获取指定USB设备的VID PID和SerialNumber
- 获取指定USB设备的VID PID和SerialNumber
- 【VC】 获取电脑USB设备VID和PID等信息。
- 获取电脑USB设备VID和PID等信息。
- 根据VID PID获取USB设备路径
- 获取usb设备的vid,pid
- 浅谈USB设备的VID和PID
- 浅谈USB设备的VID和PID
- 浅谈USB设备的VID和PID
- USB设备的VID和PID
- java定时任务,每天定时执行任务
- Hibernate配置问题
- JAVA循环体的过滤器 (continue)
- nfs文件系统的一个错误--无法挂载文件系统,只读文件系统
- XML WebService完全实例详细解析(1)
- 在linux获取并打印usb移动硬盘vid和pid
- 关于开通博客的一点小花絮
- my salesforce
- UML关系(泛化、依赖、关联、聚合、组合)
- 配置linux(suse 11 sp2,linux 3.0)服务端 ,使xmanage可连接linux的共享桌面
- 巧用CSS的Border属性
- struts2 传值方式
- TI davinci DM368 u-boot 编译方法
- JAVA Socket超时浅析