21xrx.com
2024-09-20 01:10:15 Friday
登录
文章检索 我的文章 写文章
C++如何获取USB设备序列号
2023-06-30 16:15:23 深夜i     --     --
C++ USB 设备 序列号

在C++编程中,有时需要获取USB设备的序列号以进行操作和管理,如果您也需要获取USB设备的序列号,下面是几种C++代码实现方法。

方法一:使用Windows API

Windows提供了一些API来获取USB设备的序列号,例如GetVolumeInformation()和DeviceIoControl()等函数。

示例代码:


#include <windows.h>

#include <setupapi.h>

#include <cfgmgr32.h>

#include <initguid.h>

#include <devpkey.h>

#include <iostream>

#include <string>

#pragma comment(lib, "setupapi.lib")

#pragma comment(lib, "cfgmgr32.lib")

int main()

{

  const GUID GUID_DEVINTERFACE_USB_DEVICE = {0xA5DCBF10L, 0x6530, 0x11D2, 0x51};

  HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;

  SP_DEVICE_INTERFACE_DATA devInterfaceData;

  SP_DEVICE_INTERFACE_DETAIL_DATA* devInterfaceDetailData = NULL;

  DWORD length, requiredLength = 0;

  ULONG ulStatus, ulProblemNumber;

  int index = 0;

  std::wstring devicePath;

  HANDLE hFile;

  hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);

  if(hDevInfo != INVALID_HANDLE_VALUE)

  {

    devInterfaceData.cbSize = sizeof(devInterfaceData);

    while(SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &GUID_DEVINTERFACE_USB_DEVICE, index++, &devInterfaceData))

    {

      SetupDiGetDeviceInterfaceDetail(hDevInfo, &devInterfaceData, NULL, 0, &requiredLength, NULL);

      devInterfaceDetailData = (SP_DEVICE_INTERFACE_DETAIL_DATA*) malloc(requiredLength + sizeof(SP_DEVINFO_DATA));

      if(devInterfaceDetailData == NULL)

      

        continue;

      

      devInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

      if(SetupDiGetDeviceInterfaceDetail(hDevInfo, &devInterfaceData, devInterfaceDetailData, requiredLength, &length, NULL))

      {

        devicePath.assign(devInterfaceDetailData->DevicePath);

        hFile = CreateFile(devicePath.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

        if(hFile != INVALID_HANDLE_VALUE)

        {

          STORAGE_DEVICE_NUMBER storageDeviceNumber = { 0 };

          if(DeviceIoControl(hFile, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &storageDeviceNumber, sizeof(STORAGE_DEVICE_NUMBER), &length, NULL))

          

            std::cout << "USB device serial number: " << storageDeviceNumber.SerialNumber << std::endl;

          

          CloseHandle(hFile);

        }

      }

      free(devInterfaceDetailData);

    }

    SetupDiDestroyDeviceInfoList(hDevInfo);

  }

  return 0;

}

方法二:使用libusb库

libusb是一个跨平台的USB库,可以访问USB设备并读写数据,同时也提供了获取USB设备序列号的API。

示例代码:


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <libusb-1.0/libusb.h>

void print_serial_number(libusb_device_handle* dev_handle)

{

  unsigned char buf[256];

  int rc = libusb_control_transfer(dev_handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,

                   0x01, 0x01, 0x00, buf, 256, 0);

  if (rc < 0)

  {

    printf("Error getting serial number\n");

    return;

  }

  printf("USB device serial number: %s\n", buf);

}

int main()

{

  int returnCode = 0;

  libusb_context* ctx = NULL;

  returnCode = libusb_init(&ctx);

  if (returnCode < 0)

  {

    printf("Error initializing libusb: %s\n", libusb_error_name(returnCode));

    return -1;

  }

  libusb_device_handle* dev_handle = NULL;

  dev_handle = libusb_open_device_with_vid_pid(ctx, 0xxxxx, 0xxxxx); // 替换为USB设备的VID和PID

  if (dev_handle == NULL)

  {

    printf("Error finding USB device\n");

    libusb_exit(ctx);

    return -1;

  }

  returnCode = libusb_claim_interface(dev_handle, 0);

  if (returnCode != LIBUSB_SUCCESS)

  {

    printf("Error claiming interface\n");

    libusb_close(dev_handle);

    libusb_exit(ctx);

    return -1;

  }

  print_serial_number(dev_handle);

  libusb_release_interface(dev_handle, 0);

  libusb_close(dev_handle);

  libusb_exit(ctx);

  return 0;

}

值得注意的是,两种方法都需要先将USB设备插入计算机中,才能获取到相应的序列号。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章