internal static extern Boolean DeviceIoControl( IntPtr hFile, Int32 dwIoControlCode, ref USB_NODE_INFORMATION lpInBuffer, Int32 nInBufferSize, ref USB_NODE_INFORMATION lpOutBuffer, Int32 nOutBufferSize, out Int32 lpBytesReturned, IntPtr lpOverlapped );
// Return Root Hub for this Controller public USBHub GetRootHub() { var Root = new USBHub { HubIsRootHub = true, HubDeviceDesc = "Root Hub" }; // Open a handle to the Host Controller var h = CreateFile(ControllerDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h.ToInt32() == INVALID_HANDLE_VALUE) { return(Root); } int nBytesReturned; var HubName = new USB_ROOT_HUB_NAME(); var nBytes = Marshal.SizeOf(HubName); var ptrHubName = Marshal.AllocHGlobal(nBytes); // get the Hub Name if (DeviceIoControl(h, IOCTL_USB_GET_ROOT_HUB_NAME, ptrHubName, nBytes, ptrHubName, nBytes, out nBytesReturned, IntPtr.Zero)) { HubName = (USB_ROOT_HUB_NAME)Marshal.PtrToStructure(ptrHubName, typeof(USB_ROOT_HUB_NAME)); Root.HubDevicePath = @"\\.\" + HubName.RootHubName; } // TODO: Get DriverKeyName for Root Hub // Now let's open the Hub (based upon the HubName we got above) var h2 = CreateFile(Root.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h2.ToInt32() != INVALID_HANDLE_VALUE) { var NodeInfo = new USB_NODE_INFORMATION { NodeType = (int)USB_HUB_NODE.UsbHub }; nBytes = Marshal.SizeOf(NodeInfo); var ptrNodeInfo = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(NodeInfo, ptrNodeInfo, true); // get the Hub Information if (DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes, out nBytesReturned, IntPtr.Zero)) { NodeInfo = (USB_NODE_INFORMATION)Marshal.PtrToStructure(ptrNodeInfo, typeof(USB_NODE_INFORMATION)); Root.HubIsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered); Root.HubPortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts; } Marshal.FreeHGlobal(ptrNodeInfo); CloseHandle(h2); } Marshal.FreeHGlobal(ptrHubName); CloseHandle(h); return(Root); }
private void GetNodeInformation() { if (HasNodeInformation) { return; } NodeInformation = new USB_NODE_INFORMATION(); int nBytes = Marshal.SizeOf(typeof(USB_NODE_INFORMATION)); using (SafeFileHandle handle = OpenHandle()) HasNodeInformation = Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_NODE_INFORMATION, ref NodeInformation, nBytes, out NodeInformation, nBytes, out nBytes, IntPtr.Zero); }
public USBHub GetHub() { if (!this.PortIsHub) { return(null); } USBHub uSBHub = new USBHub(); uSBHub.HubIsRootHub = false; uSBHub.HubDeviceDesc = "External Hub"; IntPtr intPtr = Acer_USB_Library.CreateFile(this.PortHubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero); if (intPtr.ToInt32() != -1) { USB_NODE_CONNECTION_NAME uSB_NODE_CONNECTION_NAME = default(USB_NODE_CONNECTION_NAME); uSB_NODE_CONNECTION_NAME.ConnectionIndex = this.PortPortNumber; int num = Marshal.SizeOf(uSB_NODE_CONNECTION_NAME); IntPtr intPtr2 = Marshal.AllocHGlobal(num); Marshal.StructureToPtr(uSB_NODE_CONNECTION_NAME, intPtr2, true); int num2 = default(int); if (Acer_USB_Library.DeviceIoControl(intPtr, 2229268, intPtr2, num, intPtr2, num, out num2, IntPtr.Zero)) { uSB_NODE_CONNECTION_NAME = (USB_NODE_CONNECTION_NAME)Marshal.PtrToStructure(intPtr2, typeof(USB_NODE_CONNECTION_NAME)); uSBHub.HubDevicePath = "\\\\.\\" + uSB_NODE_CONNECTION_NAME.NodeName; } IntPtr intPtr3 = Acer_USB_Library.CreateFile(uSBHub.HubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero); if (intPtr3.ToInt32() != -1) { USB_NODE_INFORMATION uSB_NODE_INFORMATION = default(USB_NODE_INFORMATION); uSB_NODE_INFORMATION.NodeType = 0; num = Marshal.SizeOf(uSB_NODE_INFORMATION); IntPtr intPtr4 = Marshal.AllocHGlobal(num); Marshal.StructureToPtr(uSB_NODE_INFORMATION, intPtr4, true); if (Acer_USB_Library.DeviceIoControl(intPtr3, 2229256, intPtr4, num, intPtr4, num, out num2, IntPtr.Zero)) { uSB_NODE_INFORMATION = (USB_NODE_INFORMATION)Marshal.PtrToStructure(intPtr4, typeof(USB_NODE_INFORMATION)); uSBHub.HubIsBusPowered = Convert.ToBoolean(uSB_NODE_INFORMATION.HubInformation.HubIsBusPowered); uSBHub.HubPortCount = uSB_NODE_INFORMATION.HubInformation.HubDescriptor.bNumberOfPorts; } Marshal.FreeHGlobal(intPtr4); Acer_USB_Library.CloseHandle(intPtr3); } USBDevice device = this.GetDevice(); uSBHub.HubInstanceID = device.DeviceInstanceID; uSBHub.HubManufacturer = device.Manufacturer; uSBHub.HubProduct = device.Product; uSBHub.HubSerialNumber = device.SerialNumber; uSBHub.HubDriverKey = device.DriverKey; Marshal.FreeHGlobal(intPtr2); Acer_USB_Library.CloseHandle(intPtr); } return(uSBHub); }
public USBHub GetRootHub() { USBHub uSBHub = new USBHub(); uSBHub.HubIsRootHub = true; uSBHub.HubDeviceDesc = "Root Hub"; IntPtr intPtr = Acer_USB_Library.CreateFile(this.ControllerDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero); if (intPtr.ToInt32() != -1) { int num = Marshal.SizeOf(default(USB_ROOT_HUB_NAME)); IntPtr intPtr2 = Marshal.AllocHGlobal(num); int num2 = default(int); if (Acer_USB_Library.DeviceIoControl(intPtr, 2229256, intPtr2, num, intPtr2, num, out num2, IntPtr.Zero)) { USB_ROOT_HUB_NAME uSB_ROOT_HUB_NAME = (USB_ROOT_HUB_NAME)Marshal.PtrToStructure(intPtr2, typeof(USB_ROOT_HUB_NAME)); uSBHub.HubDevicePath = "\\\\.\\" + uSB_ROOT_HUB_NAME.RootHubName; } IntPtr intPtr3 = Acer_USB_Library.CreateFile(uSBHub.HubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero); if (intPtr3.ToInt32() != -1) { USB_NODE_INFORMATION uSB_NODE_INFORMATION = default(USB_NODE_INFORMATION); uSB_NODE_INFORMATION.NodeType = 0; num = Marshal.SizeOf(uSB_NODE_INFORMATION); IntPtr intPtr4 = Marshal.AllocHGlobal(num); Marshal.StructureToPtr(uSB_NODE_INFORMATION, intPtr4, true); if (Acer_USB_Library.DeviceIoControl(intPtr3, 2229256, intPtr4, num, intPtr4, num, out num2, IntPtr.Zero)) { uSB_NODE_INFORMATION = (USB_NODE_INFORMATION)Marshal.PtrToStructure(intPtr4, typeof(USB_NODE_INFORMATION)); uSBHub.HubIsBusPowered = Convert.ToBoolean(uSB_NODE_INFORMATION.HubInformation.HubIsBusPowered); uSBHub.HubPortCount = uSB_NODE_INFORMATION.HubInformation.HubDescriptor.bNumberOfPorts; } Marshal.FreeHGlobal(intPtr4); Acer_USB_Library.CloseHandle(intPtr3); } Marshal.FreeHGlobal(intPtr2); Acer_USB_Library.CloseHandle(intPtr); } return(uSBHub); }
/// <summary> /// 获取USB HUB节点信息 /// </summary> /// <param name="DevicePath">USB HUB设备路径</param> /// <returns>节点信息</returns> public static UsbNodeInformation[] GetUsbNodeInformation(String DevicePath) { 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); } // 查询节点信息 Int32 nBytesReturned; USB_NODE_INFORMATION Buffer = new USB_NODE_INFORMATION(); Boolean Status = DeviceIoControl(hHubDevice, IOCTL_USB_GET_NODE_INFORMATION, ref Buffer, Marshal.SizeOf(Buffer), ref Buffer, Marshal.SizeOf(Buffer), out nBytesReturned, IntPtr.Zero); // 关闭设备文件 Kernel32.CloseHandle(hHubDevice); if (!Status) { return(null); } UsbNodeInformation Node = new UsbNodeInformation(); Node.NodeType = Buffer.NodeType; // 节点类型 Node.PNPDeviceID = DevicePath.Substring(0, DevicePath.LastIndexOf('#')).Replace('#', '\\'); // 设备ID Node.DevicePath = DevicePath; // 设备路径 Node.Name = GetUsbHubName(DevicePath); // 设备名称 if (Buffer.NodeType == USB_HUB_NODE.UsbHub) { Node.NumberOfPorts = Buffer.u.HubInformation.HubDescriptor.bNumberOfPorts; // 端口数 Node.HubIsBusPowered = Convert.ToBoolean(Buffer.u.HubInformation.HubIsBusPowered); // 供电方式 Node.HubCharacteristics = Buffer.u.HubInformation.HubDescriptor.wHubCharacteristics; Node.PowerOnToPowerGood = Buffer.u.HubInformation.HubDescriptor.bPowerOnToPowerGood; Node.HubControlCurrent = Buffer.u.HubInformation.HubDescriptor.bHubControlCurrent; } else { Node.NumberOfInterfaces = Buffer.u.MiParentInformation.NumberOfInterfaces; // 接口数 } return(new UsbNodeInformation[1] { Node }); }
// Return Root Hub for this Controller public USBHub GetRootHub() { IntPtr h, h2; USBHub Root = new USBHub(); Root.HubIsRootHub = true; Root.HubDeviceDesc = "Root Hub"; // Open a handle to the Host Controller h = CreateFile(ControllerDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h.ToInt32() != INVALID_HANDLE_VALUE) { int nBytesReturned; USB_ROOT_HUB_NAME HubName = new USB_ROOT_HUB_NAME(); int nBytes = Marshal.SizeOf(HubName); IntPtr ptrHubName = Marshal.AllocHGlobal(nBytes); // get the Hub Name if (DeviceIoControl(h, IOCTL_USB_GET_ROOT_HUB_NAME, ptrHubName, nBytes, ptrHubName, nBytes, out nBytesReturned, IntPtr.Zero)) { HubName = (USB_ROOT_HUB_NAME)Marshal.PtrToStructure(ptrHubName, typeof(USB_ROOT_HUB_NAME)); Root.HubDevicePath = @"\\.\" + HubName.RootHubName; } // TODO: Get DriverKeyName for Root Hub // Now let's open the Hub (based upon the HubName we got above) h2 = CreateFile(Root.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h2.ToInt32() != INVALID_HANDLE_VALUE) { USB_NODE_INFORMATION NodeInfo = new USB_NODE_INFORMATION(); NodeInfo.NodeType = (int)USB_HUB_NODE.UsbHub; nBytes = Marshal.SizeOf(NodeInfo); IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(NodeInfo, ptrNodeInfo, true); // get the Hub Information if (DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes, out nBytesReturned, IntPtr.Zero)) { NodeInfo = (USB_NODE_INFORMATION)Marshal.PtrToStructure(ptrNodeInfo, typeof(USB_NODE_INFORMATION)); Root.HubIsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered); Root.HubPortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts; } Marshal.FreeHGlobal(ptrNodeInfo); CloseHandle(h2); } Marshal.FreeHGlobal(ptrHubName); CloseHandle(h); } return Root; }
// return a down stream external hub public USBHub GetHub() { if (!PortIsHub) { return null; } USBHub Hub = new USBHub(); IntPtr h, h2; Hub.HubIsRootHub = false; Hub.HubDeviceDesc = "External Hub"; // Open a handle to the Host Controller h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h.ToInt32() != INVALID_HANDLE_VALUE) { // Get the DevicePath for downstream hub int nBytesReturned; USB_NODE_CONNECTION_NAME NodeName = new USB_NODE_CONNECTION_NAME(); NodeName.ConnectionIndex = PortPortNumber; int nBytes = Marshal.SizeOf(NodeName); IntPtr ptrNodeName = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(NodeName, ptrNodeName, true); // Use an IOCTL call to request the Node Name if (DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_NAME, ptrNodeName, nBytes, ptrNodeName, nBytes, out nBytesReturned, IntPtr.Zero)) { NodeName = (USB_NODE_CONNECTION_NAME)Marshal.PtrToStructure(ptrNodeName, typeof(USB_NODE_CONNECTION_NAME)); Hub.HubDevicePath = @"\\.\" + NodeName.NodeName; } // Now let's open the Hub (based upon the HubName we got above) h2 = CreateFile(Hub.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h2.ToInt32() != INVALID_HANDLE_VALUE) { USB_NODE_INFORMATION NodeInfo = new USB_NODE_INFORMATION(); NodeInfo.NodeType = (int)USB_HUB_NODE.UsbHub; nBytes = Marshal.SizeOf(NodeInfo); IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(NodeInfo, ptrNodeInfo, true); // get the Hub Information if (DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes, out nBytesReturned, IntPtr.Zero)) { NodeInfo = (USB_NODE_INFORMATION)Marshal.PtrToStructure(ptrNodeInfo, typeof(USB_NODE_INFORMATION)); Hub.HubIsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered); Hub.HubPortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts; } Marshal.FreeHGlobal(ptrNodeInfo); CloseHandle(h2); } // Fill in the missing Manufacture, Product, and SerialNumber values // values by just creating a Device instance and copying the values USBDevice Device = GetDevice(); Hub.HubInstanceID = Device.DeviceInstanceID; Hub.HubManufacturer = Device.Manufacturer; Hub.HubProduct = Device.Product; Hub.HubSerialNumber = Device.SerialNumber; Hub.HubDriverKey = Device.DriverKey; Marshal.FreeHGlobal(ptrNodeName); CloseHandle(h); } return Hub; }
internal static extern Boolean DeviceIoControl( IntPtr hFile, Int32 dwIoControlCode, ref USB_NODE_INFORMATION lpInBuffer, Int32 nInBufferSize, ref USB_NODE_INFORMATION lpOutBuffer, Int32 nOutBufferSize, out Int32 lpBytesReturned, IntPtr lpOverlapped );
/// <summary> /// 获取USB HUB节点信息 /// </summary> /// <param name="DevicePath">USB HUB设备路径</param> /// <returns>节点信息</returns> public static UsbNodeInformation[] GetUsbNodeInformation(String DevicePath) { 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; // 查询节点信息 Int32 nBytesReturned; USB_NODE_INFORMATION Buffer = new USB_NODE_INFORMATION(); Boolean Status = DeviceIoControl(hHubDevice, IOCTL_USB_GET_NODE_INFORMATION, ref Buffer, Marshal.SizeOf(Buffer), ref Buffer, Marshal.SizeOf(Buffer), out nBytesReturned, IntPtr.Zero); // 关闭设备文件 Kernel32.CloseHandle(hHubDevice); if (!Status) return null; UsbNodeInformation Node = new UsbNodeInformation(); Node.NodeType = Buffer.NodeType; // 节点类型 Node.PNPDeviceID = DevicePath.Substring(0, DevicePath.LastIndexOf('#')).Replace('#', '\\'); // 设备ID Node.DevicePath = DevicePath; // 设备路径 Node.Name = GetUsbHubName(DevicePath); // 设备名称 if (Buffer.NodeType == USB_HUB_NODE.UsbHub) { Node.NumberOfPorts = Buffer.u.HubInformation.HubDescriptor.bNumberOfPorts; // 端口数 Node.HubIsBusPowered = Convert.ToBoolean(Buffer.u.HubInformation.HubIsBusPowered); // 供电方式 Node.HubCharacteristics = Buffer.u.HubInformation.HubDescriptor.wHubCharacteristics; Node.PowerOnToPowerGood = Buffer.u.HubInformation.HubDescriptor.bPowerOnToPowerGood; Node.HubControlCurrent = Buffer.u.HubInformation.HubDescriptor.bHubControlCurrent; } else { Node.NumberOfInterfaces = Buffer.u.MiParentInformation.NumberOfInterfaces; // 接口数 } return new UsbNodeInformation[1] { Node }; }
// return a down stream external hub public USBHub GetHub() { if (!PortIsHub) { return(null); } var Hub = new USBHub { HubIsRootHub = false, HubDeviceDesc = "External Hub" }; // Open a handle to the Host Controller var h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h.ToInt32() == INVALID_HANDLE_VALUE) { return(Hub); } // Get the DevicePath for downstream hub int nBytesReturned; var NodeName = new USB_NODE_CONNECTION_NAME { ConnectionIndex = PortPortNumber }; var nBytes = Marshal.SizeOf(NodeName); var ptrNodeName = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(NodeName, ptrNodeName, true); // Use an IOCTL call to request the Node Name if (DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_NAME, ptrNodeName, nBytes, ptrNodeName, nBytes, out nBytesReturned, IntPtr.Zero)) { NodeName = (USB_NODE_CONNECTION_NAME)Marshal.PtrToStructure(ptrNodeName, typeof(USB_NODE_CONNECTION_NAME)); Hub.HubDevicePath = @"\\.\" + NodeName.NodeName; } // Now let's open the Hub (based upon the HubName we got above) var h2 = CreateFile(Hub.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h2.ToInt32() != INVALID_HANDLE_VALUE) { var NodeInfo = new USB_NODE_INFORMATION { NodeType = (int)USB_HUB_NODE.UsbHub }; nBytes = Marshal.SizeOf(NodeInfo); var ptrNodeInfo = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(NodeInfo, ptrNodeInfo, true); // get the Hub Information if (DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes, out nBytesReturned, IntPtr.Zero)) { NodeInfo = (USB_NODE_INFORMATION)Marshal.PtrToStructure(ptrNodeInfo, typeof(USB_NODE_INFORMATION)); Hub.HubIsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered); Hub.HubPortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts; } Marshal.FreeHGlobal(ptrNodeInfo); CloseHandle(h2); } // Fill in the missing Manufacture, Product, and SerialNumber values // values by just creating a Device instance and copying the values var Device = GetDevice(); Hub.HubInstanceID = Device.DeviceInstanceID; Hub.HubManufacturer = Device.Manufacturer; Hub.HubProduct = Device.Product; Hub.HubSerialNumber = Device.SerialNumber; Hub.HubDriverKey = Device.DriverKey; Marshal.FreeHGlobal(ptrNodeName); CloseHandle(h); return(Hub); }