HID 程式設計

來源:互聯網
上載者:User
  

1 環境配置1.1 安裝NTDDK1.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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.