HID查找设备
关键是查找设备,读写设备就用句柄用readfile,writefile就行
查找设备:
m_infor=_T("");
// TODO: Add your control notification handler code here
GUID guidHID;
HidD_GetHidGuid(&guidHID);
CString strShow;
strShow.Format("%08x-%04x-%04x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\r\n",
guidHID.Data1,guidHID.Data2,guidHID.Data3,guidHID.Data4[0],
guidHID.Data4[1],guidHID.Data4[2],guidHID.Data4[3],guidHID.Data4[4],
guidHID.Data4[5],guidHID.Data4[6],guidHID.Data4[7]);
m_infor+= _T("1.HID类的GUID标识为:\r\n ")+strShow+_T("\r\n");
m_infor+=_T("2.获得全部HID信息……\r\n ");
HDEVINFO hDevInfo;
hDevInfo=SetupDiGetClassDevs(&guidHID,NULL,NULL,DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE) // 查询全部HID信息失败
{
m_infor+=_T("查询失败 - SetupDiGetClassDevs()");
return ;
}
m_infor=m_infor+_T("获得成功!\r\n")+_T("\r\n")+_T("3.获得特定HID识别信息……\r\n ");
SP_INTERFACE_DEVICE_DATA ifdata;
ifdata.cbSize = sizeof(ifdata);
DWORD devindex;
for (devindex = 0;SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &guidHID,devindex, &ifdata); ++devindex)
{
CString num;
num.Format(_T("第(%d)个设备:\r\n "),devindex+1);
m_infor+=num;
DWORD needed;
SetupDiGetDeviceInterfaceDetail(hDevInfo, &ifdata, NULL, 0,&needed, NULL);
PSP_INTERFACE_DEVICE_DETAIL_DATA detail = (PSP_INTERFACE_DEVICE_DETAIL_DATA) malloc(needed);
detail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
SP_DEVINFO_DATA did = {sizeof(SP_DEVINFO_DATA)};
SetupDiGetDeviceInterfaceDetail(hDevInfo, &ifdata, detail,needed, NULL, &did);
CString devPath(detail->DevicePath);
m_infor+=_T("设备路径:")+devPath+_T("\r\n ");
///////////////////////////////////////////////////////
TCHAR fname[256];
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &did,SPDRP_FRIENDLYNAME, NULL, (PBYTE) fname, sizeof(fname), NULL)
&& !SetupDiGetDeviceRegistryProperty(hDevInfo, &did, SPDRP_DEVICEDESC,NULL, (PBYTE) fname, sizeof(fname), NULL))
_tcsncpy(fname, detail->DevicePath, 256);
fname[255] = 0;
m_infor+=_T(" 设备类型:");
m_infor+=fname;
m_infor+=_T("\r\n ");
//CDeviceListEntry e(detail->DevicePath, fname);
free((PVOID) detail);
////////////////////////////////////////////////////////
HANDLE hand1 = CreateFile(devPath, GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
if (hand1 == INVALID_HANDLE_VALUE)
{
m_infor+=_T(" CreateFile() - 打开失败!\r\n ");
continue;
}
else
{
m_infor+=_T(" CreateFile() - 打开成功!\r\n ");
}
HIDD_ATTRIBUTES attr = {sizeof(HIDD_ATTRIBUTES)};
BOOLEAN okay = HidD_GetAttributes(hand1, &attr);
if (okay)
{
CString tem;
tem.Format("ProductID:%x ,VendorID:%X ,VersionNumber: %x",attr.ProductID,attr.VendorID,attr.VersionNumber);
m_infor+=_T(" ")+tem+_T("\r\n ");
if (attr.ProductID==0x100 && attr.VendorID==0x930A)
{
hand=hand1;
m_receive.EnableWindow();
m_send.EnableWindow();
m_close.EnableWindow();
}
}
///////////////////////////////////////////////////////获得设备能力
m_infor+=_T(" 获得设备能力……\r\n ");
PHIDP_PREPARSED_DATA PreparsedData;
if(HidD_GetPreparsedData(hand1,&PreparsedData))
{
m_infor+=_T(" HidD_GetPreparsedData() - 调用成功! \r\n ");
}
##############################################
PHIDP_PREPARSED_DATA PreparsedData;
if(HidD_GetPreparsedData(hDevHandle,&PreparsedData))AddToInfOut("HidD_GetPreparsedData成功调用。");
HIDP_CAPS Cap;
HidP_GetCaps(PreparsedData,&Cap);
结构定义
Public Type HIDP_CAPS
Usage As Integer ‘ 用法
UsagePage As Integer ‘ 用法页
InputReportByteLength As Integer ‘ 输入报表长度
OutputReportByteLength As Integer ‘ 输出报表长度
FeatureReportByteLength As Integer ‘ 特征报表长度
Reserved(16) As Integer
NumberLinkCollectionNodes As Integer ‘ 由函数HidP_GetLinkCollectionNodes 返回的顶层集合连接数目
NumberInputButtonCaps As Integer ‘ 由函数HidP_GetButtonCaps返回的包含输入按钮能力的结构HIDP_BUTTON_CAPS的数目
NumberInputValueCaps As Integer ‘ 由函数HidP_GetValueCaps返回的包含输入数值能力的结构HIDP_VALUE_ CAPS的数目
NumberInputDataIndices As Integer ‘ 在输入报表中有关按键和数值的数据指示器(Data Indices)的数目
NumberOutputButtonCaps As Integer ‘ 由函数HidP_GetButtonCaps返回的包含输出按钮能力的结构HIDP_BUTTON_CAPS的数目
NumberOutputValueCaps As Integer ‘ 由函数HidP_GetValueCaps返回的包含输出数值能力的结构HIDP_VALUE_ CAPS的数目
NumberOutputDataIndices As Integer ‘ 在输出报表中有关按键和数值的数据指示器(Data Indices)的数目
NumberFeatureButtonCaps As Integer ‘ 由函数HidP_GetButtonCaps返回的包含特征按钮能力的结构HIDP_BUTTON_CAPS的数目
NumberFeatureValueCaps As Integer ‘ 由函数HidP_GetValueCaps返回的包含特征数值能力的结构HIDP_VALUE_ CAPS的数目
NumberFeatureDataIndices As Integer ‘ 在特征报表中有关按键和数值的数据指示器(Data Indices)的数目
End Type
##############################################
Cap=new HIDP_CAPS;
HidP_GetCaps(PreparsedData,Cap);
m_infor+=_T(" HidP_GetCaps() - 调用成功! \r\n");
CString tmp;
tmp.Format("\
Usage:%d \r\n\
UsagePage:%d \r\n\
InputReportByteLength:%d \r\n\
OutputReportByteLength:%d \r\n\
FeatureReportByteLength:%d \r\n\
NumberLinkCollectionNodes:%d \r\n\
NumberInputButtonCaps:%d \r\n\
NumberInputValueCaps:%d \r\n\
NumberInputDataIndices:%d \r\n\
NumberOutputButtonCaps:%d \r\n\
NumberOutputValueCaps:%d \r\n\
NumberOutputDataIndices:%d \r\n\
NumberFeatureButtonCaps:%d \r\n\
NumberFeatureValueCaps:%d \r\n\
NumberFeatureDataIndices:%d \r\n",
Cap->Usage,
Cap->UsagePage,
Cap->InputReportByteLength,
Cap->OutputReportByteLength,
Cap->FeatureReportByteLength,
Cap->NumberLinkCollectionNodes,
Cap->NumberInputButtonCaps,
Cap->NumberInputValueCaps,
Cap->NumberInputDataIndices,
Cap->NumberOutputButtonCaps,
Cap->NumberOutputValueCaps,
Cap->NumberOutputDataIndices,
Cap->NumberFeatureButtonCaps,
Cap->NumberFeatureValueCaps,
Cap->NumberFeatureDataIndices);
m_infor+=tmp;
/////////////////////////////////////////////////////////
}
SetupDiDestroyDeviceInfoList(hDevInfo);
UpdateData(FALSE);
浙公网安备 33010602011771号