コード例 #1
0
        public Win32.USB_NODE_CONNECTION_INFORMATION_EX GetNodeConnectionInfo(IntPtr hHub, int portIndex)
        {
            int    nBytesReturned;
            int    nNodeConnectionBytes = Marshal.SizeOf(typeof(Win32.USB_NODE_CONNECTION_INFORMATION_EX));
            IntPtr ptrNodeConnection    = Marshal.AllocHGlobal(nNodeConnectionBytes);

            Win32.USB_NODE_CONNECTION_INFORMATION_EX nodeConnection = new Win32.USB_NODE_CONNECTION_INFORMATION_EX();
            nodeConnection.ConnectionIndex = portIndex;
            Marshal.StructureToPtr(nodeConnection, ptrNodeConnection, true);

            if (!Win32.DeviceIoControl(hHub, Win32.IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX,
                                       ptrNodeConnection, nNodeConnectionBytes, ptrNodeConnection, nNodeConnectionBytes,
                                       out nBytesReturned, IntPtr.Zero))
            {
                int error = Marshal.GetLastWin32Error();
                Marshal.FreeHGlobal(ptrNodeConnection);
                return(null);
            }

            nodeConnection = (Win32.USB_NODE_CONNECTION_INFORMATION_EX)Marshal.PtrToStructure(ptrNodeConnection,
                                                                                              typeof(Win32.USB_NODE_CONNECTION_INFORMATION_EX));
            Marshal.FreeHGlobal(ptrNodeConnection);

            return(nodeConnection);
        }
コード例 #2
0
        public int FindPortIndex(String deviceDriverInst)
        {
            int portIndex = 0;

            IntPtr hHub = Open();

            if (hHub.ToInt32() == Win32.INVALID_HANDLE_VALUE)
            {
//		        ATLTRACE(_T("*** ERROR 0x%X (%d): Line %d of file %s\n"), error, error, __LINE__, __TFILE__);
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            // Loop through the Ports on the Hub any get the unique DriverKey if there is a device connected to
            // the Port. If the DriveryKey from the hub matches the DriverKey for our device, we have our Hub Index.
            foreach (UsbPort port in _Ports)
            {
                Win32.USB_NODE_CONNECTION_INFORMATION_EX nodeConnection = this.GetNodeConnectionInfo(hHub, port.Index);

                // There is a device connected to this Port
                if (nodeConnection.ConnectionStatus == Win32.USB_CONNECTION_STATUS.DeviceConnected)
                {
                    // Get the Driver Key Name
                    String driverKey = this.GetNodeConnectionDriver(hHub, port.Index);

                    if (!String.IsNullOrEmpty(driverKey))
                    {
                        // If the Driver Keys match, this is the correct Port
                        if (String.Compare(driverKey, deviceDriverInst, true) == 0)
                        {
                            portIndex = port.Index;
                            break;
                        }
                    } // if ( got the driverKey )
                }     // if(connected)
            }         // foreach ( port )

            Win32.CloseHandle(hHub);

            return(portIndex);
        }