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 static ReadOnlyCollection <USBController> GetHostControllers() { List <USBController> list = new List <USBController>(); Guid guid = new Guid("3abf6f2d-71c4-462a-8a92-1e6861e6af27"); IntPtr deviceInfoSet = Acer_USB_Library.SetupDiGetClassDevs(ref guid, 0, IntPtr.Zero, 18); if (deviceInfoSet.ToInt32() != -1) { IntPtr intPtr = Marshal.AllocHGlobal(2048); int num = 0; bool flag; do { USBController uSBController = new USBController(); uSBController.ControllerIndex = num; SP_DEVICE_INTERFACE_DATA structure = default(SP_DEVICE_INTERFACE_DATA); structure.cbSize = Marshal.SizeOf(structure); flag = Acer_USB_Library.SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref guid, num, ref structure); if (flag) { SP_DEVINFO_DATA structure2 = default(SP_DEVINFO_DATA); structure2.cbSize = Marshal.SizeOf(structure2); SP_DEVICE_INTERFACE_DETAIL_DATA sP_DEVICE_INTERFACE_DETAIL_DATA = default(SP_DEVICE_INTERFACE_DETAIL_DATA); sP_DEVICE_INTERFACE_DETAIL_DATA.cbSize = 4 + Marshal.SystemDefaultCharSize; int num2 = 0; int deviceInterfaceDetailDataSize = 2048; if (Acer_USB_Library.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref structure, ref sP_DEVICE_INTERFACE_DETAIL_DATA, deviceInterfaceDetailDataSize, ref num2, ref structure2)) { uSBController.ControllerDevicePath = sP_DEVICE_INTERFACE_DETAIL_DATA.DevicePath; int num3 = 0; int num4 = 1; if (Acer_USB_Library.SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref structure2, 0, ref num4, intPtr, 2048, ref num3)) { uSBController.ControllerDeviceDesc = Marshal.PtrToStringAuto(intPtr); } if (Acer_USB_Library.SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref structure2, 9, ref num4, intPtr, 2048, ref num3)) { uSBController.ControllerDriverKeyName = Marshal.PtrToStringAuto(intPtr); } } list.Add(uSBController); } num++; }while (flag); Marshal.FreeHGlobal(intPtr); Acer_USB_Library.SetupDiDestroyDeviceInfoList(deviceInfoSet); } return(new ReadOnlyCollection <USBController>(list)); }
private static string GetInstanceIDByKeyName(string DriverKeyName) { string result = ""; string enumerator = "USB"; IntPtr deviceInfoSet = Acer_USB_Library.SetupDiGetClassDevs(0, enumerator, IntPtr.Zero, 6); if (deviceInfoSet.ToInt32() != -1) { IntPtr intPtr = Marshal.AllocHGlobal(2048); int num = 0; bool flag; do { SP_DEVINFO_DATA structure = default(SP_DEVINFO_DATA); structure.cbSize = Marshal.SizeOf(structure); flag = Acer_USB_Library.SetupDiEnumDeviceInfo(deviceInfoSet, num, ref structure); if (flag) { int num2 = 0; int num3 = 1; string a = ""; if (Acer_USB_Library.SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref structure, 9, ref num3, intPtr, 2048, ref num2)) { a = Marshal.PtrToStringAuto(intPtr); } if (a == DriverKeyName) { int num4 = 2048; StringBuilder stringBuilder = new StringBuilder(num4); Acer_USB_Library.SetupDiGetDeviceInstanceId(deviceInfoSet, ref structure, stringBuilder, num4, out num2); result = stringBuilder.ToString(); break; } } num++; }while (flag); Marshal.FreeHGlobal(intPtr); Acer_USB_Library.SetupDiDestroyDeviceInfoList(deviceInfoSet); } return(result); }
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); }
public static bool Is_USB30_Port(int VID, int PID) { foreach (Acer_USB_Library.USBController hostController in Acer_USB_Library.GetHostControllers()) { Console.WriteLine("hostCtrl.Index: " + hostController.Index); Console.WriteLine("hostCtrl.Name: " + hostController.Name); foreach (Acer_USB_Library.USBPort port in hostController.GetRootHub().GetPorts()) { if (port.IsDeviceConnected && !port.IsHub) { Acer_USB_Library.USBDevice device = port.GetDevice(); if (device.VID == VID && device.PID == PID) { bool result = port.IsHub; Console.WriteLine("IsHub: " + result.ToString()); Console.WriteLine("Product: " + device.Product); Console.WriteLine("VID/PID: " + device.VID + " / " + device.PID); Console.WriteLine("Manufacturer: " + device.Manufacturer); Console.WriteLine("HubDevicePath: " + device.HubDevicePath); if (device.HubDevicePath.Contains("HUB30")) { Console.WriteLine("USB 3.0 port"); result = true; return(result); } Console.WriteLine("Name: " + device.Name); Console.WriteLine("DriverKey: " + device.DriverKey); Console.WriteLine("InstanceID: " + device.InstanceID); Console.WriteLine("Serial: " + device.DeviceSerialNumber); Console.WriteLine("Speed: " + port.Speed); Console.WriteLine("Port: " + device.PortNumber + Environment.NewLine); } } } Console.WriteLine("===============\n"); } return(false); }
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)); }
public USBDevice GetDevice() { if (!this.PortIsDeviceConnected) { return(null); } USBDevice uSBDevice = new USBDevice(); uSBDevice.DevicePortNumber = this.PortPortNumber; uSBDevice.DeviceHubDevicePath = this.PortHubDevicePath; uSBDevice.DeviceDescriptor = this.PortDeviceDescriptor; uSBDevice.DeviceVID = this.PortDeviceDescriptor.idVendor; uSBDevice.DevicePID = this.PortDeviceDescriptor.idProduct; IntPtr intPtr = Acer_USB_Library.CreateFile(this.PortHubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero); if (intPtr.ToInt32() != -1) { int num = 2048; string s = new string('\0', 2048 / Marshal.SystemDefaultCharSize); int num2 = default(int); if (this.PortDeviceDescriptor.iManufacturer > 0) { USB_DESCRIPTOR_REQUEST structure = default(USB_DESCRIPTOR_REQUEST); structure.ConnectionIndex = this.PortPortNumber; structure.SetupPacket.wValue = (short)(768 + this.PortDeviceDescriptor.iManufacturer); structure.SetupPacket.wLength = (short)(num - Marshal.SizeOf(structure)); structure.SetupPacket.wIndex = 1033; IntPtr intPtr2 = Marshal.StringToHGlobalAuto(s); Marshal.StructureToPtr(structure, intPtr2, true); if (Acer_USB_Library.DeviceIoControl(intPtr, 2229264, intPtr2, num, intPtr2, num, out num2, IntPtr.Zero)) { USB_STRING_DESCRIPTOR uSB_STRING_DESCRIPTOR = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(new IntPtr(intPtr2.ToInt32() + Marshal.SizeOf(structure)), typeof(USB_STRING_DESCRIPTOR)); uSBDevice.DeviceManufacturer = uSB_STRING_DESCRIPTOR.bString; } Marshal.FreeHGlobal(intPtr2); } if (this.PortDeviceDescriptor.iProduct > 0) { USB_DESCRIPTOR_REQUEST structure2 = default(USB_DESCRIPTOR_REQUEST); structure2.ConnectionIndex = this.PortPortNumber; structure2.SetupPacket.wValue = (short)(768 + this.PortDeviceDescriptor.iProduct); structure2.SetupPacket.wLength = (short)(num - Marshal.SizeOf(structure2)); structure2.SetupPacket.wIndex = 1033; IntPtr intPtr3 = Marshal.StringToHGlobalAuto(s); Marshal.StructureToPtr(structure2, intPtr3, true); if (Acer_USB_Library.DeviceIoControl(intPtr, 2229264, intPtr3, num, intPtr3, num, out num2, IntPtr.Zero)) { USB_STRING_DESCRIPTOR uSB_STRING_DESCRIPTOR2 = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(new IntPtr(intPtr3.ToInt32() + Marshal.SizeOf(structure2)), typeof(USB_STRING_DESCRIPTOR)); uSBDevice.DeviceProduct = uSB_STRING_DESCRIPTOR2.bString; } Marshal.FreeHGlobal(intPtr3); } if (this.PortDeviceDescriptor.iSerialNumber > 0) { USB_DESCRIPTOR_REQUEST structure3 = default(USB_DESCRIPTOR_REQUEST); structure3.ConnectionIndex = this.PortPortNumber; structure3.SetupPacket.wValue = (short)(768 + this.PortDeviceDescriptor.iSerialNumber); structure3.SetupPacket.wLength = (short)(num - Marshal.SizeOf(structure3)); structure3.SetupPacket.wIndex = 1033; IntPtr intPtr4 = Marshal.StringToHGlobalAuto(s); Marshal.StructureToPtr(structure3, intPtr4, true); if (Acer_USB_Library.DeviceIoControl(intPtr, 2229264, intPtr4, num, intPtr4, num, out num2, IntPtr.Zero)) { USB_STRING_DESCRIPTOR uSB_STRING_DESCRIPTOR3 = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(new IntPtr(intPtr4.ToInt32() + Marshal.SizeOf(structure3)), typeof(USB_STRING_DESCRIPTOR)); uSBDevice.DeviceSerialNumber = uSB_STRING_DESCRIPTOR3.bString; } Marshal.FreeHGlobal(intPtr4); } USB_NODE_CONNECTION_DRIVERKEY_NAME uSB_NODE_CONNECTION_DRIVERKEY_NAME = default(USB_NODE_CONNECTION_DRIVERKEY_NAME); uSB_NODE_CONNECTION_DRIVERKEY_NAME.ConnectionIndex = this.PortPortNumber; num = Marshal.SizeOf(uSB_NODE_CONNECTION_DRIVERKEY_NAME); IntPtr intPtr5 = Marshal.AllocHGlobal(num); Marshal.StructureToPtr(uSB_NODE_CONNECTION_DRIVERKEY_NAME, intPtr5, true); if (Acer_USB_Library.DeviceIoControl(intPtr, 2229280, intPtr5, num, intPtr5, num, out num2, IntPtr.Zero)) { uSB_NODE_CONNECTION_DRIVERKEY_NAME = (USB_NODE_CONNECTION_DRIVERKEY_NAME)Marshal.PtrToStructure(intPtr5, typeof(USB_NODE_CONNECTION_DRIVERKEY_NAME)); uSBDevice.DeviceDriverKey = uSB_NODE_CONNECTION_DRIVERKEY_NAME.DriverKeyName; uSBDevice.DeviceName = Acer_USB_Library.GetDescriptionByKeyName(uSBDevice.DeviceDriverKey); uSBDevice.DeviceInstanceID = Acer_USB_Library.GetInstanceIDByKeyName(uSBDevice.DeviceDriverKey); } Marshal.FreeHGlobal(intPtr5); Acer_USB_Library.CloseHandle(intPtr); } return(uSBDevice); }