前些日子,自己接手专门为Android设备开发的数据恢复软件,第一次作为项目经理带队进行项目开发。
其中为了解决设备和型号的对应关系,用于区分当前接入系统的设备是USB设备,并且是Android设备而不是通常的USB设备或者其他的手机设备。
故需要获得设备的硬件设备序号,根据硬件信息特征码来和Android特征码数据库进行匹配,来取得设备的型号。
首先根据硬件的GUID获取USB类设备集句柄
//USB设备的GUID
static const GUID GUID_DEVINTERFACE_USB_DEVICE =
{ 0xA5DCBF10L, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };
//磁盘的GUID
const GUID* szDiskClassGuid = &GUID_DEVINTERFACE_DISK; //磁盘类
const GUID* volumeGuid_ = &GUID_DEVINTERFACE_VOLUME; //卷类
//获取USB类设备集句柄
int CusbDeviceInfo::EnumUsbDeviceSet()
{
// Get device interface info set handle for all devices attached to system
hDevInfo = NULL;
hDevInfo = SetupDiGetClassDevs(
szDiskClassGuid, /* CONST GUID * ClassGuid - USB class GUID */
NULL, /* PCTSTR Enumerator */
NULL, /* HWND hwndParent */
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE/* DWORD Flags */);
if(hDevInfo == INVALID_HANDLE_VALUE)
{
DWORD err = GetLastError();
qDebug()<<"EnumUsbDeviceSet= "<<QString::number(err);
return -1;
}
//获取可移动分区
bool Rv = GetAllRemovablePartitions();
//if (Rv)
//{
int ret = EnumUsbDeviceInfo();
return ret;
//}
//else
//{
// return -1;
//}
}
//获取枚举的设备信息
SP_DEVINFO_DATA devInfoData = {sizeof(SP_DEVINFO_DATA)};
bRet = SetupDiEnumDeviceInfo(hDevInfo,dwIndex,&devInfoData);
if(!bRet)
{
DWORD err = GetLastError();
SetupDiDestroyDeviceInfoList(hDevInfo);
break;
}
DWORD nSize=0 ;
TCHAR DevInstanceId[MAX_PATH];
if(SetupDiGetDeviceInstanceId(hDevInfo, &devInfoData, DevInstanceId, sizeof(DevInstanceId), &nSize))
{
QString DeviceId = QString::fromWCharArray(DevInstanceId);
//m_DeviceIdVector.append(DeviceId);
if (DeviceId.left(3).toUpper() == "USB")
{
qDebug()<<"DeviceId = "<<DeviceId;
}
}
else
{
DWORD err = GetLastError();
continue;
}
//
//获取设备接口数据集
SP_DEVICE_INTERFACE_DATA devInterfaceData;
BYTE buffer[BUFFER_SIZE];
DWORD RequiredSize;
TCHAR buf[BUFFER_SIZE];
DEVINST devInstParent;
ZeroMemory(&devInterfaceData, sizeof(devInterfaceData));
devInterfaceData.cbSize = sizeof(devInterfaceData);
if(!SetupDiEnumDeviceInterfaces(hDevInfo,NULL,szDiskClassGuid,dwIndex,&devInterfaceData))
{
qDebug()<<"SetupDiEnumDeviceInterfaces error ";
DWORD err = GetLastError();
continue;
}
//获取设备接口细节信息
ZeroMemory(&devInfoData, sizeof(devInfoData));
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
PSP_DEVICE_INTERFACE_DETAIL_DATA pDevDetail;
pDevDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)buffer;
pDevDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
bool ret = SetupDiGetDeviceInterfaceDetail(hDevInfo, &devInterfaceData,
pDevDetail, BUFFER_SIZE, &RequiredSize, &devInfoData);
if(!ret)
{
qDebug()<<"SetupDiGetDeviceInterfaceDetail error ";
DWORD err = GetLastError();
continue;
}