自定义HID类封装库
2024-01-24
191
0
头文件chid.h
#pragma once
#include <Windows.h>
#include <tchar.h>
class CHid
{
private:
HANDLE m_hDeivce;
public:
BOOL OpenHid(USHORT VID=0x0000,USHORT PID=0xF002,USHORT BCD=0x0100);
VOID CloseHid();
BOOL SetReport(PVOID pData, ULONG nLen);
};
`
类实现CHID.cpp
#include "CHid.h"
#include<windows.h>
#include <winioctl.h>
#include <setupapi.h>
#include <initguid.h>
#include <stdio.h>
#include<hidsdi.h>
#include<tchar.h>
#pragma comment(lib,"Setupapi.lib ")
#pragma comment(lib,"hid.lib ")
BOOL USBEnumDevice(OUT PTCHAR pDeviceName, IN LPGUID pGuid, IN int instance)
{
HDEVINFO info = SetupDiGetClassDevs((GUID*)pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
if (info == INVALID_HANDLE_VALUE)
{
printf("No HDEVINFO available for this GUID\n");
return FALSE;
}
// Get interface data for the requested instance
SP_INTERFACE_DEVICE_DATA ifdata;
ifdata.cbSize = sizeof(ifdata);
if (!SetupDiEnumDeviceInterfaces(info, NULL, (GUID*)pGuid, instance, &ifdata))
{
_tprintf(TEXT("No SP_INTERFACE_DEVICE_DATA available for this GUID instance\n"));
SetupDiDestroyDeviceInfoList(info);
return FALSE;
}
// Get size of symbolic link name
DWORD ReqLen;
SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL);
PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new TCHAR[ReqLen]);
if (ifDetail == NULL)
{
SetupDiDestroyDeviceInfoList(info);
return FALSE;
}
// Get symbolic link name
ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL))
{
SetupDiDestroyDeviceInfoList(info);
delete ifDetail;
return FALSE;
}
//printf("Symbolic link is %s\n", ifDetail->DevicePath);
memcpy(pDeviceName, ifDetail->DevicePath, _tcslen(ifDetail->DevicePath) * sizeof(TCHAR));
pDeviceName[_tcslen(ifDetail->DevicePath)] = TEXT('\0');
delete[] ifDetail;
SetupDiDestroyDeviceInfoList(info);
return TRUE;
}
BOOL CHid::OpenHid(USHORT VID, USHORT PID, USHORT BCD)
{
// CloseHid();
GUID hidGuid;
::HidD_GetHidGuid((LPGUID)&hidGuid); // 取HID设备GUID
for (int i = 0;; i++)
{
TCHAR strDevicePath[512] = { 0 };
if (!USBEnumDevice(strDevicePath, &hidGuid, i))
{
break;
}
HANDLE tmp_DeviceHandle = CreateFile(strDevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (tmp_DeviceHandle == INVALID_HANDLE_VALUE)
{
continue;
}
HIDD_ATTRIBUTES Attributes;
ZeroMemory(&Attributes, sizeof(Attributes));
Attributes.Size = sizeof(HIDD_ATTRIBUTES);
if (!HidD_GetAttributes(tmp_DeviceHandle, &Attributes))
{
CloseHandle(tmp_DeviceHandle);
continue;
}
if (Attributes.VendorID != VID || Attributes.ProductID != PID || Attributes.VersionNumber != BCD)
{
CloseHandle(tmp_DeviceHandle);
continue;
}
m_hDeivce = tmp_DeviceHandle;
return TRUE;
}
return FALSE;
}
VOID CHid::CloseHid()
{
if (m_hDeivce != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hDeivce);
}
m_hDeivce = INVALID_HANDLE_VALUE;
}
BOOL CHid::SetReport(PVOID pData, ULONG nLen)
{
ULONG rtn = 0;
return WriteFile(m_hDeivce, pData, nLen, &rtn, NULL);
}
HID人机交互QQ群:564808376
UAC音频QQ群:218581009
UVC相机QQ群:331552032
BOT&UASP大容量存储QQ群:258159197
STC-USB单片机QQ群:315457461
USB技术交流QQ群2:580684376
USB技术交流QQ群:952873936