Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1696933
  • 博文数量: 584
  • 博客积分: 13857
  • 博客等级: 上将
  • 技术积分: 11883
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-16 09:34

分类: WINDOWS

2012-02-28 16:00:16



  1. #include <usbscan.h>

  2. #include <setupapi.h>

  3. extern "C"

  4. {

  5. #include <hidsdi.h>

  6. }

  7. #pragma comment(lib, "setupapi.lib")

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

  9. 打开设备
  10. 四个模块中,打开设备是最复杂的。因为它需要通过其设备类来枚举设备树上的所有设备,从而得到相匹配的设备,进而得到设备名

  11. BOOL DeviceOpen(HANDLE &handle, WORD wVID, WORD wPID)

  12. {

  13.      BOOL bRet = FALSE;

  14.      GUID hidGuid;

  15.      HDEVINFO hardwareDeviceInfo;

  16.      SP_INTERFACE_DEVICE_DATA deviceInfoData;

  17.      PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData = NULL;

  18.      ULONG predictedLength = 0;

  19.      ULONG requiredLength = 0;

  20.      CloseHandle(handle);

  21.      handle = INVALID_HANDLE_VALUE;

  22.      deviceInfoData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);

  23.      HidD_GetHidGuid(&hidGuid);

  24.      hardwareDeviceInfo = SetupDiGetClassDevs(&hidGuid, NULL, NULL, (DIGCF_PRESENT|DIGCF_DEVICEINTERFACE));

  25.      for (int i=0; i<128; i++)

  26.      {

  27.          if (!SetupDiEnumDeviceInterfaces(hardwareDeviceInfo, 0, &hidGuid, i, &deviceInfoData)) continue;

  28.          SetupDiGetDeviceInterfaceDetail(hardwareDeviceInfo, &deviceInfoData, NULL, 0, &requiredLength, NULL);

  29.          predictedLength = requiredLength;

  30.          functionClassDeviceData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(predictedLength);

  31.          if (!functionClassDeviceData) continue;

  32.          functionClassDeviceData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

  33.          if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfo, &deviceInfoData, functionClassDeviceData, predictedLength, &requiredLength, NULL))break;

  34.          handle = CreateFile(functionClassDeviceData->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

  35.          if (handle != INVALID_HANDLE_VALUE)

  36.          {

  37.              HIDD_ATTRIBUTES attri;

  38.              HidD_GetAttributes(handle, &attri);

  39.              if ((attri.VendorID == wVID) &&

  40.                  (attri.ProductID == wPID))

  41.              {

  42.                  bRet = TRUE;

  43.                  break;

  44.              }

  45.              CloseHandle(handle);

  46.              handle = INVALID_HANDLE_VALUE;

  47.          }

  48.      }

  49.      SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);

  50.      return bRet;

  51. }

关闭设备
关闭设备比较简单,只需要直接使用函数CloseHandle即可

  1. void DeviceClose(HANDLE &handle)

  2. {

  3.      CloseHandle(handle);

  4.      handle = INVALID_HANDLE_VALUE;

  5. }


写数据
假设HID的Report大小为8字节,且第一字节为ID

  1. BOOL DeviceWrite(HANDLE handle, LPCVOID lpBuffer, DWORD dwSize)

  2. {

  3.      BYTE wBuffer[8] = {0};

  4.      DWORD dwRet;

  5.      BOOL bRet;

  6.      wBuffer[0] = 0x01;

  7.      wBuffer[1] = 0x00;

  8.      memcpy(&wBuffer[2], lpBuffer, min(6, dwSize));

  9.      bRet = WriteFile(handle, wBuffer, 8, &dwRet, NULL);

  10.      return bRet;

  11. }


读数据

  1. BOOL DeviceRead(HANDLE handle, LPVOID lpBuffer, DWORD dwSize)

  2. {

  3.      BYTE rBuffer[8] = {0};

  4.      DWORD dwRet;

  5.      BOOL bRet;

  6.      rBuffer[0] = 0x01;

  7.      rBuffer[1] = 0xff;

  8.      bRet = WriteFile(handle, rBuffer, 8, &dwRet, NULL);

  9.      bRet &= ReadFile(handle, rBuffer, 8, &dwRet, NULL);

  10.      memcpy(lpBuffer, &rBuffer[1], min(7, dwSize));

  11.      return bRet;

  12. }



阅读(2612) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~