-1
我试图通过检查c#应用程序中SuperSpeed的速度来检测USB设备是否连接到USB 3端口。使用DeviceIoControl无法检测USB 3.0端口信息c#使用DeviceIoControl
我已经成功地获得标准的USB连接数据与
的DeviceIoControl(H,IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX ...
,但如果我使用
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2
我总是得到ERROR_INVALID_PARAMETER 。
这是我所做的:
// define consts and structs
const UInt32 IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2 = 0x22045c;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct USB_PROTOCOLS
{
UInt32 protocols;
public bool Usb110 { get { return (this.protocols & 0x01) == 0x01; } }
public bool Usb200 { get { return (this.protocols & 0x02) == 0x02; } }
public bool Usb300 { get { return (this.protocols & 0x04) == 0x04; } }
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct USB_NODE_CONNECTION_INFORMATION_EX_V2_FLAGS
{
UInt32 flags;
public bool DeviceIsOperatingAtSuperSpeedOrHigher
{
get { return (this.flags & 0x01) == 0x01; }
}
public bool DeviceIsSuperSpeedCapableOrHigher
{
get { return (this.flags & 0x02) == 0x02; }
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct USB_NODE_CONNECTION_INFORMATION_EX_V2
{
public int ConnectionIndex;
public int Length;
public USB_PROTOCOLS SupportedUsbProtocols;
public USB_NODE_CONNECTION_INFORMATION_EX_V2_FLAGS Flags;
}
int nBytesReturnedV2;
int nBytesV2 = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX_V2));
IntPtr ptrNodeConnectionV2 = Marshal.AllocHGlobal(nBytesV2);
USB_NODE_CONNECTION_INFORMATION_EX_V2 NodeConnectionV2 = new USB_NODE_CONNECTION_INFORMATION_EX_V2();
NodeConnectionV2.ConnectionIndex = i;
NodeConnectionV2.Length = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX_V2));
Marshal.StructureToPtr(NodeConnectionV2, ptrNodeConnectionV2, true);
// request information
if (DeviceIoControl(h, (UInt32)IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2, ptrNodeConnectionV2, nBytesV2, ptrNodeConnectionV2, nBytesV2, out nBytesReturnedV2, IntPtr.Zero))
{
NodeConnectionV2 = (USB_NODE_CONNECTION_INFORMATION_EX_V2)Marshal.PtrToStructure(ptrNodeConnectionV2, typeof(USB_NODE_CONNECTION_INFORMATION_EX_V2));
} else
{
int errCode = Marshal.GetLastWin32Error();
Console.WriteLine("Err: " + errCode);
}
而在这里,我总是有错误87(ERROR_INVALID_PARAMETER)。在我看来,IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2是错误的,但它与我在C++应用程序中使用的值相同。