internal UsbDeviceFactory( SafeFileHandle hubHandle, uint portNo, USB_NODE_CONNECTION_INFORMATION_EX conneectInfomation) : this(hubHandle, portNo, conneectInfomation, GUID_DEVINTERFACE_USB_DEVICE) { }
// return a list of the down stream ports public ReadOnlyCollection <USBPort> GetPorts() { var PortList = new List <USBPort>(); // Open a handle to the Hub device var h = CreateFile(HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h.ToInt32() == INVALID_HANDLE_VALUE) { return(new ReadOnlyCollection <USBPort>(PortList)); } var nBytes = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX)); var ptrNodeConnection = Marshal.AllocHGlobal(nBytes); // loop thru all of the ports on the hub // BTW: Ports are numbered starting at 1 for (var i = 1; i <= HubPortCount; i++) { int nBytesReturned; var NodeConnection = new USB_NODE_CONNECTION_INFORMATION_EX { 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)) { continue; } NodeConnection = (USB_NODE_CONNECTION_INFORMATION_EX)Marshal.PtrToStructure(ptrNodeConnection, typeof(USB_NODE_CONNECTION_INFORMATION_EX)); // load up the USBPort class var Status = (USB_CONNECTION_STATUS)NodeConnection.ConnectionStatus; var Speed = (USB_DEVICE_SPEED)NodeConnection.Speed; var port = new USBPort { PortPortNumber = i, PortHubDevicePath = HubDevicePath, PortStatus = Status.ToString(), PortSpeed = Speed.ToString(), PortIsDeviceConnected = (NodeConnection.ConnectionStatus == (int)USB_CONNECTION_STATUS.DeviceConnected), PortIsHub = Convert.ToBoolean(NodeConnection.DeviceIsHub), PortDeviceDescriptor = NodeConnection.DeviceDescriptor }; // add it to the list PortList.Add(port); } Marshal.FreeHGlobal(ptrNodeConnection); CloseHandle(h); // convert it into a Collection return(new ReadOnlyCollection <USBPort>(PortList)); }
internal static extern Boolean DeviceIoControl( IntPtr hFile, Int32 dwIoControlCode, ref USB_NODE_CONNECTION_INFORMATION_EX lpInBuffer, Int32 nInBufferSize, ref USB_NODE_CONNECTION_INFORMATION_EX lpOutBuffer, Int32 nOutBufferSize, out Int32 lpBytesReturned, IntPtr lpOverlapped );
internal UsbDeviceFactory( SafeFileHandle hubHandle, uint portNo, USB_NODE_CONNECTION_INFORMATION_EX conneectInfomation, Guid targetDevice) { _hubHandle = hubHandle; _portNo = portNo; _usbConnectInfomation = conneectInfomation; _targetDevice = targetDevice; }
internal static USB_NODE_CONNECTION_INFORMATION_EX GetNodeConnectionInformation(SafeFileHandle handle, UInt32 port) { int nBytes = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX)); USB_NODE_CONNECTION_INFORMATION_EX nodeConnection = new USB_NODE_CONNECTION_INFORMATION_EX(); nodeConnection.ConnectionIndex = port; if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, ref nodeConnection, nBytes, out nodeConnection, nBytes, out nBytes, IntPtr.Zero)) { throw new Win32Exception(); } return(nodeConnection); }
private void GetNodeConnectionInfo() { if (HasNodeConnectionInfo) { return; } if (Parent == null) { HasNodeConnectionInfo = true; return; } using (SafeFileHandle handle = Parent.OpenHandle()) NodeConnectionInfo = GetNodeConnectionInformation(handle, AdapterNumber); HasNodeConnectionInfo = true; }
public ReadOnlyCollection <USBPort> GetPorts() { List <USBPort> list = new List <USBPort>(); IntPtr intPtr = Acer_USB_Library.CreateFile(this.HubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero); if (intPtr.ToInt32() != -1) { int num = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX)); IntPtr intPtr2 = Marshal.AllocHGlobal(num); for (int i = 1; i <= this.HubPortCount; i++) { USB_NODE_CONNECTION_INFORMATION_EX uSB_NODE_CONNECTION_INFORMATION_EX = default(USB_NODE_CONNECTION_INFORMATION_EX); uSB_NODE_CONNECTION_INFORMATION_EX.ConnectionIndex = i; Marshal.StructureToPtr(uSB_NODE_CONNECTION_INFORMATION_EX, intPtr2, true); int num2 = default(int); if (Acer_USB_Library.DeviceIoControl(intPtr, 2229320, intPtr2, num, intPtr2, num, out num2, IntPtr.Zero)) { uSB_NODE_CONNECTION_INFORMATION_EX = (USB_NODE_CONNECTION_INFORMATION_EX)Marshal.PtrToStructure(intPtr2, typeof(USB_NODE_CONNECTION_INFORMATION_EX)); USBPort uSBPort = new USBPort(); uSBPort.PortPortNumber = i; uSBPort.PortHubDevicePath = this.HubDevicePath; USB_CONNECTION_STATUS connectionStatus = (USB_CONNECTION_STATUS)uSB_NODE_CONNECTION_INFORMATION_EX.ConnectionStatus; uSBPort.PortStatus = connectionStatus.ToString(); USB_DEVICE_SPEED speed = (USB_DEVICE_SPEED)uSB_NODE_CONNECTION_INFORMATION_EX.Speed; uSBPort.PortSpeed = speed.ToString(); uSBPort.PortIsDeviceConnected = (uSB_NODE_CONNECTION_INFORMATION_EX.ConnectionStatus == 1); uSBPort.PortIsHub = Convert.ToBoolean(uSB_NODE_CONNECTION_INFORMATION_EX.DeviceIsHub); uSBPort.PortDeviceDescriptor = uSB_NODE_CONNECTION_INFORMATION_EX.DeviceDescriptor; list.Add(uSBPort); } } Marshal.FreeHGlobal(intPtr2); Acer_USB_Library.CloseHandle(intPtr); } return(new ReadOnlyCollection <USBPort>(list)); }
/// <summary> /// 获取USB节点连接信息 /// </summary> /// <param name="DevicePath">设备路径</param> /// <param name="NumberOfPorts">端口总数</param> /// <returns>USB节点信息连接信息集合</returns> public static UsbNodeConnectionInformation[] GetUsbNodeConnectionInformation(String DevicePath, Int32 NumberOfPorts) { if (String.IsNullOrEmpty(DevicePath)) { return(null); } // 打开设备文件 IntPtr hHubDevice = Kernel32.CreateFile( "\\\\.\\" + DevicePath, NativeFileAccess.GENERIC_WRITE, NativeFileShare.FILE_SHARE_WRITE, IntPtr.Zero, NativeFileMode.OPEN_EXISTING, IntPtr.Zero, IntPtr.Zero); if (hHubDevice == Kernel32.INVALID_HANDLE_VALUE) { return(null); } List <UsbNodeConnectionInformation> NodeCollection = new List <UsbNodeConnectionInformation>(); // 枚举端口 USB_NODE_CONNECTION_INFORMATION_EX Buffer = new USB_NODE_CONNECTION_INFORMATION_EX(); for (Int32 ConnectionIndex = 1; ConnectionIndex <= NumberOfPorts; ConnectionIndex++) { // 查询节点信息 Int32 nBytesReturned; Buffer.ConnectionIndex = ConnectionIndex; Boolean Status = DeviceIoControl(hHubDevice, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, ref Buffer, Marshal.SizeOf(Buffer), ref Buffer, Marshal.SizeOf(Buffer), out nBytesReturned, IntPtr.Zero); if (Status) { // 确定语言ID UInt16 LanguageID = SelectLanguageID(hHubDevice, ConnectionIndex); // 提取信息 UsbNodeConnectionInformation Node = new UsbNodeConnectionInformation(); Node.DevicePath = DevicePath; Node.ConnectionIndex = Buffer.ConnectionIndex; Node.ConnectionStatus = Buffer.ConnectionStatus; if (Buffer.ConnectionStatus == USB_CONNECTION_STATUS.DeviceConnected) { Node.CurrentConfigurationValue = Buffer.CurrentConfigurationValue; Node.Speed = Buffer.Speed; Node.DeviceIsHub = Convert.ToBoolean(Buffer.DeviceIsHub); Node.DeviceAddress = Buffer.DeviceAddress; Node.NumberOfOpenPipes = Buffer.NumberOfOpenPipes; // 设备描述符 Node.DeviceDescriptor.bDescriptorType = Buffer.DeviceDescriptor.bDescriptorType; Node.DeviceDescriptor.bDeviceClass = Buffer.DeviceDescriptor.bDeviceClass; Node.DeviceDescriptor.bDeviceSubClass = Buffer.DeviceDescriptor.bDeviceSubClass; Node.DeviceDescriptor.bDeviceProtocol = Buffer.DeviceDescriptor.bDeviceProtocol; Node.DeviceDescriptor.UsbVersion = BcdVersionToString(Buffer.DeviceDescriptor.bcdUSB); // USB版本号 Node.DeviceDescriptor.DeviceVersion = BcdVersionToString(Buffer.DeviceDescriptor.bcdDevice); // 设备版本号 Node.DeviceDescriptor.idVendor = Buffer.DeviceDescriptor.idVendor; // 厂商标识 Node.DeviceDescriptor.idProduct = Buffer.DeviceDescriptor.idProduct; // 产品标识 if (LanguageID != 0) { if (Buffer.DeviceDescriptor.iSerialNumber != 0) { // 序列号 Node.DeviceDescriptor.SerialNumber = GetStringDescriptor(hHubDevice, Buffer.ConnectionIndex, Buffer.DeviceDescriptor.iSerialNumber, LanguageID); } if (Buffer.DeviceDescriptor.iManufacturer != 0) { // 制造商名称 Node.DeviceDescriptor.Manufacturer = GetStringDescriptor(hHubDevice, Buffer.ConnectionIndex, Buffer.DeviceDescriptor.iManufacturer, LanguageID); } if (Buffer.DeviceDescriptor.iProduct != 0) { // 产品名称 Node.DeviceDescriptor.Product = GetStringDescriptor(hHubDevice, Buffer.ConnectionIndex, Buffer.DeviceDescriptor.iProduct, LanguageID); } } Node.DeviceDescriptor.bMaxPacketSize0 = Buffer.DeviceDescriptor.bMaxPacketSize0; Node.DeviceDescriptor.bNumConfigurations = Buffer.DeviceDescriptor.bNumConfigurations; // 管道信息 Node.PipeList = new List <UsbPipeInfo>(); for (Int32 PipeIndex = 0; PipeIndex < Buffer.NumberOfOpenPipes; PipeIndex++) { UsbPipeInfo PipeInfo; PipeInfo.ScheduleOffset = Buffer.PipeList[PipeIndex].ScheduleOffset; PipeInfo.bDescriptorType = Buffer.PipeList[PipeIndex].EndpointDescriptor.bDescriptorType; PipeInfo.bEndpointAddress = Buffer.PipeList[PipeIndex].EndpointDescriptor.bEndpointAddress; PipeInfo.bmAttributes = Buffer.PipeList[PipeIndex].EndpointDescriptor.bmAttributes; PipeInfo.wMaxPacketSize = Buffer.PipeList[PipeIndex].EndpointDescriptor.wMaxPacketSize; PipeInfo.bInterval = Buffer.PipeList[PipeIndex].EndpointDescriptor.bInterval; Node.PipeList.Add(PipeInfo); } } NodeCollection.Add(Node); } } // 关闭设备文件 Kernel32.CloseHandle(hHubDevice); // 返回结果 if (NodeCollection.Count == 0) { return(null); } else { return(NodeCollection.ToArray()); } }
public UsbHubFactory(SafeFileHandle hubHandle, uint portNo, USB_NODE_CONNECTION_INFORMATION_EX conneectInfomation) { _portNo = portNo; _hubHandle = hubHandle; _usbConnectInfomation = conneectInfomation; }
// return a list of the down stream ports public System.Collections.ObjectModel.ReadOnlyCollection<USBPort> GetPorts() { List<USBPort> PortList = new List<USBPort>(); // Open a handle to the Hub device 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); // loop thru all of the ports on the hub // BTW: Ports are numbered starting at 1 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)); // load up the USBPort class 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; // add it to the list PortList.Add(port); } } Marshal.FreeHGlobal(ptrNodeConnection); CloseHandle(h); } // convert it into a Collection return new System.Collections.ObjectModel.ReadOnlyCollection<USBPort>(PortList); }
internal static extern Boolean DeviceIoControl( IntPtr hFile, Int32 dwIoControlCode, ref USB_NODE_CONNECTION_INFORMATION_EX lpInBuffer, Int32 nInBufferSize, ref USB_NODE_CONNECTION_INFORMATION_EX lpOutBuffer, Int32 nOutBufferSize, out Int32 lpBytesReturned, IntPtr lpOverlapped );
/// <summary> /// 获取USB节点连接信息 /// </summary> /// <param name="DevicePath">设备路径</param> /// <param name="NumberOfPorts">端口总数</param> /// <returns>USB节点信息连接信息集合</returns> public static UsbNodeConnectionInformation[] GetUsbNodeConnectionInformation(String DevicePath, Int32 NumberOfPorts) { if (String.IsNullOrEmpty(DevicePath)) return null; // 打开设备文件 IntPtr hHubDevice = Kernel32.CreateFile( "\\\\.\\" + DevicePath, NativeFileAccess.GENERIC_WRITE, NativeFileShare.FILE_SHARE_WRITE, IntPtr.Zero, NativeFileMode.OPEN_EXISTING, IntPtr.Zero, IntPtr.Zero); if (hHubDevice == Kernel32.INVALID_HANDLE_VALUE) return null; List<UsbNodeConnectionInformation> NodeCollection = new List<UsbNodeConnectionInformation>(); // 枚举端口 USB_NODE_CONNECTION_INFORMATION_EX Buffer = new USB_NODE_CONNECTION_INFORMATION_EX(); for (Int32 ConnectionIndex = 1; ConnectionIndex <= NumberOfPorts; ConnectionIndex++) { // 查询节点信息 Int32 nBytesReturned; Buffer.ConnectionIndex = ConnectionIndex; Boolean Status = DeviceIoControl(hHubDevice, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, ref Buffer, Marshal.SizeOf(Buffer), ref Buffer, Marshal.SizeOf(Buffer), out nBytesReturned, IntPtr.Zero); if (Status) { // 确定语言ID UInt16 LanguageID = SelectLanguageID(hHubDevice, ConnectionIndex); // 提取信息 UsbNodeConnectionInformation Node = new UsbNodeConnectionInformation(); Node.DevicePath = DevicePath; Node.ConnectionIndex = Buffer.ConnectionIndex; Node.ConnectionStatus = Buffer.ConnectionStatus; if (Buffer.ConnectionStatus == USB_CONNECTION_STATUS.DeviceConnected) { Node.CurrentConfigurationValue = Buffer.CurrentConfigurationValue; Node.Speed = Buffer.Speed; Node.DeviceIsHub = Convert.ToBoolean(Buffer.DeviceIsHub); Node.DeviceAddress = Buffer.DeviceAddress; Node.NumberOfOpenPipes = Buffer.NumberOfOpenPipes; // 设备描述符 Node.DeviceDescriptor.bDescriptorType = Buffer.DeviceDescriptor.bDescriptorType; Node.DeviceDescriptor.bDeviceClass = Buffer.DeviceDescriptor.bDeviceClass; Node.DeviceDescriptor.bDeviceSubClass = Buffer.DeviceDescriptor.bDeviceSubClass; Node.DeviceDescriptor.bDeviceProtocol = Buffer.DeviceDescriptor.bDeviceProtocol; Node.DeviceDescriptor.UsbVersion = BcdVersionToString(Buffer.DeviceDescriptor.bcdUSB); // USB版本号 Node.DeviceDescriptor.DeviceVersion = BcdVersionToString(Buffer.DeviceDescriptor.bcdDevice); // 设备版本号 Node.DeviceDescriptor.idVendor = Buffer.DeviceDescriptor.idVendor; // 厂商标识 Node.DeviceDescriptor.idProduct = Buffer.DeviceDescriptor.idProduct; // 产品标识 if (LanguageID != 0) { if (Buffer.DeviceDescriptor.iSerialNumber != 0) { // 序列号 Node.DeviceDescriptor.SerialNumber = GetStringDescriptor(hHubDevice, Buffer.ConnectionIndex, Buffer.DeviceDescriptor.iSerialNumber, LanguageID); } if (Buffer.DeviceDescriptor.iManufacturer != 0) { // 制造商名称 Node.DeviceDescriptor.Manufacturer = GetStringDescriptor(hHubDevice, Buffer.ConnectionIndex, Buffer.DeviceDescriptor.iManufacturer, LanguageID); } if (Buffer.DeviceDescriptor.iProduct != 0) { // 产品名称 Node.DeviceDescriptor.Product = GetStringDescriptor(hHubDevice, Buffer.ConnectionIndex, Buffer.DeviceDescriptor.iProduct, LanguageID); } } Node.DeviceDescriptor.bMaxPacketSize0 = Buffer.DeviceDescriptor.bMaxPacketSize0; Node.DeviceDescriptor.bNumConfigurations = Buffer.DeviceDescriptor.bNumConfigurations; // 管道信息 Node.PipeList = new List<UsbPipeInfo>(); for (Int32 PipeIndex = 0; PipeIndex < Buffer.NumberOfOpenPipes; PipeIndex++) { UsbPipeInfo PipeInfo; PipeInfo.ScheduleOffset = Buffer.PipeList[PipeIndex].ScheduleOffset; PipeInfo.bDescriptorType = Buffer.PipeList[PipeIndex].EndpointDescriptor.bDescriptorType; PipeInfo.bEndpointAddress = Buffer.PipeList[PipeIndex].EndpointDescriptor.bEndpointAddress; PipeInfo.bmAttributes = Buffer.PipeList[PipeIndex].EndpointDescriptor.bmAttributes; PipeInfo.wMaxPacketSize = Buffer.PipeList[PipeIndex].EndpointDescriptor.wMaxPacketSize; PipeInfo.bInterval = Buffer.PipeList[PipeIndex].EndpointDescriptor.bInterval; Node.PipeList.Add(PipeInfo); } } NodeCollection.Add(Node); } } // 关闭设备文件 Kernel32.CloseHandle(hHubDevice); // 返回结果 if (NodeCollection.Count == 0) return null; else return NodeCollection.ToArray(); }
internal void SetNodeConnectionInfo(USB_NODE_CONNECTION_INFORMATION_EX nci) { NodeConnectionInfo = nci; HasNodeConnectionInfo = true; }