#include "pch.h"
#include "HidListen.h"
#include <Windows.h>
#include <tchar.h>
#include <Dbt.h>
#include <setupapi.h>
#include <iostream>
#include "HidHandle.h"
#include <cstring>
using namespace std;
#pragma comment (lib, "Kernel32.lib")
#pragma comment (lib, "User32.lib")
#define THRD_MESSAGE_EXIT WM_USER + 1
const _TCHAR CLASS_NAME[] = _T("Sample Window Class");
HWND hWnd;
HANDLE hThread;
static const GUID GUID_DEVINTERFACE_LIST[] =
{
// GUID_DEVINTERFACE_USB_DEVICE
{ 0xA5DCBF10, 0x6530, 0x11D2,{ 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } },
// GUID_DEVINTERFACE_DISK
{ 0x53f56307, 0xb6bf, 0x11d0,{ 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b } },
// GUID_DEVINTERFACE_HID,
{ 0x4D1E55B2, 0xF16F, 0x11CF,{ 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } },
// GUID_NDIS_LAN_CLASS
{ 0xad498944, 0x762f, 0x11d0,{ 0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c } } ,
//// GUID_DEVINTERFACE_COMPORT //
{ 0x86e0d1e0, 0x8089, 0x11d0,{ 0x9c, 0xe4, 0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73 } },
//// GUID_DEVINTERFACE_SERENUM_BUS_ENUMERATOR //
{ 0x4D36E978, 0xE325, 0x11CE,{ 0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18 } },
//// GUID_DEVINTERFACE_PARALLEL //
{ 0x97F76EF0, 0xF883, 0x11D0,{ 0xAF, 0x1F, 0x00, 0x00, 0xF8, 0x00, 0x84, 0x5C } },
//// GUID_DEVINTERFACE_PARCLASS //{ 0x811FC6A5, 0xF728, 0x11D0, { 0xA5, 0x37, 0x00, 0x00, 0xF8, 0x75, 0x3E, 0xD1 } }
};
void UpdateDevice(PDEV_BROADCAST_DEVICEINTERFACE pDevInf, WPARAM wParam)
{
char dbcc_name[1024] = { 0 };
WideCharToMultiByte(CP_ACP, 0, pDevInf->dbcc_name, -1, dbcc_name, sizeof(dbcc_name), NULL, NULL);
string szDevId(dbcc_name);
if (DBT_DEVICEARRIVAL == wParam)
{
if ((int)(szDevId.find("HID")) >= 0)
{
map<string, HidHandle*>::iterator it = Pens.find(szDevId.c_str());
if (it != Pens.end()) return;
HidHandle* hid = new HidHandle();
strcpy(hid->ID, szDevId.c_str());
Pens.insert(pair<string, HidHandle*>(szDevId.c_str(), hid));
HANDLE handle = CreateFileA(szDevId.c_str(),
GENERIC_WRITE | GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if (handle == INVALID_HANDLE_VALUE) {
handle = CreateFileA(szDevId.c_str(),
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
}
if ((long long)handle < 0) return;
HIDD_ATTRIBUTES attrib;
attrib.Size = sizeof(HIDD_ATTRIBUTES);
HidD_GetAttributes(handle, &attrib);
CloseHandle(handle);
hid->PID = attrib.ProductID;
hid->VID = attrib.VendorID;
if (OnHIDDeviceStatusChangeCallBack != NULL)
{
OnHIDDeviceStatusChangeCallBack(hid->VID, hid->PID,szDevId.c_str(), true);
}
}
}
else
{
if ((int)(szDevId.find("HID")) >= 0)
{
map<string, HidHandle*>::iterator it = Pens.find(szDevId.c_str());
if (it == Pens.end()) return;
if (OnHIDDeviceStatusChangeCallBack != NULL)
{
OnHIDDeviceStatusChangeCallBack(it->second->VID,it->second->PID, szDevId.c_str(), false);
}
delete(it->second);
it->second = NULL;
Pens.erase(szDevId.c_str());
}
}
}
LRESULT DeviceChange(UINT message, WPARAM wParam, LPARAM lParam)
{
if (DBT_DEVICEARRIVAL == wParam || DBT_DEVICEREMOVECOMPLETE == wParam)
{
PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)lParam;
PDEV_BROADCAST_DEVICEINTERFACE pDevInf;
PDEV_BROADCAST_HANDLE pDevHnd;
PDEV_BROADCAST_OEM pDevOem;
PDEV_BROADCAST_PORT pDevPort;
PDEV_BROADCAST_VOLUME pDevVolume;
switch (pHdr->dbch_devicetype)
{
case DBT_DEVTYP_DEVICEINTERFACE:
pDevInf = (PDEV_BROADCAST_DEVICEINTERFACE)pHdr;
UpdateDevice(pDevInf, wParam);
break;
case DBT_DEVTYP_HANDLE:
pDevHnd = (PDEV_BROADCAST_HANDLE)pHdr;
break;
case DBT_DEVTYP_OEM:
pDevOem = (PDEV_BROADCAST_OEM)pHdr;
break;
case DBT_DEVTYP_PORT:
pDevPort = (PDEV_BROADCAST_PORT)pHdr;
break;
case DBT_DEVTYP_VOLUME:
pDevVolume = (PDEV_BROADCAST_VOLUME)pHdr;
break;
}
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
break;
case WM_SIZE:
break;
case WM_DEVICECHANGE:
return DeviceChange(message, wParam, lParam);
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
ATOM MyRegisterClass()
{
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = CLASS_NAME;
return RegisterClass(&wc);
}
bool CreateMessageOnlyWindow()
{
hWnd = CreateWindowEx(0, CLASS_NAME, _T(""), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, // Parent window
NULL, // Menu
GetModuleHandle(NULL), // Instance handle
NULL // Additional application data
);
return hWnd != NULL;
}
void RegisterDeviceNotify()
{
HDEVNOTIFY hDevNotify;
for (int i = 0; i < sizeof(GUID_DEVINTERFACE_LIST) / sizeof(GUID); i++)
{
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory(&NotificationFilter, sizeof(NotificationFilter));
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_LIST[i];
hDevNotify = RegisterDeviceNotification(hWnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
}
}
DWORD WINAPI ThrdFunc(LPVOID lpParam)
{
if (0 == MyRegisterClass()) return -1;
if (!CreateMessageOnlyWindow()) return -1;
RegisterDeviceNotify();
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (msg.message == THRD_MESSAGE_EXIT)
{
cout << "worker receive the exiting Message..." << endl;
return 0;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
int SatartListen() {
DWORD iThread;
hThread = CreateThread(NULL, 0, ThrdFunc, NULL, 0, &iThread);
if (hThread == NULL) {
cout << "error" << endl;
return -1;
}
return 0;
}
void StopListen() {
TerminateThread(hThread, 0);
CloseHandle(hThread);
}
u010816417
- 粉丝: 5
- 资源: 7
最新资源
- html+css+js网页设计 美食 美食天下2个页面(里面包含php和mysql)
- API For Docker 一个基于多种编程语言开源免费不限制提供生活常用,出行服务,开发工具,金融服务,通讯服务和公益大数据的平台.zip
- 基于uniapp智能小程序商城源码(小程序毕业设计完整源码+LW).zip
- BasicOCR是一个致力于解决自然场景文字识别算法研究的项目 该项目由长城数字大数据应用技术研究院佟派AI团队发起和维护 .zip
- 毕设-java-swing-学生选课成绩信息管理系统(ER图文档+视频+源码)7.zip
- Bonree Ants流式大数据处理引擎.zip
- 基于小程序的超市购物系统代码源码(小程序毕业设计完整源码+LW).zip
- 毕设-java-swing-图书管管理系统(视频+源码)5.zip
- 基于模糊滑模控制器Fuzzy-SMC的永磁同步电机FOC 1.转速环采用Fuzzy-SMC控制器; 2.控制器参数使用PSO算法进行在线寻优,降低调参难度; 3.提供算法对应的参考文献和仿真模型
- 基于小程序的高校就业招聘系统的设计与实现源码(小程序毕业设计完整源码+LW).zip
- C#毕业设计-基于ASP.NET的图书管理系统源码+数据库(高分毕设)
- CCF-大数据精准营销中搜狗用户画像挖掘.zip
- 基于小程序的公考学习平台的设计与实现源码(小程序毕业设计完整源码+LW).zip
- ibm 磁带带库驱动程序
- 游泳溺水识别数据集,对25729张图片进行PACICAL VOC XML格式的标注,溺水平均识别率在89.9%
- 激光熔覆传热传质,浓度场,可观察不同元素分布已经扩散情况 程序中包含固体传热,层流等模块
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0