读取的信息很多,但是我目前只需要硬件ID、pid、vid、设备名称:
头文件代码如下:
#include <windows.h>
#include <setupapi.h>
#include <Cfgmgr32.h>
#include <iostream>
#include <vector>
#include <string>
#include <initguid.h>
#include <Usbiodef.h>
#include <Usb100.h>
#include <Usbioctl.h>
/* 表示USB设备的设备结构 */
struct Device
{
std::string strDevPath; //设备路径
USB_DEVICE_DESCRIPTOR stDevDescriptor; //设备描述符
std::string manufacture; //制造商名称
std::string serial; //序列号
std::string product; //产品名称
};
class CFindUsbDevices
{
std::wstring GetHubPath(unsigned long iDevice);
long GetPortByLocation(const std::wstring& strLocation);
std::string GetStrDescriptor(std::wstring strHubPath, ULONG port, USHORT index);
USB_DEVICE_DESCRIPTOR GetDevDescriptor(std::wstring strHubPath, ULONG port);
public:
std::vector<Device*> GetDevices(GUID* guid);
};
cpp文件代码:
#include "FindUsbDevices.h"
#pragma comment(lib, "Setupapi.lib")
/**
* 获取中心路径对象
* param实例集线器的设备实例
* return与提供的设备实例关联的集线器的设备路径
*/
std::wstring CFindUsbDevices::GetHubPath(unsigned long iDevice)
{
std::wstring strHubPath;
DWORD iMember = 0;
SP_DEVINFO_DATA stDeviceInfo;
SP_DEVICE_INTERFACE_DATA stDeviceInter;
PSP_DEVICE_INTERFACE_DETAIL_DATA stDeviceDetail;
DWORD Flags = DIGCF_PRESENT | DIGCF_DEVICEINTERFACE;
HDEVINFO pDeviceInfo = SetupDiGetClassDevs((LPGUID)&GUID_DEVINTERFACE_USB_HUB, NULL, NULL, Flags);
if (INVALID_HANDLE_VALUE != pDeviceInfo) {
while (ERROR_NO_MORE_ITEMS != GetLastError()) {
stDeviceInfo.cbSize = sizeof(SP_DEVINFO_DATA);
auto bSutup = SetupDiEnumDeviceInfo(pDeviceInfo, iMember, &stDeviceInfo);
if(bSutup && stDeviceInfo.DevInst == iDevice) {
stDeviceInter.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
auto bDiEnum = SetupDiEnumDeviceInterfaces(pDeviceInfo, NULL, (LPGUID)&GUID_DEVINTERFACE_USB_H