internal static extern Boolean DeviceIoControl( IntPtr hFile, Int32 dwIoControlCode, IntPtr lpInBuffer, Int32 nInBufferSize, ref USB_HCD_DRIVERKEY_NAME lpOutBuffer, Int32 nOutBufferSize, out Int32 nBytesReturned, IntPtr lpOverlapped );
/// <summary> /// USB ROOT HUB设备路径 /// </summary> /// <param name="PNPDeviceID">USB主控制器设备ID</param> /// <returns>USB ROOT HUB设备路径</returns> public static String GetUsbRootHubPath(String PNPDeviceID) { if (String.IsNullOrEmpty(PNPDeviceID)) { return(null); } // 打开设备 IntPtr hHCDev = Kernel32.CreateFile( "\\\\.\\" + PNPDeviceID.Replace('\\', '#') + "#{3ABF6F2D-71C4-462A-8A92-1E6861E6AF27}", NativeFileAccess.GENERIC_WRITE, NativeFileShare.FILE_SHARE_WRITE, IntPtr.Zero, NativeFileMode.OPEN_EXISTING, IntPtr.Zero, IntPtr.Zero); if (hHCDev == Kernel32.INVALID_HANDLE_VALUE) { return(null); } // 获取USB ROOT HUB名称 Int32 nBytesReturned; USB_HCD_DRIVERKEY_NAME Buffer = new USB_HCD_DRIVERKEY_NAME(); Boolean Status = DeviceIoControl(hHCDev, IOCTL_USB_GET_ROOT_HUB_NAME, IntPtr.Zero, 0, ref Buffer, Marshal.SizeOf(Buffer), out nBytesReturned, IntPtr.Zero); // 关闭设备 Kernel32.CloseHandle(hHCDev); return(Status ? Buffer.Name : null); }
internal static extern Boolean DeviceIoControl( IntPtr hFile, Int32 dwIoControlCode, IntPtr lpInBuffer, Int32 nInBufferSize, ref USB_HCD_DRIVERKEY_NAME lpOutBuffer, Int32 nOutBufferSize, out Int32 nBytesReturned, IntPtr lpOverlapped );
/// <summary> /// USB ROOT HUB设备路径 /// </summary> /// <param name="PNPDeviceID">USB主控制器设备ID</param> /// <returns>USB ROOT HUB设备路径</returns> public static String GetUsbRootHubPath(String PNPDeviceID) { if (String.IsNullOrEmpty(PNPDeviceID)) return null; // 打开设备 IntPtr hHCDev = Kernel32.CreateFile( "\\\\.\\" + PNPDeviceID.Replace('\\', '#') + "#{3ABF6F2D-71C4-462A-8A92-1E6861E6AF27}", NativeFileAccess.GENERIC_WRITE, NativeFileShare.FILE_SHARE_WRITE, IntPtr.Zero, NativeFileMode.OPEN_EXISTING, IntPtr.Zero, IntPtr.Zero); if (hHCDev == Kernel32.INVALID_HANDLE_VALUE) return null; // 获取USB ROOT HUB名称 Int32 nBytesReturned; USB_HCD_DRIVERKEY_NAME Buffer = new USB_HCD_DRIVERKEY_NAME(); Boolean Status = DeviceIoControl(hHCDev, IOCTL_USB_GET_ROOT_HUB_NAME, IntPtr.Zero, 0, ref Buffer, Marshal.SizeOf(Buffer), out nBytesReturned, IntPtr.Zero); // 关闭设备 Kernel32.CloseHandle(hHCDev); return Status ? Buffer.Name : null; }