簡體   English   中英

無法使用DeviceIoControl使用c#檢測USB 3.0端口信息

[英]Can not detect USB 3.0 port information with c# using DeviceIoControl

我嘗試通過檢查ac#應用程序中SuperSpeed的速度來檢測USB設備是否已連接到USB 3端口。

我設法獲得了標准的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 ++應用程序中使用的值相同。

遇到類似的問題:

祝好運 !

請按照我的代碼來解決您的問題。 它僅是用於檢測USB端口5的示例代碼(您可以首先參考USBView作為端口號)

    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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM