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