All about Programming

HID device query/find/send data (USB장치에 데이터 보내기)

민토즈 2020. 10. 6. 14:12
300x250

 

For Windows - C based

#include <cfgmgr32.h>
extern "C" 

{ 
   #include<hidsdi.h> 
}

#pragma comment (lib, "hid.lib")
#pragma comment (lib, "Cfgmgr32.lib")

// These are the default device attributes set in the driver which are used to identify the device. 
// 찾으려고 하는 장치의 vendor id와 product id
#define HIDMINI_DEFAULT_PID              0x5678 
#define HIDMINI_DEFAULT_VID              0x1234

int main()
{
   GUID hidguid;
   HANDLE file = INVALID_HANDLE_VALUE; CONFIGRET cr = CR_SUCCESS;
   PWSTR deviceInterfaceList = NULL;
   ULONG deviceInterfaceListLength = 0;
   PWSTR currentInterface;
   BOOLEAN bRet = FALSE;
   HANDLE devHandle = INVALID_HANDLE_VALUE;
   PHIDP_PREPARSED_DATA Ppd;     // The opaque parser info describing this device
   HIDP_CAPS  Caps;                     // The Capabilities of this hid device.
   HIDD_ATTRIBUTES attr;               // Device attributes
   LPBYTE buffer = NULL;

   // The HidD_GetHidGuid routine returns the device interfaceGUID for HIDClass devices.
   HidD_GetHidGuid(&hidguid);
   
   cr = CM_Get_Device_Interface_List_Size(
                                  &deviceInterfaceListLength,
                                  InterfaceGuid,
                                  NULL,
                                  CM_GET_DEVICE_INTERFACE_LIST_PRESENT);

   if (cr != CR_SUCCESS)
      return 0;
   if (deviceInterfaceListLength <= 1)
      return 0;    
 
   deviceInterfaceList = (PWSTR)malloc(deviceInterfaceListLength * sizeof(WCHAR));
   if (deviceInterfaceList == NULL) 
      return 0;

   cr = CM_Get_Device_Interface_ListW(
                                   InterfaceGuid,
                                   NULL,
                                   deviceInterfaceList,
                                   deviceInterfaceListLength,
                                   CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
   if (cr != CR_SUCCESS) {
      free(deviceInterfaceList); 
      return 0;
   }

   for (currentInterface = deviceInterfaceList;
        *currentInterface;
        currentInterface += wcslen(currentInterface) + 1) {

      devHandle = CreateFileW(currentInterface,
                                GENERIC_READ | GENERIC_WRITE,
                                FILE_SHARE_READ | FILE_SHARE_WRITE,
                                NULL, // no SECURITY_ATTRIBUTES structure
                                OPEN_EXISTING, // No special create flags
                                0, // No special attributes
                                NULL); // No template file

      if (INVALID_HANDLE_VALUE == devHandle) 
         continue;


      if (!HidD_GetAttributes(devHandle, &attr))
         continue;
      
      if ((attr.VendorID != HIDMINI_DEFAULT_VID) || (attr.ProductID != HIDMINI_DEFAULT_PID))
         continue;
      
      if (!HidD_GetPreparsedData (file, &Ppd))
         continue;

      if (!HidP_GetCaps (Ppd, &Caps))
      {
         HidD_FreePreparsedData (Ppd);
         continue;
      }
     
     // found my device
     // allocate buffer & fill the buffer
     // WriteFile 
     CloseHandle(devHandle);
     break;

   free(deviceInterfaceList); 
}

 

 

300x250