虚拟键盘鼠标UVKM自义HID接口
2023-06-12
425
0
这里可以参考一个Windows自定义HID接口来实现:
HID.h
#pragma once
#include <Windows.h>
typedef struct _MOUSE_STRUCT
{
UCHAR ReportId;//A0
union
{
UCHAR Value;
struct
{
UCHAR LeftBtn : 1;
UCHAR RightBtn : 1;
UCHAR Reseved : 6;
}u;
}btn;
UCHAR Horizontal;
UCHAR Veritical;
UCHAR Wheel;
}MOUSE_STRUCT;
typedef struct _GMOUSE_STRUCT
{
UCHAR ReportId; //A2
union
{
UCHAR Button;
struct
{
UCHAR LeftBtn : 1;
UCHAR RightBtn : 1;
UCHAR Reseved : 6;
}u;
}btn;
USHORT X;
USHORT Y;
}GMOUSE_STRUCT;
typedef struct _KEYBOARD_STRUCT
{
UCHAR ReportId;//A1
union
{
UCHAR Value;
struct
{
UCHAR LeftCtrl : 1;
UCHAR LeftShift : 1;
UCHAR LeftAlt : 1;
UCHAR LeftGui : 1;
UCHAR RightCtrl : 1;
UCHAR RightShift : 1;
UCHAR RightlAlt : 1;
UCHAR RightGui : 1;
}u;
}Fun;
UCHAR Reserved;
UCHAR Key[6];
}KEYBOARD_STRUCT;
class CHid
{
private:
HANDLE m_hDeivce;
public:
BOOL OpenHid(USHORT VID=0x0000,USHORT PID=0xF001,USHORT BCD=0x0100);
VOID CloseHid();
BOOL SetReport(PVOID pData, ULONG nLen);
};
HID.CPP
#include "pch.h"
#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)
{
if (nLen > 254)
{
return FALSE;
}
UCHAR data[256] = { 0 };
data[0] = 0x05; //ID
data[1] = nLen; //length
CopyMemory(&data[2], pData, nLen);
ULONG rtn = 0;
return WriteFile(m_hDeivce, data, sizeof(data), &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