VC HID设备
2016-03-31 10:47
330 查看
关于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;
}
======================================================
#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;
}
相关文章推荐
- java 通配符解惑
- 简单了解一下--谓词(NSPredicate)
- C# 关键字var
- Android ListView的OnItemClickListener详解-id跟position的区别
- 有关linux下redis overcommit_memory的问题
- 页面执行顺序
- HTML九宫格布局实现方法
- Linux下面安装虚拟机
- VS批量删注释,代码保护第一步
- float在ie浏览器上的兼容问题
- 子序列的和
- STM32F103RBT的TFT---UC_GUI简单移植
- UIAlertController的用法
- 手把手教你打造一个心电图效果View Android自定义View
- Asp调用存储过程,command.CreateParameter 参数值的类型说明
- 【ios学习记录】-如何定制UITableView的圆角单元格
- 减面工具搜索整理(待补充)
- jQuery fadeIn() 方法
- 2016.03.30回顾
- 原码, 反码和补码.