Advertisement
Guest User

New HIDAPI Enumeration for Windows

a guest
Mar 21st, 2017
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.00 KB | None | 0 0
  1. #include <windows.h>
  2. #include <devguid.h>    // for GUID_DEVCLASS_CDROM etc
  3. #include <setupapi.h>
  4. #include <cfgmgr32.h>   // for MAX_DEVICE_ID_LEN, CM_Get_Parent and CM_Get_Device_ID
  5. #define INITGUID
  6. #include <tchar.h>
  7. #include <stdio.h>
  8.  
  9. //#include "c:\WinDDK\7600.16385.1\inc\api\devpkey.h"
  10.  
  11. // include DEVPKEY_Device_BusReportedDeviceDesc from WinDDK\7600.16385.1\inc\api\devpropdef.h
  12. #ifdef DEFINE_DEVPROPKEY
  13. #undef DEFINE_DEVPROPKEY
  14. #endif
  15. #ifdef INITGUID
  16. #define DEFINE_DEVPROPKEY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8, pid) EXTERN_C const DEVPROPKEY DECLSPEC_SELECTANY name = { { l, w1, w2, { b1, b2,  b3,  b4,  b5,  b6,  b7,  b8 } }, pid }
  17. #else
  18. #define DEFINE_DEVPROPKEY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8, pid) EXTERN_C const DEVPROPKEY name
  19. #endif // INITGUID
  20.  
  21. // include DEVPKEY_Device_BusReportedDeviceDesc from WinDDK\7600.16385.1\inc\api\devpkey.h
  22. DEFINE_DEVPROPKEY(DEVPKEY_Device_BusReportedDeviceDesc, 0x540b947e, 0x8b40, 0x45bc, 0xa8, 0xa2, 0x6a, 0x0b, 0x89, 0x4c, 0xbd, 0xa2, 4);     // DEVPROP_TYPE_STRING
  23. DEFINE_DEVPROPKEY(DEVPKEY_Device_ContainerId, 0x8c7ed206, 0x3f8a, 0x4827, 0xb3, 0xab, 0xae, 0x9e, 0x1f, 0xae, 0xfc, 0x6c, 2);     // DEVPROP_TYPE_GUID
  24. DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14);    // DEVPROP_TYPE_STRING
  25. DEFINE_DEVPROPKEY(DEVPKEY_DeviceDisplay_Category, 0x78c34fc8, 0x104a, 0x4aca, 0x9e, 0xa4, 0x52, 0x4d, 0x52, 0x99, 0x6e, 0x57, 0x5a);  // DEVPROP_TYPE_STRING_LIST
  26. DEFINE_DEVPROPKEY(DEVPKEY_Device_LocationInfo, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 15);    // DEVPROP_TYPE_STRING
  27. DEFINE_DEVPROPKEY(DEVPKEY_Device_Manufacturer, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 13);    // DEVPROP_TYPE_STRING
  28. DEFINE_DEVPROPKEY(DEVPKEY_Device_SecuritySDS, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 26);    // DEVPROP_TYPE_SECURITY_DESCRIPTOR_STRING
  29.  
  30. #define ARRAY_SIZE(arr)     (sizeof(arr)/sizeof(arr[0]))
  31.  
  32. #pragma comment (lib, "setupapi.lib")
  33.  
  34. typedef BOOL(WINAPI *FN_SetupDiGetDevicePropertyW)(
  35.     __in       HDEVINFO DeviceInfoSet,
  36.     __in       PSP_DEVINFO_DATA DeviceInfoData,
  37.     __in       const DEVPROPKEY *PropertyKey,
  38.     __out      DEVPROPTYPE *PropertyType,
  39.     __out_opt  PBYTE PropertyBuffer,
  40.     __in       DWORD PropertyBufferSize,
  41.     __out_opt  PDWORD RequiredSize,
  42.     __in       DWORD Flags
  43.     );
  44.  
  45. // List all USB devices with some additional information
  46. void ListDevices()
  47. {
  48.     unsigned i, j;
  49.     DWORD dwSize, dwPropertyRegDataType;
  50.     DEVPROPTYPE ulPropertyType;
  51.     CONFIGRET status;
  52.     HDEVINFO hDevInfo;
  53.     SP_DEVINFO_DATA DeviceInfoData;
  54.     const static LPCTSTR arPrefix[3] = { TEXT("VID_"), TEXT("PID_"), TEXT("MI_") };
  55.     TCHAR szDeviceInstanceID[MAX_DEVICE_ID_LEN];
  56.     TCHAR szDesc[1024], szHardwareIDs[4096];
  57.     WCHAR szBuffer[4096];
  58.     LPTSTR pszToken, pszNextToken;
  59.     TCHAR szVid[MAX_DEVICE_ID_LEN], szPid[MAX_DEVICE_ID_LEN], szMi[MAX_DEVICE_ID_LEN];
  60.     FN_SetupDiGetDevicePropertyW fn_SetupDiGetDevicePropertyW = (FN_SetupDiGetDevicePropertyW)
  61.         GetProcAddress(GetModuleHandle(TEXT("Setupapi.dll")), "SetupDiGetDevicePropertyW");
  62.  
  63.     GUID InterfaceClassGuid = { 0x4d1e55b2, 0xf16f, 0x11cf,{ 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } };
  64.     hDevInfo = SetupDiGetClassDevsA(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  65.     if (hDevInfo == INVALID_HANDLE_VALUE)
  66.         return;
  67.  
  68.     // Find the ones that are driverless
  69.     for (i = 0; ; i++) {
  70.         DeviceInfoData.cbSize = sizeof(DeviceInfoData);
  71.         if (!SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData))
  72.             break;
  73.  
  74.         status = CM_Get_Device_ID(DeviceInfoData.DevInst, szDeviceInstanceID, MAX_PATH, 0);
  75.         if (status != CR_SUCCESS)
  76.             continue;
  77.  
  78.         // Display device instance ID
  79.         _tprintf(TEXT("%s\n"), szDeviceInstanceID);
  80.  
  81.         if (SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC,
  82.             &dwPropertyRegDataType, (BYTE*)szDesc,
  83.             sizeof(szDesc),   // The size, in bytes
  84.             &dwSize))
  85.             _tprintf(TEXT("    Device Description: \"%s\"\n"), szDesc);
  86.  
  87.         if (SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_HARDWAREID,
  88.             &dwPropertyRegDataType, (BYTE*)szHardwareIDs,
  89.             sizeof(szHardwareIDs),    // The size, in bytes
  90.             &dwSize)) {
  91.             LPCTSTR pszId;
  92.             _tprintf(TEXT("    Hardware IDs:\n"));
  93.             for (pszId = szHardwareIDs;
  94.                 *pszId != TEXT('\0') && pszId + dwSize / sizeof(TCHAR) <= szHardwareIDs + ARRAYSIZE(szHardwareIDs);
  95.                 pszId += lstrlen(pszId) + 1) {
  96.  
  97.                 _tprintf(TEXT("        \"%s\"\n"), pszId);
  98.             }
  99.         }
  100.  
  101.         // Retreive the device description as reported by the device itself
  102.         // On Vista and earlier, we can use only SPDRP_DEVICEDESC
  103.         // On Windows 7, the information we want ("Bus reported device description") is
  104.         // accessed through DEVPKEY_Device_BusReportedDeviceDesc
  105.         if (fn_SetupDiGetDevicePropertyW && fn_SetupDiGetDevicePropertyW(hDevInfo, &DeviceInfoData, &DEVPKEY_Device_BusReportedDeviceDesc,
  106.             &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {
  107.  
  108.             if (fn_SetupDiGetDevicePropertyW(hDevInfo, &DeviceInfoData, &DEVPKEY_Device_BusReportedDeviceDesc,
  109.                 &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0))
  110.                 _tprintf(TEXT("    Bus Reported Device Description: \"%ls\"\n"), szBuffer);
  111.             if (fn_SetupDiGetDevicePropertyW(hDevInfo, &DeviceInfoData, &DEVPKEY_Device_Manufacturer,
  112.                 &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {
  113.                 _tprintf(TEXT("    Device Manufacturer: \"%ls\"\n"), szBuffer);
  114.             }
  115.             if (fn_SetupDiGetDevicePropertyW(hDevInfo, &DeviceInfoData, &DEVPKEY_Device_FriendlyName,
  116.                 &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {
  117.                 _tprintf(TEXT("    Device Friendly Name: \"%ls\"\n"), szBuffer);
  118.             }
  119.             if (fn_SetupDiGetDevicePropertyW(hDevInfo, &DeviceInfoData, &DEVPKEY_Device_LocationInfo,
  120.                 &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {
  121.                 _tprintf(TEXT("    Device Location Info: \"%ls\"\n"), szBuffer);
  122.             }
  123.             if (fn_SetupDiGetDevicePropertyW(hDevInfo, &DeviceInfoData, &DEVPKEY_Device_SecuritySDS,
  124.                 &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {
  125.                 // See Security Descriptor Definition Language on MSDN
  126.                 // (http://msdn.microsoft.com/en-us/library/windows/desktop/aa379567(v=vs.85).aspx)
  127.                 _tprintf(TEXT("    Device Security Descriptor String: \"%ls\"\n"), szBuffer);
  128.             }
  129.             if (fn_SetupDiGetDevicePropertyW(hDevInfo, &DeviceInfoData, &DEVPKEY_Device_ContainerId,
  130.                 &ulPropertyType, (BYTE*)szDesc, sizeof(szDesc), &dwSize, 0)) {
  131.                 StringFromGUID2((REFGUID)szDesc, szBuffer, ARRAY_SIZE(szBuffer));
  132.                 _tprintf(TEXT("    ContainerId: \"%ls\"\n"), szBuffer);
  133.             }
  134.             if (fn_SetupDiGetDevicePropertyW(hDevInfo, &DeviceInfoData, &DEVPKEY_DeviceDisplay_Category,
  135.                 &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0))
  136.                 _tprintf(TEXT("    Device Display Category: \"%ls\"\n"), szBuffer);
  137.         }
  138.  
  139.         pszToken = _tcstok_s(szDeviceInstanceID, TEXT("\\#&"), &pszNextToken);
  140.         while (pszToken != NULL) {
  141.             szVid[0] = TEXT('\0');
  142.             szPid[0] = TEXT('\0');
  143.             szMi[0] = TEXT('\0');
  144.             for (j = 0; j < 3; j++) {
  145.                 if (_tcsncmp(pszToken, arPrefix[j], lstrlen(arPrefix[j])) == 0) {
  146.                     switch (j) {
  147.                     case 0:
  148.                         _tcscpy_s(szVid, ARRAY_SIZE(szVid), pszToken);
  149.                         break;
  150.                     case 1:
  151.                         _tcscpy_s(szPid, ARRAY_SIZE(szPid), pszToken);
  152.                         break;
  153.                     case 2:
  154.                         _tcscpy_s(szMi, ARRAY_SIZE(szMi), pszToken);
  155.                         break;
  156.                     default:
  157.                         break;
  158.                     }
  159.                 }
  160.             }
  161.             if (szVid[0] != TEXT('\0'))
  162.                 _tprintf(TEXT("    vid: \"%s\"\n"), szVid);
  163.             if (szPid[0] != TEXT('\0'))
  164.                 _tprintf(TEXT("    pid: \"%s\"\n"), szPid);
  165.             if (szMi[0] != TEXT('\0'))
  166.                 _tprintf(TEXT("    mi: \"%s\"\n"), szMi);
  167.             pszToken = _tcstok_s(NULL, TEXT("\\#&"), &pszNextToken);
  168.         }
  169.     }
  170.  
  171.     return;
  172. }
  173.  
  174. int _tmain()
  175. {
  176.     // List all connected USB devices
  177.     _tprintf(TEXT("---------------\n"));
  178.     _tprintf(TEXT("- HID devices -\n"));
  179.     _tprintf(TEXT("---------------\n"));
  180.     ListDevices();
  181.  
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement