HID 程序设计

来源:互联网 发布:淘宝源代码怎么用 编辑:程序博客网 时间:2024/05/17 22:41
  
1 环境配置
1.1 安装NTDDK
1.2 设置VC++6.0的配置
工具->选择->目录
1.3 程序引用
#include <windows.h>
 
extern "C" {
#include <hidsdi.h>
#include <hidpi.h>
#include <setupapi.h>
}
 
#pragma comment(lib,"setupapi.lib")
#pragma comment(lib,"hid.lib")
 
不加#include <windows.h>的后果:
c:/program files/microsoft visual studio/vc98/include/rpcndr.h(58) : fatal error C1189: #error : incorrect <rpcndr.h> version. Use the header that matches with the MIDL compiler.
不加extern “C” {}的后果:
编译通过,构造出错,hid.obj : error LNK2001: unresolved external symbol "void __stdcall HidD_GetHidGuid(struct _GUID *)" (?HidD_GetHidGuid@@YGXPAU_GUID@@@Z)
Debug/hid.exe : fatal error LNK1120: 1 unresolved externals
不加#pragma comment(lib,"hid.lib") 的后果:hid.obj : error LNK2001: unresolved external symbol _HidD_GetHidGuid@4
 
 
 
代码
/*名称:GetDevPath
 功能:获取设备路径
 参数:MemberIndex 设备列表的索引,txtpath文本缓存,以字符串保存设备路径
 返回:-2表示结束,-1 表示不能获取GUID,0表示未找到设备,>0表示成功,且返回值为路径字符串长度
*/
int HidDevice::GetDevPath(DWORD MemberIndex,char *txtpath)
{
    int    ret;
    DWORD Length,Required;
   
    //获GUID
    HidD_GetHidGuid(&HidGuid);
    hDevInfo=SetupDiGetClassDevs(&HidGuid,NULL,NULL,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
    if (hDevInfo == INVALID_HANDLE_VALUE)
    {
       return -1;
    }
    //枚举设备
    ret=SetupDiEnumDeviceInterfaces(hDevInfo,0,&HidGuid,MemberIndex,&devInfoData);
    if (!ret)
       if(MemberIndex==1)
       {
       // MessageBox(NULL,"未找到任何可用USB设备!",NULL,NULL);
           return 0;
       }
       else
           return -2;
    //获detaildata结构数组的长度
    SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInfoData,NULL,0,&Length,NULL);
    detailData=(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);
    detailData->cbSize=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
    //获detailData
    ret=SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInfoData,detailData,Length,&Required,NULL);
    strcpy(txtpath,detailData->DevicePath);
 
    return strlen(txtpath);
}
/*名称:GetProductInfo
 功能:获取usb的hid设备的信息
 参数:vendorid供应商,productid产品id,vernum版本号,manufaturer制造商,产品
 返回:1
*/
int HidDevice::GetProductInfo(char *vendorid,char *productid,char *vernum,char *manufaturer,char *product)
{
    HANDLE        hDeviceHandle;
    HIDD_ATTRIBUTES Attributes;
    WCHAR         mString[256];
 
    hDeviceHandle = CreateFile(detailData->DevicePath ,
                            GENERIC_READ | GENERIC_WRITE,
                            FILE_SHARE_READ | FILE_SHARE_WRITE,
                            NULL,
                            OPEN_EXISTING,
                            0,
                            NULL);
    if(hDeviceHandle == INVALID_HANDLE_VALUE)
       return 0;
    HidD_GetAttributes(hDeviceHandle,&Attributes);
    //将有关该设备的标识显示出来
    sprintf(vendorid,"0x%04X",Attributes.VendorID);
    sprintf(productid,"0x%04X",Attributes.ProductID);
    sprintf(vernum,"0x%04X",Attributes.VersionNumber);
    HidD_GetManufacturerString(hDeviceHandle,mString,sizeof(mString));
    if (wcstombs(manufaturer,mString,256) == -1) // fail
       manufaturer[0] = NULL;
    HidD_GetProductString(hDeviceHandle,mString,sizeof(mString));
    if (wcstombs(product,mString,256) == -1)
           product[0] = NULL;
 
    CloseHandle(hDeviceHandle);
    return 1;
}
原创粉丝点击