C # Trouver le numéro de logement spécifique du concentrateur USB (10 logements) auquel USB est connecté ou non. Je veux obtenir l’emplacement spécifique où l’USB est connecté ou non

C # J’ai un hub USB de 10 emplacements USB connecté à mon port USB. Je souhaite que le périphérique USB soit connecté au port spécifique.
Exemple: deux clés USB sont connectées aux logements 3 et 7. Par conséquent, je veux une liste indiquant que les logements 3 et 7 ont un port USB et que le logement restant est vide. J’ai essayé d’utiliser WMI Query Win32_USBHub. Mais ici, je ne reçois que 6 identifiants de périphérique et non pas 10. Je différencie les ports en utilisant un VID commun pour l’identifiant de périphérique. Mais toujours même après avoir connecté les clés USB au port spécifique. Je veux obtenir leur emplacement correspondant dans lequel ils sont connectés à USBHub. Je ne parviens pas à identifier le logement dans lequel le port USB est connecté et où le logement est vide.

ManagementObjectCollection collection; var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"); collection = searcher.Get(); 

J’essaierais d’extraire location information de location information du périphérique USB (comme dans le gestionnaire de périphériques) … Je ne code pas en C # ni en WMI, mais vous devriez pouvoir obtenir ce type d’informations avec setupapi.h qui fait partie de winapi. (Je pense) je le fais comme ça en C ++ / VCL :

 #include  bool USBinfo() { int i,n; AnsiSsortingng s,txt=""; DWORD dwSize,dwPropertyRegDataType; HDEVINFO hDevInfo; SP_DEVINFO_DATA DeviceInfoData; TCHAR szDesc[1024]; // List all connected USB devices hDevInfo = SetupDiGetClassDevs(NULL, TEXT("USB"), NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES); if (hDevInfo == INVALID_HANDLE_VALUE) return false; for (i=0;;i++) { DeviceInfoData.cbSize = sizeof(DeviceInfoData); if (!SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData)) break; SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC,&dwPropertyRegDataType, (BYTE*)szDesc,sizeof(szDesc),&dwSize); s=szDesc; n=48; while (s.Length()n) s=s.SubSsortingng(1,n); txt+=s+" "; // this just set constant ssortingng size to allign the columns to n chars SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_HARDWAREID,&dwPropertyRegDataType, (BYTE*)szDesc,sizeof(szDesc),&dwSize); s=szDesc; if (s=="USB\\VID_????&PID_????REV_????") { // here you can do custom stuff for specific VID,PID just change the ???? in above line to your specific VID,PID,REV } s=szDesc; n=64; while (s.Length()n) s=s.SubSsortingng(1,n); txt+=s+" "; SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_LOCATION_INFORMATION,&dwPropertyRegDataType, (BYTE*)szDesc,sizeof(szDesc),&dwSize); s=szDesc; n=64; while (s.Length()n) s=s.SubSsortingng(1,n); txt+=s+" "; txt+="\r\n"; } Main->mm_log->Lines->Add(txt); // this just output txt ssortingng to memo return true; } 

Voici la sortie sur ma machine:

 USB Root Hub USB\ROOT_HUB&VID1022&PID7807&REV0011 USB\ROOT_HUB&VID1022&PID7807&REV0011 USB Root Hub USB\ROOT_HUB&VID1022&PID7807&REV0011 USB\ROOT_HUB&VID1022&PID7807&REV0011 USB Root Hub USB\ROOT_HUB&VID1022&PID7809&REV0011 USB\ROOT_HUB&VID1022&PID7809&REV0011 USB Root Hub USB\ROOT_HUB20&VID1022&PID7808&REV0011 USB\ROOT_HUB20&VID1022&PID7808&REV0011 USB Root Hub USB\ROOT_HUB20&VID1022&PID7808&REV0011 USB\ROOT_HUB20&VID1022&PID7808&REV0011 USB Composite Device USB\VID_048D&PID_9006&REV_0200 Port_#0001.Hub_#0004 IT9135 BDA Device USB\VID_048D&PID_9006&REV_0200&MI_00 0000.0013.0002.001.000.000.000.000.000 USB Input Device USB\VID_048D&PID_9006&REV_0200&MI_01 0000.0013.0002.001.000.000.000.000.000 Canon LiDE 30 USB\VID_04A9&PID_220E&REV_0100 Port_#0005.Hub_#0001 American Power Conversion USB UPS USB\VID_051D&PID_0002&REV_0106 Port_#0001.Hub_#0001 USB Input Device USB\Vid_093A&Pid_2510&Rev_0100 USB Optical Mouse USB Input Device USB\VID_413C&PID_2107&REV_0115 Port_#0002.Hub_#0001 

Comme vous pouvez le voir, la dernière colonne (3ème) contient les informations souhaitées. Recherchez dans setupapi.h toutes les définitions SPDRP_ vous pouvez utiliser … La seule chose utilisée à partir de la VCL est AnsiSsortingng . Changez-la donc pour n’importe quel type de chaîne dont vous disposez.

Ceci n’est pas limité à l’ USB . Si vous voulez que tous les périphériques, changez le paramètre de recherche TEXT("USB") en NULL

 hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES);