// You can view the situation when you press the game handle button.
// USB interface game handle
// Compilation environment: Windows 2000 Server VC 6.0 Win2k DDK
#include
Void main () {guid hidguid;
/ / Find the GUID ID of the HID class in this system hidd_gethidguid; _tprintf ("" The GUID identity of the HID class in the system is:% 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]); // Prepare to find the HID specification USB device HDEVINFO HDEVINFO = SetupdiGetClassdevs (& HidGuid, Null, Null, Digcf_Present | Digcf_deviceinterface); if (hdevinfo == invalid_handle_value) {_tprintf ("" Errors that meet the HID specification USB device error / N "); Return;}
_tprintf ("Find available USB Devices ... / 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) _tprintf ("Sorry, no USB device available! / n"); else _tprintf ("There is no more available USB device! / n");
SetupDiDestroyDeviceInfoList (hDevInfo); return;} _tprintf ( "find a USB device: / n"); // if a USB device is found, obtaining PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetailData details of the apparatus; 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)) _tprintf ("Find a Find path device! / N"); ELSE _tprintf ("Device Path:% S / N", PDeviceInterfaceDetAildata-> DevicePath); // Open Device Handle 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) _tprintf ( "Error opening device path / n!"); Else {HIDD_ATTRIBUTES Attributes; HidD_GetAttributes (hDeviceHandle, & Attributes); // about the identification device is displayed _tprintf ( "Supplier ID / T: 0x% 04x / n ", attributes.vendorid; _tprintf (" Product ID / T: 0x% 04x / n ", Attributes.ProductId; _tprintf (" Product version number: 0x% 04x / n ", Attributes.versionNumber; wchar mstring [256]; tchar buffer [256];
HIDD_GETMANUFACTURERSTRING (HDeviceHandle, Mstring, Sizeof (MString)); IF (WCSTombs (Buffer, Mstring, 256) == -1) // Fail Buffer [0] = NULL; _TPRINTF ("Manufacturer: / T% S / N" , Buffer; hidd_getproductstring (HDeviceHandle, Mstring, Sizeof (MString)); IF (WCSTombs (Buffer, Mstring, 256) == -1) Buffer [0] = NULL; _TPRINTF ("Product Name: / T% S / N ", Buffer); // communication: PHIDP_PREPARSED_DATA pHidpPreparsedData; HIDP_CAPS hidPCaps; if (HidD_GetPreparsedData (hDeviceHandle, & pHidpPreparsedData!)) {_tprintf (" Get HID PREPARED DATA failed / n "); return;!} NTSTATUS status = HidP_GetCaps (pHidpPreparsedData, & hidPCaps); if (status == HIDP_STATUS_SUCCESS) {_tprintf ( "CAP information is as follows: / n"); _tprintf ( "InputReportByteLength% d / n", hidPCaps.InputReportByteLength); _tprintf ( "OutputReportByteLength% d / n", hidPCaps. OutputReportBytelength; DWord NreadBytes = 0; byte * PinputReport = New Byte [HIDPCAPS.INPUTREPORTBYTLENGTH]; MEMSET (PinputReport, 0, Hidpcaps.InputReportbytelength; do {read File (hDeviceHandle, pInputReport, hidPCaps.InputReportByteLength, & nReadBytes, NULL); if (hidPCaps.InputReportByteLength == nReadBytes) {for (unsigned int i = 0; i <(nReadBytes-1); i ) _tprintf ( "% 02x-" PinputReport [i]); _tprintf ("% 02x / r", pinputreport [nucedBytes-1]);} IF (PinputReport [NreadBytes-2] == 0x20) // Break the loop when pressing a specific key {_tprintf ( "/ n"); Break;} Sleep (10);} while (hidpcaps.inputreportbytelength == nreadbytes);
// Release the handle resource closehandle (HDeviceHandle);} MemberIndex ;} while (bsuccess);