Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 枚举USB设备
【标  题】:枚举USB设备
【关键字】:USB
【来  源】:http://blog.csdn.net/DL88250/archive/2006/12/08/1435347.aspx

枚举USB设备

Your Ad Here

下面是代码。。。。

// 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;
}


 

Enter键播放都是正常的,这是怎么回事?蓝色:【上一篇】
的是什么意思?如果你是说用FLASH做网址连:【下一篇】
【相关文章】
  • create usbfs on Solaris?
  • [原创]国内大部分的USBKey通过B/S方式(CAPICOM)产生数字签名的严重安全漏洞
  • Solaris USB callback的处理方法
  • 嵌入式设备LINUX USB slave驱动
  • 魔术方块型USB--USB Twister Hub
  • 疑似slax挂载usb的解决方法
  • Linux下USB内核之学习笔记
  • USB了解
  • Redhat-FC3,AS4下USB设备(有时候磁盘)挂载死机的预防代码!
  • 发现GDCA USBKey(电子钥匙)的CSP数字签名实现存在缺陷
  • 【随机文章】
  • statspack 快速安装及基本使用
  • 使用反射为指定的文件类型创建关联
  • Visual Studio.Net 内幕(1)
  • 手绘实例《胶彩篇》
  • JAVA/JSP学习系列之十一(简单的“抓网页”代码)
  • 与Ajax邂逅.
  • Google二进制文件搜索让数千恶意程序现原形
  • VS2005调试嵌入式WINCE目标设备程序的连接方式
  • 在PPC上实现编译ASP
  • VTC128/M-VTC128/W系列大型多媒体系统
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.