我有一个由10个USB插槽连接到我的USB端口的USB集线器。我想让USB设备连接到特定的端口。
例:两个USB连接在插槽3和插槽7。所以,我想要一个列表,显示插槽3和插槽7有USB和rest插槽是空的。
我尝试过使用WMI查询Win32_USBHub。但在这里,我只得到6个设备ID,而不是10个。我使用用于设备ID的公共VID来区分端口。
但即使在USB连接到特定端口之后。我想得到它们连接到USBHub的相应插槽。
我无法识别USB连接的插槽和插槽是空的。
ManagementObjectCollection collection;
var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub");
collection = searcher.Get();
发布于 2017-05-29 07:17:27
我会尝试从USB设备中提取(与设备管理器中的相同).我不使用C#或WMI编写代码,但是您应该能够使用作为winapi一部分的setupapi.h
获得此类信息,我认为这是在C++/VCL中这样做的。
#include <setupapi.h>
bool USBinfo()
{
int i,n;
AnsiString 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+=" "; if (s.Length()>n) s=s.SubString(1,n); txt+=s+" "; // this just set constant string 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+=" "; if (s.Length()>n) s=s.SubString(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+=" "; if (s.Length()>n) s=s.SubString(1,n); txt+=s+" ";
txt+="\r\n";
}
Main->mm_log->Lines->Add(txt); // this just output txt string to memo
return true;
}
这里的输出在我的机器上:
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
如您所见,最后一列(第3列)包含您想要的信息。查看setupapi.h
内部的所有SPDRP_
定义您可以使用..。VCL中唯一使用的是AnsiString
,因此将其更改为您可以使用的任何字符串类型。
这并不局限于USB。如果您想要所有设备,那么将TEXT("USB")
搜索参数更改为NULL
hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES);
发布于 2020-08-19 08:34:19
static int GetPhysicalPort()
{
try
{
devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnPSignedDriver WHERE DeviceId LIKE 'USB\\VID%' AND Description = 'USB Mass Storage Device' "))
{
collection = searcher.Get();
searcher.Dispose();
}
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceId"),
(string)device.GetPropertyValue("Description"),
(string)device.GetPropertyValue("Location")
));
}
collection.Dispose();
string LastAdded = devices[0].Location.Substring(6, 4);
Console.WriteLine(LastAdded);
return Convert.ToInt32(LastAdded);
}
catch (Exception e)
{
Console.WriteLine(e);
return 0;
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string Description, string location)
{
this.DeviceID = deviceID;
this.Desc = Description;
this.Location = location;
}
public string DeviceID { get;}
public string Desc { get;}
public string Location { get;}
}
我正在用这个方法来获取你想要的插槽。事实上,由于我的要求,我选择了最后一个插入USB的插槽。您只需调试并查看类USBDeviceInfo的内容,然后将其用于您自己的目的。
https://stackoverflow.com/questions/44236310
复制相似问题