Windows上USB裝置檢測

來源:互聯網
上載者:User
枚舉匯流排 主機控制器的驅動程式(HCD(Host Control Driver))。它位於USB主機控制器與USB系統軟體之間。 可以用CreateFile開啟名字為" \\\\.\\HCD1", \\\\.\\HCD2的檔案來檢測HCD匯流排。 開啟控制代碼之後可以通過 DeviceIoControl 傳遞IOCTL_GET_HCD_DRIVERKEY_NAME 參數來得到DriveName。 並可以用 CM_系列函數遍曆各節點來得到相應的驅動描述。 可以通過調用 IOCTL_USB_GET_ROOT_HUB_NAME 為參數的DeviceIoControl 來得到此主機磁碟機上的根HUB。 通過CreateFile開啟名字為" \\\\.\\HUBNAME"形式的檔案,來得到根HUB的控制代碼,然後通過將IOCTL_USB_GET_NODE_CONNECTION_INFORMATION 傳遞給函數DeviceIoControl來得到此HUB的串連資訊,通過這個資訊可以知道此HUB上有幾個連接埠,以及每個連接埠的裝置串連情況。還能得到串連連接埠裝置的VID,PID,如果有的話。 如果HUB連接埠已經有裝置串連,可以通過DeviceIoControl傳遞參數IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME 來得到串連裝置的情況(傳遞參數為Hub控制代碼和當前的索引值,索引值要從1開始),如果當前連接埠串連的裝置為另外一個HUB,那麼可以通過像DevioceIoControl傳遞參數IOCTL_USB_GET_NODE_CONNECTION_NAME 來得到此HUB的名字,然後就可以枚舉得到此子HUB的資訊了。   尋找裝置 通過PID或者VID或者ClassGUID或者InterfaceGUID,可以得到合格當前裝置。 typedef struct _SSDevHandles {     DWORD dwDevsCount;     HDEVINFO hDevInfoSet;     SP_DEVINFO_DATA *pDevDatas; } SSDevHandles; HANDLE STDCALL SSDevGetDevices(IN LPCTSTR lpVid /* = NULL */, IN LPCTSTR lpPID /* = NULL */,                              IN const GUID* pSetupClassGuid /* = NULL */, IN const GUID* pInterfaceClassGuid /* = NULL */) {     SSDevHandles *phDevs = NULL;     HDEVINFO hDevInfoSet = INVALID_HANDLE_VALUE;     check_int_begin     {         if(lpVid != NULL && _tcslen(lpVid) == 0)             lpVid = NULL;         if(lpPID != NULL && _tcslen(lpPID) == 0)             lpPID = NULL;         hDevInfoSet = SetupDiCreateDeviceInfoList(pSetupClassGuid, NULL);         check_int_bool(hDevInfoSet != INVALID_HANDLE_VALUE, SS_RC_NOT_FOUND);         if(pInterfaceClassGuid == NULL)             hDevInfoSet = SetupDiGetClassDevsEx(NULL, NULL, NULL,                 DIGCF_ALLCLASSES|DIGCF_DEVICEINTERFACE|DIGCF_PRESENT,                 hDevInfoSet, NULL, NULL);         else             hDevInfoSet = SetupDiGetClassDevsEx(pInterfaceClassGuid, NULL, NULL,                 DIGCF_DEVICEINTERFACE|DIGCF_PRESENT,                 hDevInfoSet, NULL, NULL);         check_int_bool(hDevInfoSet != INVALID_HANDLE_VALUE, SS_RC_NOT_FOUND);         SP_DEVINFO_DATA dtDevInfo = { sizeof(SP_DEVINFO_DATA) };         for(DWORD dwMemIdx = 0; ;dwMemIdx++)         {             if(!SetupDiEnumDeviceInfo(hDevInfoSet, dwMemIdx, &dtDevInfo))             {                 if(::GetLastError() == ERROR_NO_MORE_ITEMS)                 {                     break;                 }                 continue;;             }             if(lpVid != NULL || lpPID != NULL)             {                 TCHAR szInstanceId[MAX_INSTANCE_ID] = {0};                 SetupDiGetDeviceInstanceId(hDevInfoSet, &dtDevInfo, szInstanceId, SS_DIMOF(szInstanceId), NULL);                 if(_tcslen(szInstanceId) <= 0)                     continue;                 TCHAR szVID[5] = {0}, szPID[5] = {0};                 if(!SSDevUtilGetVIDPID(szInstanceId, szVID, SS_DIMOF(szVID), szPID, SS_DIMOF(szPID)))                     continue;                 if(lpVid != NULL && _tcsicmp(lpVid, szVID) != 0)                     continue;                 if(lpPID != NULL && _tcsicmp(lpPID, szPID) != 0)                     continue;             }             DWORD dwCapbilities = 0;             if(!SetupDiGetDeviceRegistryProperty(hDevInfoSet, &dtDevInfo, SPDRP_CAPABILITIES, NULL,\                 (BYTE*)&dwCapbilities, sizeof(DWORD), NULL))                 continue;             if(!SS_FLAG_ISSET(dwCapbilities, CM_DEVCAP_REMOVABLE))                 continue;             if(phDevs == NULL)             {                 phDevs = (SSDevHandles *)SS_MALLOC(sizeof(SSDevHandles));                 memset(phDevs, 0, sizeof(SSDevHandles));                 phDevs->hDevInfoSet = INVALID_HANDLE_VALUE;             }             phDevs->pDevDatas = (SP_DEVINFO_DATA*)SS_REALLOC(phDevs->pDevDatas, (phDevs->dwDevsCount + 1) * sizeof(SP_DEVINFO_DATA));             MP_ASSERT(phDevs->pDevDatas != NULL);             phDevs->pDevDatas[phDevs->dwDevsCount] = dtDevInfo;             phDevs->dwDevsCount++;         }     }     check_int_finally     {         if(phDevs != NULL && phDevs->dwDevsCount > 0)         {             phDevs->hDevInfoSet = hDevInfoSet;             hDevInfoSet = INVALID_HANDLE_VALUE;         }         if(hDevInfoSet != INVALID_HANDLE_VALUE)         {             SetupDiDestroyDeviceInfoList(hDevInfoSet);             hDevInfoSet = INVALID_HANDLE_VALUE;         }     }     return phDevs; }   並通過setupDi函數SetupDiGetDeviceInstanceId ,SetupDiGetDeviceRegistryProperty 來得到裝置資訊。   監控USB裝置插拔 註冊裝置通知事件 HANDLE STDCALL SSPNPRegisterDeviceNotifyToHwnd(IN HWND hWnd) {     if(!::IsWindow(hWnd))         return NULL;     SSPNPDeviceNotifyHandle *pDeviceHandle = (SSPNPDeviceNotifyHandle *)SS_MALLOC(sizeof(SSPNPDeviceNotifyHandle));     memset(pDeviceHandle, 0, sizeof(SSPNPDeviceNotifyHandle));     DEV_BROADCAST_DEVICEINTERFACE NotificationFilter = {0};     NotificationFilter.dbcc_size = sizeof(NotificationFilter);     NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;     for(int nIdx=0; nIdx<SS_DIMOF(GUID_DEVINTERFACE_LIST); nIdx++)     {         NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_LIST[nIdx];         pDeviceHandle->hDevNotifies[pDeviceHandle->nCount++] = ::RegisterDeviceNotification(hWnd,             &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);     }     if(pDeviceHandle->nCount <= 0)     {         SS_FREE(pDeviceHandle);         pDeviceHandle = NULL;     }     return (HANDLE)pDeviceHandle; } BOOL STDCALL SSPNPUnregisterDeviceNotify(IN HANDLE hDevNotify) {     SSPNPDeviceNotifyHandle *pDeviceHandle = (SSPNPDeviceNotifyHandle *)hDevNotify;     if(pDeviceHandle == NULL)         return FALSE;     for(int nIdx=0; nIdx<SS_DIMOF(GUID_DEVINTERFACE_LIST); nIdx++)     {         if(pDeviceHandle->hDevNotifies[nIdx] != NULL)              UnregisterDeviceNotification(pDeviceHandle->hDevNotifies[nIdx]);     }     SS_FREE(pDeviceHandle);     pDeviceHandle = NULL;     return TRUE; } 反註冊裝置通知事件 BOOL STDCALL SSPNPUnregisterDeviceNotify(IN HANDLE hDevNotify) {     SSPNPDeviceNotifyHandle *pDeviceHandle = (SSPNPDeviceNotifyHandle *)hDevNotify;     if(pDeviceHandle == NULL)       &n
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.