- 1 year ago
Capture mouse HID packets on windows using C++?
To capture mouse HID packets on Windows using C++, you can use the WinUSB API. The following steps outline a general approach to accomplish this:
Use the
SetupDiGetClassDevs
function to get a handle to the USB devices connected to the system.Use the
SetupDiEnumDeviceInterfaces
function to enumerate the interfaces of each USB device.Use the
SetupDiGetDeviceInterfaceDetail
function to retrieve the details of each USB device interface.Use the
CreateFile
function to obtain a handle to the USB device interface.Use the
DeviceIoControl
function to send an IOCTL to the USB device interface, which enables the device to send HID packets.Use the
ReadFile
function to read the HID packets sent by the USB device.
Here is some sample code that demonstrates the above approach:
c++#include <Windows.h>
#include <SetupAPI.h>
#include <Usb100.h>
#pragma comment(lib, "SetupAPI.lib")
int main()
{
// Get a handle to the USB devices connected to the system.
HDEVINFO deviceInfoSet = SetupDiGetClassDevs(&GUID_DEVCLASS_USB, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (deviceInfoSet == INVALID_HANDLE_VALUE)
{
// Handle error.
return 1;
}
// Enumerate the interfaces of each USB device.
SP_DEVICE_INTERFACE_DATA deviceInterfaceData = { 0 };
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInterfaces(deviceInfoSet, NULL, &GUID_DEVCLASS_USB, i, &deviceInterfaceData); i++)
{
// Retrieve the details of the USB device interface.
DWORD requiredSize = 0;
SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, NULL, 0, &requiredSize, NULL);
PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR, requiredSize);
deviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, deviceInterfaceDetailData, requiredSize, NULL, NULL))
{
// Handle error.
LocalFree(deviceInterfaceDetailData);
continue;
}
// Obtain a handle to the USB device interface.
HANDLE deviceHandle = CreateFile(deviceInterfaceDetailData->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
if (deviceHandle == INVALID_HANDLE_VALUE)
{
// Handle error.
LocalFree(deviceInterfaceDetailData);
continue;
}
// Send an IOCTL to the USB device interface to enable the device to send HID packets.
ULONG bytesReturned = 0;
HID_XFER_PACKET hidXferPacket = { 0 };
if (!DeviceIoControl(deviceHandle, IOCTL_HID_SET_FEATURE, &hidXferPacket, sizeof(HID_XFER_PACKET), NULL, 0, &bytesReturned, NULL))
{
// Handle error.
CloseHandle(deviceHandle);
LocalFree(deviceInterfaceDetailData);
continue;
}
// Read the HID packets sent by the USB device.
UCHAR buffer[1024] = { 0 };
DWORD bytesRead = 0;
while (true)
{
OVERLAPPED overlapped = { 0 };
overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!ReadFile(deviceHandle, buffer, sizeof