一些电子DIY们喜欢用游戏手柄来做控制,我把整理好的代码贴出了,供大家参考:
///
#include <stdio.h>
#include <windows.h>
#include <wxp/hidsdi.h>
#include <wxp/hidpi.h>
#include <setupapi.h>
#pragma comment(lib, "setupapi.lib")
#pragma comment(lib, "hid.lib")
int main(int argc, char *argv[])
{
GUID HidGuid;
HDEVINFO hDevInfo;
DWORD MemberIndex = 0;
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
BOOL bSuccess = FALSE;
PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetailData;
DWORD Length = 0;
HANDLE hDeviceHandle;
HIDD_ATTRIBUTES Attributes;
WCHAR mString[256];
TCHAR Buffer[256];
PHIDP_PREPARSED_DATA pHidpPreparsedData;
HIDP_CAPS hidPCaps;
NTSTATUS status;
BOOL find_flag = FALSE;
DWORD nReadBytes = 0;
BYTE *pInputReport;
unsigned int i;
HidD_GetHidGuid(&HidGuid);
printf("GUID:%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x/n",
HidGuid.Data1, HidGuid.Data2, HidGuid.Data3, HidGuid.Data4[0],
HidGuid.Data4[1],HidGuid.Data4[2],HidGuid.Data4[3],HidGuid.Data4[4],
HidGuid.Data4[5],HidGuid.Data4[6],HidGuid.Data4[7]);
hDevInfo = SetupDiGetClassDevs(&HidGuid, NULL,NULL,DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if(hDevInfo == INVALID_HANDLE_VALUE)
{
printf("符合HID规范的USB设备发生错误/n");
return -1;
}
printf("正在查找游戏手柄.../n");
DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
do
{
bSuccess = SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &HidGuid, MemberIndex, &DeviceInterfaceData);
if (!bSuccess)
{
if(MemberIndex == 0)
{
printf("没有找到可用的USB设备!/n");
}
else
{
printf("没有更多的可用的USB设备!/n");
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return -1;
}
printf("找到了一个USB设备:/n");
SetupDiGetDeviceInterfaceDetail(hDevInfo, &DeviceInterfaceData, NULL, 0, &Length, NULL);
pDeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);
pDeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &DeviceInterfaceData,
pDeviceInterfaceDetailData, Length, NULL, NULL))
{
printf("查找路径设备时出错!/n");
}
else
{
printf("设备路径:%s/n",pDeviceInterfaceDetailData->DevicePath );
}
hDeviceHandle = CreateFile(pDeviceInterfaceDetailData->DevicePath , GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDeviceHandle == INVALID_HANDLE_VALUE)
{
printf("打开设备路径出错!/n");
}
else
{
HidD_GetAttributes(hDeviceHandle,&Attributes);
//将有关该设备的标识显示出来
printf("供应商ID/t:0X%04X/n",Attributes.VendorID);
printf("产品ID/t:0X%04X/n",Attributes.ProductID);
printf("产品版本号:0X%04X/n",Attributes.VersionNumber);
}
if(Attributes.VendorID == 0x0079 && Attributes.ProductID == 0x0006)
{
printf("Found my device!!!/n");
find_flag = TRUE;
break;
}
//Sleep(1000);
MemberIndex++;
}while(bSuccess);
if(find_flag == TRUE)
{
HidD_GetManufacturerString(hDeviceHandle,mString,sizeof(mString));
if (wcstombs(Buffer,mString,256) == -1)
{
Buffer[0] = 0;
}
printf("生产商:/t%s/n",Buffer);
HidD_GetProductString(hDeviceHandle,mString,sizeof(mString));
if (wcstombs(Buffer,mString,256) == -1)
{
Buffer[0] = 0;
}
printf("产品名称:/t%s/n",Buffer);
if (!HidD_GetPreparsedData(hDeviceHandle,&pHidpPreparsedData))
{
printf("获取 HID PREPAREDDATA 失败!/n");
return -1;
}
status = HidP_GetCaps(pHidpPreparsedData,&hidPCaps);
if (status == HIDP_STATUS_SUCCESS)
{
printf("CAP信息如下:/n");
printf(" InputReportByteLength %d/n", hidPCaps.InputReportByteLength);
printf(" OutputReportByteLength %d/n", hidPCaps.OutputReportByteLength);
}
pInputReport = malloc(hidPCaps.InputReportByteLength);
memset(pInputReport,0,hidPCaps.InputReportByteLength);
do
{
ReadFile(hDeviceHandle,pInputReport,hidPCaps.InputReportByteLength,&nReadBytes,NULL);
if (hidPCaps.InputReportByteLength == nReadBytes)
{
for(i=0; i<(nReadBytes-1);i++)
{
printf("%02x-",pInputReport[i]);
}
printf("%02x/r",pInputReport[nReadBytes-1]);
}
if (pInputReport[nReadBytes-2] == 0x20)
{
printf("/n");
break;
}
Sleep(10);
}while(hidPCaps.InputReportByteLength == nReadBytes);
}
CloseHandle(hDeviceHandle);
return 0;
}
/
运行结果如下图: