VC HID设备

关于USB-HID开发:
======================================================
#include <dbt.h>
extern "C" {
// Declare the C libraries used
#include "setupapi.h"  // Must link in setupapi.lib
#include "hidsdi.h"   // Must link in hid.lib
}
#pragma comment(lib,"setupapi")
#pragma comment(lib,"hid")
======================================================
GUID DevGuid;
HidD_GetHidGuid(&DevGuid); //获取HID设备的GUID


//注册HID设备改变事件 WM_DEVICECHANGE ,
//MFC:ON_WM_DEVICECHANGE() OnDeviceChange(UINT nEventType, DWORD dwData)
//MFC :DEV_BROADCAST_DEVICEINTERFACE* dbd = (DEV_BROADCAST_DEVICEINTERFACE*)dwData;


static bool RegisterForUsbEvents(HANDLE handle, GUID *DevGuid) {
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
memset(&NotificationFilter, 0, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = *DevGuid;


HDEVNOTIFY   hDevNotify;
hDevNotify = RegisterDeviceNotification(handle, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
if (!hDevNotify)
{
//int   Err = GetLastError();
//
return false;
}
return true;
}
//查找哪一个HID设备
static CString FindDevice(int VendorID, int ProductID, int Flash_outputsize, int Flash_inputsize)
{
CString devicePath = L"";


GUID                             HidGuid;
HDEVINFO                         DevInfo;
SP_DEVICE_INTERFACE_DATA         DevData;
PSP_DEVICE_INTERFACE_DETAIL_DATA DevDetail;
ULONG                            Length;
int                              Index;
BOOL                             ok;
int DevCount = 0;
/* Get GUID for all System HIDs */


HidD_GetHidGuid(&HidGuid);


/* Get Device Information for all present devices */
DevInfo = SetupDiGetClassDevs(&HidGuid,
NULL,
NULL,
(DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)
);


DevData.cbSize = sizeof(DevData);


DevDetail = NULL;


Index = -1;


/* Scan all Devices */
do {
Index++;


/* Device Interface Element of a Device Information set */
ok = SetupDiEnumDeviceInterfaces(DevInfo,
0,
&HidGuid,
Index,
&DevData
);
if (!ok) break;


/* Get Device Interface Details - Get Length */
ok = SetupDiGetDeviceInterfaceDetail(DevInfo,
&DevData,
NULL,
0,
&Length,
NULL
);


/* Allocate memory for Device Detailed Data */
DevDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);


/* Set cbSize in the DevDetail structure */
DevDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);


/* Get Device Interface Details */
ok = SetupDiGetDeviceInterfaceDetail(DevInfo,
&DevData,
DevDetail,
Length,
NULL,
NULL
);
if (!ok)
{
free(DevDetail);
DevDetail = NULL;
continue;
}
devicePath = DevDetail->DevicePath;


CLog::Debug(__FILEW__, __FUNCTIONW__, __LINE__, devicePath);
free(DevDetail);
/* Create File for Device Read/Write */




CString search;

search.Format(L"vid_%04x&pid_%04x", VendorID, ProductID);
if (devicePath.Find(search) >= 0){
CLog::Debug(__FILEW__, __FUNCTIONW__, __LINE__, L"is ok");
break;
}
devicePath = L"";
} while (1);
SetupDiDestroyDeviceInfoList(DevInfo);
return devicePath;
}
///然后是CreateFile,WriteFile,ReadFile操作
//以下是读取信息
PHIDP_PREPARSED_DATA       PreparsedData;
_HIDD_ATTRIBUTES hidAttributes;
HidD_GetAttributes(m_hHandle, &hidAttributes);
if (HidD_GetPreparsedData(m_hHandle, &PreparsedData))// get windows to read the device data into an internal buffer
{
// extract the device capabilities from the internal buffer
HidP_GetCaps(PreparsedData, &Capabilities);
printf("InputReportByteLength:%d\r\n", Capabilities.InputReportByteLength);
HidD_FreePreparsedData(PreparsedData); // before we quit the funtion, we must free the internal buffer reserved in 


GetPreparsedData
}else // GetPreparsedData failed? Chuck an exception
{
CloseHandle(m_hHandle);
m_hHandle = NULL;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值