标题:
USB HID设备读写代码实现c++(4)
[打印本页]
作者:
yuyang911220
时间:
2015-12-24 21:59
标题:
USB HID设备读写代码实现c++(4)
bool MCDHid::findMCDHid()
{
int vendorID = VENDOR_ID;
int productID = PRODUCT_ID;
DWORD required;
GUID hidGuid;
HDEVINFO hDevInfo;
SP_DEVICE_INTERFACE_DATA devInfoData;
int memberIndex = 0;
LONG result;
bool lastDevice = false;
DWORD length;
PSP_DEVICE_INTERFACE_DETAIL_DATA detailData = NULL;
bool deviceDetected = false;
HidD_GetHidGuid(&hidGuid);
hDevInfo = SetupDiGetClassDevs
(&hidGuid,
NULL,
NULL,
DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
//当应用程序不再需要使用SetupDiGetClassDevs函数的传回值hDevInfo所指的数组时,应该调用SetupDiDeStroyDeviceInfoList函数来释放资源
if(hDevInfo == INVALID_HANDLE_VALUE)
{
//printf("Failed to open device handle!");
return false;
}
devInfoData.cbSize = sizeof(devInfoData);
do
{
result = SetupDiEnumDeviceInterfaces(
hDevInfo,
0,
&hidGuid,
memberIndex,
&devInfoData);
if (result != 0)
{
result = SetupDiGetDeviceInterfaceDetail(
hDevInfo,
&devInfoData,
NULL,
0,
&length,
NULL);
detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(length);
if(!detailData)
{
//printf("Device information allacation failed!");
return false;
}
detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
result = SetupDiGetDeviceInterfaceDetail(
hDevInfo,
&devInfoData,
detailData,
length,
&required,
NULL);
if(!result)
{
//printf("Device information reallocation failed!");
return false;
}
_hidDevice.HidDevice = CreateFile(
detailData->DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
_hidDevice.Attributes.Size = sizeof(HIDD_ATTRIBUTES);
result = HidD_GetAttributes(
_hidDevice.HidDevice,
&_hidDevice.Attributes);
if ((_hidDevice.Attributes.VendorID == vendorID) && (_hidDevice.Attributes.ProductID == productID))
{
int status;
PHIDP_PREPARSED_DATA PreparsedData ;
deviceDetected = true;
//获取USB设备的预解析数据
result = HidD_GetPreparsedData(_hidDevice.HidDevice,&PreparsedData);
if(!result)
{
//printf("无法获取USB设备的预解析数据!");
return false;
}
status= HidP_GetCaps(PreparsedData, &_hidDevice.Caps);
_fUsbCommRunning = true;
HidD_FreePreparsedData(PreparsedData);
PreparsedData = NULL;
}
else
{
//printf("没有找到设备!");
closeMCDHid();
}
free(detailData);
detailData = NULL;
}
else
{
lastDevice = true;
}
memberIndex = memberIndex + 1;
}while((lastDevice == false)&&(deviceDetected == false));
SetupDiDestroyDeviceInfoList(hDevInfo);
return deviceDetected;
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0