PVOID buf = malloc(100);
char * buf1 = (char *)malloc(100);
PVOID buf2 = malloc(100);
char * buf3 = (char *)malloc(100);
PVOID buf4 = malloc(100);
char * buf5 = (char *)malloc(100);
int num = 0;
int num1 = 0;
int num2 = 0;
PHIDD_ATTRIBUTES a;
UpdateData(false); //更新界面
int Count = 0; //Total number of devices found
DWORD strSize=0,requiredSize=0;
BOOL result1,result2;
//定义一些变量,以后会用到
SP_DEVINFO_DATA DeviceInfoData;
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData;
//第一步:获取deviceID
GUID deviceId;
HidD_GetHidGuid(&deviceId);
//第二步:获取设备信息
HDEVINFO handle;
handle = SetupDiGetClassDevs(&deviceId, NULL, NULL, (DIGCF_DEVICEINTERFACE | DIGCF_PRESENT)); //Get only HID devices
//第三步:对所有的设备进行枚举
result1=false; //定义一些变量
result2=false;
CString temp11="";
while(1)
{
DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
result1 = SetupDiEnumDeviceInterfaces(
handle,
NULL, // IN PSP_DEVINFO_DATA DeviceInfoData, OPTIONAL
&deviceId,
Count,
&DeviceInterfaceData
);
if(result1 == FALSE)
{
break;
}
//获得设备详细数据(初步)
result1 = SetupDiGetDeviceInterfaceDetail(handle,
&DeviceInterfaceData,
NULL,
0,
&strSize,
NULL);
requiredSize=strSize;
DeviceInterfaceDetailData=(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiredSize);
DeviceInterfaceDetailData->cbSize=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
DeviceInfoData.cbSize=sizeof(SP_DEVINFO_DATA);
//再次获得详细数据
SetupDiGetDeviceInterfaceDetail(handle,
&DeviceInterfaceData,
DeviceInterfaceDetailData,
strSize,
&requiredSize,
&DeviceInfoData);
//获得设备路径(最重要的部分)
//m_zf1=DeviceInterfaceDetailData->DevicePath;
UpdateData(false);
Count++;
}
if(Count != 0)
{
//AfxMessageBox("进来了");
a = (PHIDD_ATTRIBUTES)malloc(100);
a->Size = sizeof(struct _HIDD_ATTRIBUTES);
HANDLE hCom = CreateFile(DeviceInterfaceDetailData->DevicePath,
NULL,//GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if (hCom == INVALID_HANDLE_VALUE)
{
AfxMessageBox("无效句柄!");
}
HidD_GetAttributes(hCom,a);
//m_sz1 = a->VendorID;
result2 = HidD_GetManufacturerString(hCom,buf,100);
if(result2 == FALSE)
{
AfxMessageBox("失败!");
}
result2 = HidD_GetProductString(hCom,buf2,100);
if(result2 == FALSE)
{
AfxMessageBox("失败!");
}
result2 = HidD_GetSerialNumberString(hCom,buf4,100);
if(result2 == FALSE)
{
AfxMessageBox("失败!");
}
while(1)
{
if(buf1[num-1] != '\0')
{
if(num == 0)
{
sprintf(buf1,"%s",(char *)buf);
}
else
{
sprintf(buf1,"%s%s",buf1,(char *)buf + (2*num));
}
num++;
}
else
{
break;
}
}
while(1)
{
if(buf3[num1-1] != '\0')
{
if(num1 == 0)
{
sprintf(buf3,"%s",(char *)buf2);
}
else
{
sprintf(buf3,"%s%s",buf3,(char *)buf2 + (2*num1));
}
num1++;
}
else
{
break;
}
}
while(1)
{
if(buf5[num2-1] == '\0')
{
if(num2 == 0)
{
sprintf(buf5,"%s",(char *)buf4);
}
else
{
sprintf(buf5,"%s%s",buf5,(char *)buf4 + (2*num1));
}
num2++;
}
else
{
if(num2 == 0)
{
buf5 = "Not specified";
}
break;
}
}
m_zf1 = buf1;
m_zf2 = buf3;
m_zf3 = buf5;
}
UpdateData(false);