下面是代碼。。。。
// DeviceManager.cpp : 定義控制台應用程式的進入點。
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <setupapi.h>
#define DIRECTINPUT_VERSION 0x0800
#include <Dinput.h>
extern "C"
{
#include <hidsdi.h>
}
#include <tchar.h>
#pragma comment(lib, "Dinput8.lib")
#pragma comment(lib, "Dinput.lib")
#pragma comment(lib, "Setupapi.lib")
#pragma comment(lib, "hid.lib")
GUID myGuid;
LPDIRECTINPUT8 m_lpDI; // DI8介面指標
LPDIENUMDEVICESCALLBACK DIEnumDevicesCallback(const DIDEVICEINSTANCE* lpddi, VOID* pvRef)
{
*(GUID*) pvRef = lpddi->guidInstance;
return DIENUM_STOP;
}
int main(void)
{
GUID HidGuid;
// 尋找本系統中HID類的GUID標識
HidD_GetHidGuid(&HidGuid);
printf("系統中HID類的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]);
// 準備尋找符合HID規範的USB裝置
HDEVINFO hDevInfo = SetupDiGetClassDevs(
&HidGuid,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
printf("符合HID規範的USB裝置發生錯誤 /n");
return 0;
}
printf("正在尋找可用的USB裝置。。。。 /n");
DWORD MemberIndex = 0;
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
BOOL bSuccess = FALSE;
DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
do
{
bSuccess = SetupDiEnumDeviceInterfaces(
hDevInfo,
NULL,
&HidGuid,
MemberIndex,
&DeviceInterfaceData);
if ((!bSuccess) && (GetLastError() == ERROR_NO_MORE_ITEMS))
{
if(MemberIndex == 0)
{
printf("抱歉,未找到可用的USB裝置! /n");
}
else
{
printf("沒有更多的可用的USB裝置! /n");
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
printf("找到了一個USB裝置: /n");
// 若找到了一個USB裝置,則擷取該裝置的細節資訊
PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetailData;
DWORD Length = 0;
SetupDiGetDeviceInterfaceDetail(
hDevInfo,
&DeviceInterfaceData,
NULL,
0,
&Length,
NULL);
pDeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);
pDeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); //MUST BE!!!
if (!SetupDiGetDeviceInterfaceDetail(
hDevInfo,
&DeviceInterfaceData,
pDeviceInterfaceDetailData,
Length,
NULL,
NULL))
{
printf("尋找路徑裝置時出錯! /n");
}
else
{
printf("裝置路徑:%s ", pDeviceInterfaceDetailData->DevicePath);
}
//開啟裝置控制代碼
HANDLE 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");
// printf("%d", GetLastError());
}
else
{
HIDD_ATTRIBUTES Attributes;
HidD_GetAttributes(hDeviceHandle,&Attributes);
/**************************************************/
/*
long result;
int ReadBuffer[10];
int flag;
do
{
ReadBuffer[9] = 0;
ReadBuffer[8] = 0;
ReadBuffer[7] = 0;
ReadBuffer[6] = 0;
ReadBuffer[5] = 0;
ReadBuffer[4] = 0;
ReadBuffer[3] = 0;
ReadBuffer[2] = 0;
ReadBuffer[1] = 0;
ReadBuffer[0] = 0;
result = HidD_GetFeature(
hDeviceHandle,
(PVOID)ReadBuffer,
0x09);
// for (int i = 0; i < ReadBuffer[1]; i++)
{
//printf("%d", ReadBuffer[i]);
}
}
//scanf("%d", &flag);
while(1);
*/
HRESULT hr;
HINSTANCE hInstance = GetModuleHandle(NULL) ;//擷取執行個體控制代碼;
//建立DirectInput介面
if(NULL == m_lpDI)
{
hr = DirectInput8Create(hInstance,
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void**)&m_lpDI, //介面取值
NULL);
hr = m_lpDI->EnumDevices(DI8DEVCLASS_GAMECTRL,
(LPDIENUMDEVICESCALLBACK)DIEnumDevicesCallback, //回呼函數
&myGuid, //賦值GUID
DIEDFL_ATTACHEDONLY); //掃描安裝好的和串連好的裝置
}
/**************************************************/
//將有關該裝置的標識顯示出來
printf("供應商ID :0X%04X ",Attributes.VendorID);
printf("產品ID :0X%04X ",Attributes.ProductID);
printf("產品版本號碼:0X%04X ",Attributes.VersionNumber);
WCHAR mString[256];
TCHAR Buffer[256];
HidD_GetManufacturerString(hDeviceHandle,mString,sizeof(mString));
if (wcstombs((char *)Buffer,mString,256) == -1) // fail
{
Buffer[0] = NULL;
}
printf("生產商: %s ",Buffer);
HidD_GetProductString(hDeviceHandle,mString,sizeof(mString));
if (wcstombs((char *)Buffer,mString,256) == -1)
{
Buffer[0] = NULL;
}
printf("產品名稱: %s ",Buffer);
// 通訊:
PHIDP_PREPARSED_DATA pHidpPreparsedData;
HIDP_CAPS hidPCaps;
if (!HidD_GetPreparsedData(hDeviceHandle, &pHidpPreparsedData))
{
printf("擷取 HID PREPARED DATA 失敗! /n");
return 0;
}
NTSTATUS status = HidP_GetCaps(pHidpPreparsedData,&hidPCaps);
if (status == HIDP_STATUS_SUCCESS)
{
printf("CAP資訊如下: /n");
printf(" InputReportByteLength %d ", hidPCaps.InputReportByteLength);
printf(" OutputReportByteLength %d ", hidPCaps.OutputReportByteLength);
}
DWORD nReadBytes = 0;
BYTE *pInputReport = new BYTE[hidPCaps.InputReportByteLength];
memset(pInputReport,0,hidPCaps.InputReportByteLength);
do
{
ReadFile(hDeviceHandle,
pInputReport,
hidPCaps.InputReportByteLength,
&nReadBytes,
NULL);
if (hidPCaps.InputReportByteLength == nReadBytes)
{
for (unsigned int i = 0; i< nReadBytes - 1;i++)
{
printf("%02x-",pInputReport[i]);
}
printf("%02x ",pInputReport[nReadBytes-1]);
}
// if (pInputReport[nReadBytes-2] == 0x20) //break the loop when pressing a specific key
{
// printf(" /n");
break;
}
Sleep(10);
}while (hidPCaps.InputReportByteLength == nReadBytes);
// 釋放控制代碼資源
CloseHandle(hDeviceHandle);
}
MemberIndex++;
}while(bSuccess);
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}