I try to detect if a USB device is connected to USB 3 port by checking the speed for SuperSpeed in ac# application.
I've managed to get standard usb connection data with
DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX ...
but if I'm using
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2
I got always ERROR_INVALID_PARAMETER.
Here is what I have done:
// 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);
}
And here I got always error 87 (ERROR_INVALID_PARAMETER). It seems to me that IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2 is wrong but it's the same value as I used in a C++ application.
Ran into similar issue :
Good luck !
follow as my code for resolve your issue. it only sample code for detect USB port 5(you can refer USBView for port number at first)
struct USB_NODE_CONNECTION_INFORMATION_EX_V2
{
// one based port number
public int ConnectionIndex;
// length of the structure
public int Length;
// On input a bitmask that indicates which USB protocols are understood by the caller
// On output a bitmask that indicates which USB signaling protocols are supported by the port
//public USB_PROTOCOLS SupportedUsbProtocols;
public int SupportedUsbProtocols;
// A bitmask indicating properties of the connected device or port
//public USB_NODE_CONNECTION_INFORMATION_EX_V2_FLAGS Flags;
public int Flags;
}
//////////////////////////////////////////////////////////////////////////////
[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
);
//////////////////////////////////////////////////////////////////////////////
private static int CTL_CODE(int dwDeviceType, int dwFunction, int dwMethod, int dwAccess)
{
return ((dwDeviceType) << 16) | ((dwAccess) << 14) | ((dwFunction) << 2) | (dwMethod);
}
///////////////////////////////////////////////////////////////////////////
private const int USB_GET_NODE_CONNECTION_INFORMATION_EX_V2 = 279;
private static int IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2 = CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_INFORMATION_EX_V2, METHOD_BUFFERED, FILE_ANY_ACCESS);//IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2 = 0x22045c;
///////////////////////////////////////////////////////////////////////////
private IntPtr OpenUSB()
{
//ouverture en lecture partagAce
return CreateFile(@"\\.\USB#ROOT_HUB30#4&393adc3c&0&0#{f18a0e88-c30c-11d0-8815-00a0c906bed8}", Convert.ToUInt32(GENERIC_WRITE), Convert.ToInt32(FILE_SHARE_WRITE), IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
}
/////////////////////////////////////////////////////////////////////
public void GetUSBProtocol()
{
IntPtr pDisplay = OpenUSB();
if (pDisplay == new IntPtr(-1))
return;
int nBytesReturned;
int nBytesV2 = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX_V2));
IntPtr ptrNodeConnectionV2 = Marshal.AllocHGlobal(nBytesV2);
USB_NODE_CONNECTION_INFORMATION_EX_V2 pBrightness = new USB_NODE_CONNECTION_INFORMATION_EX_V2();
pBrightness.ConnectionIndex = 5;//if you wnat to dectect port 5
pBrightness.Length = nBytesV2;
pBrightness.SupportedUsbProtocols = 4;
Marshal.StructureToPtr(pBrightness, ptrNodeConnectionV2, true);
DeviceIoControl(pDisplay, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2, ptrNodeConnectionV2, nBytesV2, ptrNodeConnectionV2, nBytesV2, out nBytesReturned, IntPtr.Zero);
pBrightness = (USB_NODE_CONNECTION_INFORMATION_EX_V2)Marshal.PtrToStructure(ptrNodeConnectionV2, typeof(USB_NODE_CONNECTION_INFORMATION_EX_V2));
CloseHandle(pDisplay);
return;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.