USB Device detection on Windows

Source: Internet
Author: User
Enumerating BusesThe driver for the host controller (HCD (host control Driver)). It is located between the USB host controller and the USB system software. You can use CreateFile to open the name " \\\\.\\hcd1 ", \\\\.\\HCD2 files to detect the HCD bus. After opening the handle, you can get drivename by passing the Ioctl_get_hcd_driverkey_name parameter DeviceIoControl. The CM_ series function can be used to traverse each node to get the corresponding driver description. You can get the root HUB on this host drive by calling Ioctl_usb_get_root_hub_name as a parameter DeviceIoControl. Open the name by CreateFile to " \\\\.\\hubname "form of the file to get the root of the handle of the hub, and then by passing the ioctl_usb_get_node_connection_information to the function DeviceIoControl to get the connection information of this hub , this information lets you know that there are several ports on the hub and the device connectivity for each port. You can also get the vid,pid of the connection port device, if any. If the hub port already has a device connection, you can pass the DeviceIoControl parameter ioctl_usb_get_node_connection_driverkey_name to get the attached device (pass the parameter as the hub handle and the current index value). , the index value starts at 1), if the device connected to the front-end port is another hub, then the name of the hub can be obtained by passing the parameter ioctl_usb_get_node_connection_name like Devioceiocontrol. You can then enumerate the information for this child hub.Find a Device The current device that meets the criteria can be obtained by PID or vid or Classguid or interfaceguid. typedef struct _SSDEVHANDLES {    dword dwdevscount;     hdevinfo Hdevinfoset;     sp_devinfo_data *pdevdatas; } ssdevhandles; HANDLE stdcall ssdevgetdevices (in LPCTSTR lpvid/* = null */, in LPCTSTR lppid/* = null */, &NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;                         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;;d wmemidx++)          {            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; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}     }     return PhDevs ;   and Setupdigetdeviceregistryproperty to get device information through the SETUPDI function Setupdigetdeviceinstanceid.   monitor USB Device plug Registering device notification events HANDLE stdcall Sspnpregisterdevicenotifytohwnd (in HWND hwnd) {&NBSP;&NBSP;&NBSP;&NBSP;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); &NBSP;&NBSP;&NBSP;&NBSP}     if (pdevicehandle->ncount <= 0)     {         ss_free (Pdevicehandle);         pdevicehandle = NULL; &NBSP;&NBSP;&NBSP;&NBSP}     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)   & nbsp;          Unregisterdevicenotification (pDeviceHandle-> HDEVNOTIFIES[NIDX]); &NBSP;&NBSP;&NBSP;&NBSP}     ss_free (Pdevicehandle);     pdevicehandle = NULL;     return TRUE; Anti-registration Device notification event BOOL stdcall sspnpunregisterdevicenotify (in HANDLE hdevnotify) {     Sspnpdevicenotifyhandle *pdevicehandle = (Sspnpdevicenotifyhandle *) hdevnotify;     if (Pdevicehandle = = NULL)       &n

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.