- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
有谁知道使用 C# 检测连接到 USB 3.0 主机端口的 USB 设备是运行在 3.0 还是 2.0 的方法?
我们正在制造 USB 3.0 延长线,我们需要验证所有引脚是否已正确焊接。我们想在软件中做到这一点。我们想将 3.0 拇指驱动器连接到电缆并检查设备是否在 USB 3.0 模式下运行。如果它处于 2.0 模式,我们知道它们是 1 条或多条 USB 3.0 线的问题。
最佳答案
在一些源代码的帮助下,我设法制作了一个工作演示 I found .
private static void Main(string[] args)
{
var hostCtrls = USB.GetHostControllers();
foreach (var hostCtrl in hostCtrls)
{
var hub = hostCtrl.GetRootHub();
foreach (var port in hub.GetPorts())
{
if (port.IsDeviceConnected && !port.IsHub)
{
var device = port.GetDevice();
Console.WriteLine("Serial: " + device.DeviceSerialNumber);
Console.WriteLine("Speed: " + port.Speed);
Console.WriteLine("Port: " + device.PortNumber + Environment.NewLine);
}
}
}
}
应用程序枚举 USB 主机 Controller 。然后它获取根集线器并枚举属于它的端口。如果连接了设备但不是集线器,则会显示所需信息。
在您的情况下,您可能知道要检查哪个设备,因此您可以修改源代码(上面和链接的代码)以专门检查该设备。
您需要在 USB 类中创建一个方法,通过指定端口号和到集线器的路径从特定集线器获取特定端口。
类似于:
GetDeviceSpeed(string hubPath, int portNumber) { ... }
并用适当的值调用它:
var hubPath = @"\\.\NUSB3#ROOT_HUB30#5&b235176&0#{f18a0e88-c30c-11d0-8815-00a0c906bed8}";
var portNumber = 2;
GetDeviceSpeed(hubPath, portNumber);
如果您不愿意这样做,那么您可以简单地使用上面的代码并注意您要测试的设备的序列号,然后只检查速度:
if (device.DeviceSerialNumber == "xxxxxx")
Console.WriteLine("Speed: " + port.Speed);
如果您要在带有 GUI 的应用程序中使用它,您只需在下拉列表中选择要检查的设备。
嗯...有一些想法,希望有一个可行的解决方案。
附录
为了长篇大论,我将包括我用于演示的修改类。
(由于 SO 对 30000 个字符的限制而删除了注释和空行):
public class USB
{
const int GENERIC_WRITE = 0x40000000;
const int FILE_SHARE_READ = 0x1;
const int FILE_SHARE_WRITE = 0x2;
const int OPEN_EXISTING = 0x3;
const int INVALID_HANDLE_VALUE = -1;
const int IOCTL_GET_HCD_DRIVERKEY_NAME = 0x220424;
const int IOCTL_USB_GET_ROOT_HUB_NAME = 0x220408;
const int IOCTL_USB_GET_NODE_INFORMATION = 0x220408;
const int IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX = 0x220448;
const int IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION = 0x220410;
const int IOCTL_USB_GET_NODE_CONNECTION_NAME = 0x220414;
const int IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME = 0x220420;
const int USB_DEVICE_DESCRIPTOR_TYPE = 0x1;
const int USB_STRING_DESCRIPTOR_TYPE = 0x3;
const int BUFFER_SIZE = 2048;
const int MAXIMUM_USB_STRING_LENGTH = 255;
const string GUID_DEVINTERFACE_HUBCONTROLLER = "3abf6f2d-71c4-462a-8a92-1e6861e6af27";
const string REGSTR_KEY_USB = "USB";
const int DIGCF_PRESENT = 0x2;
const int DIGCF_ALLCLASSES = 0x4;
const int DIGCF_DEVICEINTERFACE = 0x10;
const int SPDRP_DRIVER = 0x9;
const int SPDRP_DEVICEDESC = 0x0;
const int REG_SZ = 1;
enum USB_HUB_NODE
{
UsbHub,
UsbMIParent
}
enum USB_CONNECTION_STATUS
{
NoDeviceConnected,
DeviceConnected,
DeviceFailedEnumeration,
DeviceGeneralFailure,
DeviceCausedOvercurrent,
DeviceNotEnoughPower,
DeviceNotEnoughBandwidth,
DeviceHubNestedTooDeeply,
DeviceInLegacyHub
}
enum USB_DEVICE_SPEED : byte
{
UsbLowSpeed,
UsbFullSpeed,
UsbHighSpeed,
UsbSuperSpeed
}
[StructLayout(LayoutKind.Sequential)]
struct SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public IntPtr DevInst;
public IntPtr Reserved;
}
[StructLayout(LayoutKind.Sequential)]
struct SP_DEVICE_INTERFACE_DATA
{
public int cbSize;
public Guid InterfaceClassGuid;
public int Flags;
public IntPtr Reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
public int cbSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
public string DevicePath;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct USB_HCD_DRIVERKEY_NAME
{
public int ActualLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
public string DriverKeyName;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct USB_ROOT_HUB_NAME
{
public int ActualLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
public string RootHubName;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct USB_HUB_DESCRIPTOR
{
public byte bDescriptorLength;
public byte bDescriptorType;
public byte bNumberOfPorts;
public short wHubCharacteristics;
public byte bPowerOnToPowerGood;
public byte bHubControlCurrent;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public byte[] bRemoveAndPowerMask;
}
[StructLayout(LayoutKind.Sequential)]
struct USB_HUB_INFORMATION
{
public USB_HUB_DESCRIPTOR HubDescriptor;
public byte HubIsBusPowered;
}
[StructLayout(LayoutKind.Sequential)]
struct USB_NODE_INFORMATION
{
public int NodeType;
public USB_HUB_INFORMATION HubInformation;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct USB_NODE_CONNECTION_INFORMATION_EX
{
public int ConnectionIndex;
public USB_DEVICE_DESCRIPTOR DeviceDescriptor;
public byte CurrentConfigurationValue;
public byte Speed;
public byte DeviceIsHub;
public short DeviceAddress;
public int NumberOfOpenPipes;
public int ConnectionStatus;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct USB_DEVICE_DESCRIPTOR
{
public byte bLength;
public byte bDescriptorType;
public short bcdUSB;
public byte bDeviceClass;
public byte bDeviceSubClass;
public byte bDeviceProtocol;
public byte bMaxPacketSize0;
public short idVendor;
public short idProduct;
public short bcdDevice;
public byte iManufacturer;
public byte iProduct;
public byte iSerialNumber;
public byte bNumConfigurations;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct USB_STRING_DESCRIPTOR
{
public byte bLength;
public byte bDescriptorType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAXIMUM_USB_STRING_LENGTH)]
public string bString;
}
[StructLayout(LayoutKind.Sequential)]
struct USB_SETUP_PACKET
{
public byte bmRequest;
public byte bRequest;
public short wValue;
public short wIndex;
public short wLength;
}
[StructLayout(LayoutKind.Sequential)]
struct USB_DESCRIPTOR_REQUEST
{
public int ConnectionIndex;
public USB_SETUP_PACKET SetupPacket;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct USB_NODE_CONNECTION_NAME
{
public int ConnectionIndex;
public int ActualLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
public string NodeName;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct USB_NODE_CONNECTION_DRIVERKEY_NAME
{
public int ConnectionIndex;
public int ActualLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
public string DriverKeyName;
}
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
static extern IntPtr SetupDiGetClassDevs(
ref Guid ClassGuid,
int Enumerator,
IntPtr hwndParent,
int Flags
);
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
static extern IntPtr SetupDiGetClassDevs(
int ClassGuid,
string Enumerator,
IntPtr hwndParent,
int Flags
);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SetupDiEnumDeviceInterfaces(
IntPtr DeviceInfoSet,
IntPtr DeviceInfoData,
ref Guid InterfaceClassGuid,
int MemberIndex,
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData
);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SetupDiGetDeviceInterfaceDetail(
IntPtr DeviceInfoSet,
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
ref SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData,
int DeviceInterfaceDetailDataSize,
ref int RequiredSize,
ref SP_DEVINFO_DATA DeviceInfoData
);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SetupDiGetDeviceRegistryProperty(
IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData,
int iProperty,
ref int PropertyRegDataType,
IntPtr PropertyBuffer,
int PropertyBufferSize,
ref int RequiredSize
);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SetupDiEnumDeviceInfo(
IntPtr DeviceInfoSet,
int MemberIndex,
ref SP_DEVINFO_DATA DeviceInfoData
);
[DllImport("setupapi.dll", SetLastError = true)]
static extern bool SetupDiDestroyDeviceInfoList(
IntPtr DeviceInfoSet
);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SetupDiGetDeviceInstanceId(
IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData,
StringBuilder DeviceInstanceId,
int DeviceInstanceIdSize,
out int RequiredSize
);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool DeviceIoControl(
IntPtr hDevice,
int dwIoControlCode,
IntPtr lpInBuffer,
int nInBufferSize,
IntPtr lpOutBuffer,
int nOutBufferSize,
out int lpBytesReturned,
IntPtr lpOverlapped
);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr CreateFile(
string lpFileName,
int dwDesiredAccess,
int dwShareMode,
IntPtr lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
IntPtr hTemplateFile
);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool CloseHandle(
IntPtr hObject
);
static public System.Collections.ObjectModel.ReadOnlyCollection<USBController> GetHostControllers()
{
List<USBController> HostList = new List<USBController>();
Guid HostGUID = new Guid(GUID_DEVINTERFACE_HUBCONTROLLER);
IntPtr h = SetupDiGetClassDevs(ref HostGUID, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
bool Success;
int i = 0;
do
{
USBController host = new USBController();
host.ControllerIndex = i;
SP_DEVICE_INTERFACE_DATA dia = new SP_DEVICE_INTERFACE_DATA();
dia.cbSize = Marshal.SizeOf(dia);
Success = SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref HostGUID, i, ref dia);
if (Success)
{
SP_DEVINFO_DATA da = new SP_DEVINFO_DATA();
da.cbSize = Marshal.SizeOf(da);
SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA();
didd.cbSize = 4 + Marshal.SystemDefaultCharSize;
int nRequiredSize = 0;
int nBytes = BUFFER_SIZE;
if (SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nBytes, ref nRequiredSize, ref da))
{
host.ControllerDevicePath = didd.DevicePath;
int RequiredSize = 0;
int RegType = REG_SZ;
if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref RegType, ptrBuf, BUFFER_SIZE, ref RequiredSize))
{
host.ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf);
}
if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref RegType, ptrBuf, BUFFER_SIZE, ref RequiredSize))
{
host.ControllerDriverKeyName = Marshal.PtrToStringAuto(ptrBuf);
}
}
HostList.Add(host);
}
i++;
} while (Success);
Marshal.FreeHGlobal(ptrBuf);
SetupDiDestroyDeviceInfoList(h);
}
return new System.Collections.ObjectModel.ReadOnlyCollection<USBController>(HostList);
}
public class USBController
{
internal int ControllerIndex;
internal string ControllerDriverKeyName, ControllerDevicePath, ControllerDeviceDesc;
public USBController()
{
ControllerIndex = 0;
ControllerDevicePath = "";
ControllerDeviceDesc = "";
ControllerDriverKeyName = "";
}
public int Index
{
get { return ControllerIndex; }
}
public string DevicePath
{
get { return ControllerDevicePath; }
}
public string DriverKeyName
{
get { return ControllerDriverKeyName; }
}
public string Name
{
get { return ControllerDeviceDesc; }
}
public USBHub GetRootHub()
{
IntPtr h, h2;
USBHub Root = new USBHub();
Root.HubIsRootHub = true;
Root.HubDeviceDesc = "Root Hub";
h = CreateFile(ControllerDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
int nBytesReturned;
USB_ROOT_HUB_NAME HubName = new USB_ROOT_HUB_NAME();
int nBytes = Marshal.SizeOf(HubName);
IntPtr ptrHubName = Marshal.AllocHGlobal(nBytes);
if (DeviceIoControl(h, IOCTL_USB_GET_ROOT_HUB_NAME, ptrHubName, nBytes, ptrHubName, nBytes, out nBytesReturned, IntPtr.Zero))
{
HubName = (USB_ROOT_HUB_NAME)Marshal.PtrToStructure(ptrHubName, typeof(USB_ROOT_HUB_NAME));
Root.HubDevicePath = @"\\.\" + HubName.RootHubName;
}
h2 = CreateFile(Root.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (h2.ToInt32() != INVALID_HANDLE_VALUE)
{
USB_NODE_INFORMATION NodeInfo = new USB_NODE_INFORMATION();
NodeInfo.NodeType = (int)USB_HUB_NODE.UsbHub;
nBytes = Marshal.SizeOf(NodeInfo);
IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(NodeInfo, ptrNodeInfo, true);
if (DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes, out nBytesReturned, IntPtr.Zero))
{
NodeInfo = (USB_NODE_INFORMATION)Marshal.PtrToStructure(ptrNodeInfo, typeof(USB_NODE_INFORMATION));
Root.HubIsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered);
Root.HubPortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
}
Marshal.FreeHGlobal(ptrNodeInfo);
CloseHandle(h2);
}
Marshal.FreeHGlobal(ptrHubName);
CloseHandle(h);
}
return Root;
}
}
public class USBHub
{
internal int HubPortCount;
internal string HubDriverKey, HubDevicePath, HubDeviceDesc;
internal string HubManufacturer, HubProduct, HubSerialNumber, HubInstanceID;
internal bool HubIsBusPowered, HubIsRootHub;
public USBHub()
{
HubPortCount = 0;
HubDevicePath = "";
HubDeviceDesc = "";
HubDriverKey = "";
HubIsBusPowered = false;
HubIsRootHub = false;
HubManufacturer = "";
HubProduct = "";
HubSerialNumber = "";
HubInstanceID = "";
}
public int PortCount
{
get { return HubPortCount; }
}
public string DevicePath
{
get { return HubDevicePath; }
}
public string DriverKey
{
get { return HubDriverKey; }
}
public string Name
{
get { return HubDeviceDesc; }
}
public string InstanceID
{
get { return HubInstanceID; }
}
public bool IsBusPowered
{
get { return HubIsBusPowered; }
}
public bool IsRootHub
{
get { return HubIsRootHub; }
}
public string Manufacturer
{
get { return HubManufacturer; }
}
public string Product
{
get { return HubProduct; }
}
public string SerialNumber
{
get { return HubSerialNumber; }
}
public System.Collections.ObjectModel.ReadOnlyCollection<USBPort> GetPorts()
{
List<USBPort> PortList = new List<USBPort>();
IntPtr h = CreateFile(HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
int nBytes = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX));
IntPtr ptrNodeConnection = Marshal.AllocHGlobal(nBytes);
for (int i = 1; i <= HubPortCount; i++)
{
int nBytesReturned;
USB_NODE_CONNECTION_INFORMATION_EX NodeConnection = new USB_NODE_CONNECTION_INFORMATION_EX();
NodeConnection.ConnectionIndex = i;
Marshal.StructureToPtr(NodeConnection, ptrNodeConnection, true);
if (DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, ptrNodeConnection, nBytes, ptrNodeConnection, nBytes, out nBytesReturned, IntPtr.Zero))
{
NodeConnection = (USB_NODE_CONNECTION_INFORMATION_EX)Marshal.PtrToStructure(ptrNodeConnection, typeof(USB_NODE_CONNECTION_INFORMATION_EX));
USBPort port = new USBPort();
port.PortPortNumber = i;
port.PortHubDevicePath = HubDevicePath;
USB_CONNECTION_STATUS Status = (USB_CONNECTION_STATUS)NodeConnection.ConnectionStatus;
port.PortStatus = Status.ToString();
USB_DEVICE_SPEED Speed = (USB_DEVICE_SPEED)NodeConnection.Speed;
port.PortSpeed = Speed.ToString();
port.PortIsDeviceConnected = (NodeConnection.ConnectionStatus == (int)USB_CONNECTION_STATUS.DeviceConnected);
port.PortIsHub = Convert.ToBoolean(NodeConnection.DeviceIsHub);
port.PortDeviceDescriptor = NodeConnection.DeviceDescriptor;
PortList.Add(port);
}
}
Marshal.FreeHGlobal(ptrNodeConnection);
CloseHandle(h);
}
return new System.Collections.ObjectModel.ReadOnlyCollection<USBPort>(PortList);
}
}
public class USBPort
{
internal int PortPortNumber;
internal string PortStatus, PortHubDevicePath, PortSpeed;
internal bool PortIsHub, PortIsDeviceConnected;
internal USB_DEVICE_DESCRIPTOR PortDeviceDescriptor;
public USBPort()
{
PortPortNumber = 0;
PortStatus = "";
PortHubDevicePath = "";
PortSpeed = "";
PortIsHub = false;
PortIsDeviceConnected = false;
}
public int PortNumber
{
get { return PortPortNumber; }
}
public string HubDevicePath
{
get { return PortHubDevicePath; }
}
public string Status
{
get { return PortStatus; }
}
public string Speed
{
get { return PortSpeed; }
}
public bool IsHub
{
get { return PortIsHub; }
}
public bool IsDeviceConnected
{
get { return PortIsDeviceConnected; }
}
public USBDevice GetDevice()
{
if (!PortIsDeviceConnected)
{
return null;
}
USBDevice Device = new USBDevice();
Device.DevicePortNumber = PortPortNumber;
Device.DeviceHubDevicePath = PortHubDevicePath;
Device.DeviceDescriptor = PortDeviceDescriptor;
IntPtr h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
int nBytesReturned;
int nBytes = BUFFER_SIZE;
string NullString = new string((char)0, BUFFER_SIZE / Marshal.SystemDefaultCharSize);
if (PortDeviceDescriptor.iManufacturer > 0)
{
USB_DESCRIPTOR_REQUEST Request = new USB_DESCRIPTOR_REQUEST();
Request.ConnectionIndex = PortPortNumber;
Request.SetupPacket.wValue = (short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iManufacturer);
Request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(Request));
Request.SetupPacket.wIndex = 0x409;
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(NullString);
Marshal.StructureToPtr(Request, ptrRequest, true);
if (DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes, ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
{
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(Request));
USB_STRING_DESCRIPTOR StringDesc = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(ptrStringDesc, typeof(USB_STRING_DESCRIPTOR));
Device.DeviceManufacturer = StringDesc.bString;
}
Marshal.FreeHGlobal(ptrRequest);
}
if (PortDeviceDescriptor.iProduct > 0)
{
USB_DESCRIPTOR_REQUEST Request = new USB_DESCRIPTOR_REQUEST();
Request.ConnectionIndex = PortPortNumber;
Request.SetupPacket.wValue = (short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iProduct);
Request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(Request));
Request.SetupPacket.wIndex = 0x409;
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(NullString);
Marshal.StructureToPtr(Request, ptrRequest, true);
if (DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes, ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
{
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(Request));
USB_STRING_DESCRIPTOR StringDesc = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(ptrStringDesc, typeof(USB_STRING_DESCRIPTOR));
Device.DeviceProduct = StringDesc.bString;
}
Marshal.FreeHGlobal(ptrRequest);
}
if (PortDeviceDescriptor.iSerialNumber > 0)
{
USB_DESCRIPTOR_REQUEST Request = new USB_DESCRIPTOR_REQUEST();
Request.ConnectionIndex = PortPortNumber;
Request.SetupPacket.wValue = (short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iSerialNumber);
Request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(Request));
Request.SetupPacket.wIndex = 0x409;
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(NullString);
Marshal.StructureToPtr(Request, ptrRequest, true);
if (DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes, ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
{
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(Request));
USB_STRING_DESCRIPTOR StringDesc = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(ptrStringDesc, typeof(USB_STRING_DESCRIPTOR));
Device.DeviceSerialNumber = StringDesc.bString;
}
Marshal.FreeHGlobal(ptrRequest);
}
USB_NODE_CONNECTION_DRIVERKEY_NAME DriverKey = new USB_NODE_CONNECTION_DRIVERKEY_NAME();
DriverKey.ConnectionIndex = PortPortNumber;
nBytes = Marshal.SizeOf(DriverKey);
IntPtr ptrDriverKey = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(DriverKey, ptrDriverKey, true);
if (DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME, ptrDriverKey, nBytes, ptrDriverKey, nBytes, out nBytesReturned, IntPtr.Zero))
{
DriverKey = (USB_NODE_CONNECTION_DRIVERKEY_NAME)Marshal.PtrToStructure(ptrDriverKey, typeof(USB_NODE_CONNECTION_DRIVERKEY_NAME));
Device.DeviceDriverKey = DriverKey.DriverKeyName;
Device.DeviceName = GetDescriptionByKeyName(Device.DeviceDriverKey);
Device.DeviceInstanceID = GetInstanceIDByKeyName(Device.DeviceDriverKey);
}
Marshal.FreeHGlobal(ptrDriverKey);
CloseHandle(h);
}
return Device;
}
public USBHub GetHub()
{
if (!PortIsHub)
{
return null;
}
USBHub Hub = new USBHub();
IntPtr h, h2;
Hub.HubIsRootHub = false;
Hub.HubDeviceDesc = "External Hub";
h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
int nBytesReturned;
USB_NODE_CONNECTION_NAME NodeName = new USB_NODE_CONNECTION_NAME();
NodeName.ConnectionIndex = PortPortNumber;
int nBytes = Marshal.SizeOf(NodeName);
IntPtr ptrNodeName = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(NodeName, ptrNodeName, true);
if (DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_NAME, ptrNodeName, nBytes, ptrNodeName, nBytes, out nBytesReturned, IntPtr.Zero))
{
NodeName = (USB_NODE_CONNECTION_NAME)Marshal.PtrToStructure(ptrNodeName, typeof(USB_NODE_CONNECTION_NAME));
Hub.HubDevicePath = @"\\.\" + NodeName.NodeName;
}
h2 = CreateFile(Hub.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (h2.ToInt32() != INVALID_HANDLE_VALUE)
{
USB_NODE_INFORMATION NodeInfo = new USB_NODE_INFORMATION();
NodeInfo.NodeType = (int)USB_HUB_NODE.UsbHub;
nBytes = Marshal.SizeOf(NodeInfo);
IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(NodeInfo, ptrNodeInfo, true);
if (DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes, out nBytesReturned, IntPtr.Zero))
{
NodeInfo = (USB_NODE_INFORMATION)Marshal.PtrToStructure(ptrNodeInfo, typeof(USB_NODE_INFORMATION));
Hub.HubIsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered);
Hub.HubPortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
}
Marshal.FreeHGlobal(ptrNodeInfo);
CloseHandle(h2);
}
USBDevice Device = GetDevice();
Hub.HubInstanceID = Device.DeviceInstanceID;
Hub.HubManufacturer = Device.Manufacturer;
Hub.HubProduct = Device.Product;
Hub.HubSerialNumber = Device.SerialNumber;
Hub.HubDriverKey = Device.DriverKey;
Marshal.FreeHGlobal(ptrNodeName);
CloseHandle(h);
}
return Hub;
}
}
public class USBDevice
{
internal int DevicePortNumber;
internal string DeviceDriverKey, DeviceHubDevicePath, DeviceInstanceID, DeviceName;
internal string DeviceManufacturer, DeviceProduct, DeviceSerialNumber;
internal USB_DEVICE_DESCRIPTOR DeviceDescriptor;
public USBDevice()
{
DevicePortNumber = 0;
DeviceHubDevicePath = "";
DeviceDriverKey = "";
DeviceManufacturer = "";
DeviceProduct = "Unknown USB Device";
DeviceSerialNumber = "";
DeviceName = "";
DeviceInstanceID = "";
}
public int PortNumber
{
get { return DevicePortNumber; }
}
public string HubDevicePath
{
get { return DeviceHubDevicePath; }
}
public string DriverKey
{
get { return DeviceDriverKey; }
}
public string InstanceID
{
get { return DeviceInstanceID; }
}
public string Name
{
get { return DeviceName; }
}
public string Manufacturer
{
get { return DeviceManufacturer; }
}
public string Product
{
get { return DeviceProduct; }
}
public string SerialNumber
{
get { return DeviceSerialNumber; }
}
}
static string GetDescriptionByKeyName(string DriverKeyName)
{
string ans = "";
string DevEnum = REGSTR_KEY_USB;
IntPtr h = SetupDiGetClassDevs(0, DevEnum, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
string KeyName;
bool Success;
int i = 0;
do
{
SP_DEVINFO_DATA da = new SP_DEVINFO_DATA();
da.cbSize = Marshal.SizeOf(da);
Success = SetupDiEnumDeviceInfo(h, i, ref da);
if (Success)
{
int RequiredSize = 0;
int RegType = REG_SZ;
KeyName = "";
if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref RegType, ptrBuf, BUFFER_SIZE, ref RequiredSize))
{
KeyName = Marshal.PtrToStringAuto(ptrBuf);
}
if (KeyName == DriverKeyName)
{
if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref RegType, ptrBuf, BUFFER_SIZE, ref RequiredSize))
{
ans = Marshal.PtrToStringAuto(ptrBuf);
}
break;
}
}
i++;
} while (Success);
Marshal.FreeHGlobal(ptrBuf);
SetupDiDestroyDeviceInfoList(h);
}
return ans;
}
static string GetInstanceIDByKeyName(string DriverKeyName)
{
string ans = "";
string DevEnum = REGSTR_KEY_USB;
IntPtr h = SetupDiGetClassDevs(0, DevEnum, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
string KeyName;
bool Success;
int i = 0;
do
{
SP_DEVINFO_DATA da = new SP_DEVINFO_DATA();
da.cbSize = Marshal.SizeOf(da);
Success = SetupDiEnumDeviceInfo(h, i, ref da);
if (Success)
{
int RequiredSize = 0;
int RegType = REG_SZ;
KeyName = "";
if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref RegType, ptrBuf, BUFFER_SIZE, ref RequiredSize))
{
KeyName = Marshal.PtrToStringAuto(ptrBuf);
}
if (KeyName == DriverKeyName)
{
int nBytes = BUFFER_SIZE;
StringBuilder sb = new StringBuilder(nBytes);
SetupDiGetDeviceInstanceId(h, ref da, sb, nBytes, out RequiredSize);
ans = sb.ToString();
break;
}
}
i++;
} while (Success);
Marshal.FreeHGlobal(ptrBuf);
SetupDiDestroyDeviceInfoList(h);
}
return ans;
}
}
关于c# - 检测设备是否使用 USB 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15820091/
我有一个 if 语句,如下所示 if (not(fullpath.lower().endswith(".pdf")) or not (fullpath.lower().endswith(tup
然而,在 PHP 中,可以: only appears if $foo is true. only appears if $foo is false. 在 Javascript 中,能否在一个脚
XML有很多好处。它既是机器可读的,也是人类可读的,它具有标准化的格式,并且用途广泛。 它也有一些缺点。它是冗长的,不是传输大量数据的非常有效的方法。 XML最有用的方面之一是模式语言。使用模式,您可
由于长期使用 SQL2000,我并没有真正深入了解公用表表达式。 我给出的答案here (#4025380)和 here (#4018793)违背了潮流,因为他们没有使用 CTE。 我很欣赏它们对于递
我有一个应用程序: void deleteObj(id){ MyObj obj = getObjById(id); if (obj == null) { throw n
我的代码如下。可能我以类似的方式多次使用它,即简单地说,我正在以这种方式管理 session 和事务: List users= null; try{ sess
在开发J2EE Web应用程序时,我通常会按以下方式组织我的包结构 com.jameselsey.. 控制器-控制器/操作转到此处 服务-事务服务类,由控制器调用 域-应用程序使用的我的域类/对象 D
这更多是出于好奇而不是任何重要问题,但我只是想知道 memmove 中的以下片段文档: Copying takes place as if an intermediate buffer were us
路径压缩涉及将根指定为路径上每个节点的新父节点——这可能会降低根的等级,并可能降低路径上所有节点的等级。有办法解决这个问题吗?有必要处理这个吗?或者,也许可以将等级视为树高的上限而不是确切的高度? 谢
我有两个类,A 和 B。A 是 B 的父类,我有一个函数接收指向 A 类型类的指针,检查它是否也是 B 类型,如果是将调用另一个函数,该函数接受一个指向类型 B 的类的指针。当函数调用另一个函数时,我
有没有办法让 valgrind 使用多个处理器? 我正在使用 valgrind 的 callgrind 进行一些瓶颈分析,并注意到我的应用程序中的资源使用行为与在 valgrind/callgrind
假设我们要使用 ReaderT [(a,b)]超过 Maybe monad,然后我们想在列表中进行查找。 现在,一个简单且不常见的方法是: 第一种可能性 find a = ReaderT (looku
我的代码似乎有问题。我需要说的是: if ( $('html').attr('lang').val() == 'fr-FR' ) { // do this } else { // do
根据this文章(2018 年 4 月)AKS 在可用性集中运行时能够跨故障域智能放置 Pod,但尚不考虑更新域。很快就会使用更新域将 Pod 放入 AKS 中吗? 最佳答案 当您设置集群时,它已经自
course | section | type comart2 : bsit201 : lec comart2 :
我正在开发自己的 SDK,而这又依赖于某些第 3 方 SDK。例如 - OkHttp。 我应该将 OkHttp 添加到我的 build.gradle 中,还是让我的 SDK 用户包含它?在这种情况下,
随着 Rust 越来越充实,我对它的兴趣开始激起。我喜欢它支持代数数据类型,尤其是那些匹配的事实,但是对其他功能习语有什么想法吗? 例如标准库中是否有标准过滤器/映射/归约函数的集合,更重要的是,您能
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
我一直在研究 PHP 中的对象。我见过的所有示例甚至在它们自己的对象上都使用了对象构造函数。 PHP 会强制您这样做吗?如果是,为什么? 例如: firstname = $firstname;
...比关联数组? 关联数组会占用更多内存吗? $arr = array(1, 1, 1); $arr[10] = 1; $arr[] = 1; // <- index is 11; does the
我是一名优秀的程序员,十分优秀!