#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);
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
c/c++ windows 通过hidsdi进行串口通信 GUID InterfaceClassGuid = { 0x4d1e55b2, 0xf16f, 0x11cf, { 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } }; SP_DEVINFO_DATA devinfo_data; SP_DEVICE_INTERFACE_DATA device_interface_data; SP_DEVICE_INTERFACE_DETAIL_DATA_A* device_interface_detail_data = NULL; HDEVINFO device_info_set = INVALID_HANDLE_VALUE; int device_index = 0; HDEVINFO DeviceInfoSet; DeviceInfoSet = SetupDiCreateDeviceInfoList(NULL, NULL); devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
资源详情
资源评论
资源推荐
收起资源包目录
HIDComm.zip (13个子文件)
HIDComm
HIDComm.cpp 4KB
framework.h 159B
HIDComm.h 2KB
HidListen.cpp 6KB
HIDComm.vcxproj.user 168B
dllmain.cpp 445B
HidHandle.h 1KB
pch.cpp 158B
HidListen.h 57B
HIDComm.vcxproj.filters 2KB
HIDComm.vcxproj 8KB
HidHandle.cpp 4KB
pch.h 544B
共 13 条
- 1
u010816417
- 粉丝: 5
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 适用于 Android、Java 和 Kotlin Multiplatform 的现代 I,O 库 .zip
- 高通TWS蓝牙规格书,做HIFI级别的耳机用
- Qt读写Usb设备的数据
- 这个存储库适合初学者从 Scratch 开始学习 JavaScript.zip
- AUTOSAR 4.4.0版本Rte模块标准文档
- 25考研冲刺快速复习经验.pptx
- MATLAB使用教程-初步入门大全
- 该存储库旨在为 Web 上的语言提供新信息 .zip
- 考研冲刺的实用经验与技巧.pptx
- Nvidia GeForce GT 1030-GeForce Studio For Win10&Win11(Win10&Win11 GeForce GT 1030显卡驱动)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0