linux查看硬盘vid,在linux获取并打印usb移动硬盘vid和pid

代码:

#include

#include

#include

#include

#include

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值