param( [Parameter(Mandatory = $true)][string]$InstanceId ) $ErrorActionPreference = 'Stop' $inf = (Resolve-Path (Join-Path $PSScriptRoot 'qhsusb.inf')).Path if (-not (Test-Path -LiteralPath $inf)) { throw '找不到 WinUSB 驱动描述' } Add-Type -TypeDefinition @" using System; using System.Runtime.InteropServices; using System.Text; public static class QhsusbOne { public const uint DIGCF_PRESENT = 0x2; public const uint DIGCF_ALLCLASSES = 0x4; public const uint DI_ENUMSINGLEINF = 0x00010000; public const uint DIF_SELECTBESTCOMPATDRV = 0x17; public const uint DIF_INSTALLDEVICE = 0x2; public const uint SPDIT_COMPATDRIVER = 2; [StructLayout(LayoutKind.Sequential)] public struct SP_DEVINFO_DATA { public int cbSize; public Guid ClassGuid; public uint DevInst; public IntPtr Reserved; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SP_DEVINSTALL_PARAMS { public int cbSize; public int Flags; public int FlagsEx; public IntPtr hwndParent; public IntPtr InstallMsgHandler; public IntPtr InstallMsgHandlerContext; public IntPtr FileQueue; public IntPtr ClassInstallReserved; public int Reserved; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string DriverPath; } [DllImport("setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr SetupDiGetClassDevs(IntPtr classGuid, string enumerator, IntPtr hwnd, uint flags); [DllImport("setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool SetupDiOpenDeviceInfo(IntPtr deviceInfoSet, string deviceInstanceId, IntPtr hwnd, uint flags, ref SP_DEVINFO_DATA data); [DllImport("setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool SetupDiGetDeviceInstallParams(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA data, ref SP_DEVINSTALL_PARAMS parms); [DllImport("setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool SetupDiSetDeviceInstallParams(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA data, ref SP_DEVINSTALL_PARAMS parms); [DllImport("setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool SetupDiBuildDriverInfoList(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA data, uint type); [DllImport("setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool SetupDiCallClassInstaller(uint installFunction, IntPtr deviceInfoSet, ref SP_DEVINFO_DATA data); [DllImport("setupapi.dll", SetLastError = true)] public static extern bool SetupDiDestroyDriverInfoList(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA data, uint type); [DllImport("setupapi.dll", SetLastError = true)] public static extern bool SetupDiDestroyDeviceInfoList(IntPtr deviceInfoSet); } "@ function Throw-IfFailed([bool]$ok, [string]$step) { if ($ok) { return } $code = [Runtime.InteropServices.Marshal]::GetLastWin32Error() throw ($step + ' 失败,Win32 错误 ' + $code) } $set = [QhsusbOne]::SetupDiGetClassDevs([IntPtr]::Zero, 'USB', [IntPtr]::Zero, [QhsusbOne]::DIGCF_PRESENT -bor [QhsusbOne]::DIGCF_ALLCLASSES) Throw-IfFailed (-not ($set -eq [IntPtr]::Zero -or $set -eq [IntPtr]-1)) '打开设备列表' $dev = New-Object QhsusbOne+SP_DEVINFO_DATA $dev.cbSize = [Runtime.InteropServices.Marshal]::SizeOf($dev) try { $ok = [QhsusbOne]::SetupDiOpenDeviceInfo($set, $InstanceId, [IntPtr]::Zero, 0, [ref]$dev) Throw-IfFailed $ok '定位这一台设备' $parms = New-Object QhsusbOne+SP_DEVINSTALL_PARAMS $parms.cbSize = [Runtime.InteropServices.Marshal]::SizeOf($parms) $ok = [QhsusbOne]::SetupDiGetDeviceInstallParams($set, [ref]$dev, [ref]$parms) Throw-IfFailed $ok '读取安装参数' $parms.Flags = $parms.Flags -bor [QhsusbOne]::DI_ENUMSINGLEINF $parms.DriverPath = $inf $ok = [QhsusbOne]::SetupDiSetDeviceInstallParams($set, [ref]$dev, [ref]$parms) Throw-IfFailed $ok '指定驱动文件' $ok = [QhsusbOne]::SetupDiBuildDriverInfoList($set, [ref]$dev, [QhsusbOne]::SPDIT_COMPATDRIVER) Throw-IfFailed $ok '建立驱动列表' try { $ok = [QhsusbOne]::SetupDiCallClassInstaller([QhsusbOne]::DIF_SELECTBESTCOMPATDRV, $set, [ref]$dev) Throw-IfFailed $ok '选择驱动' $ok = [QhsusbOne]::SetupDiCallClassInstaller([QhsusbOne]::DIF_INSTALLDEVICE, $set, [ref]$dev) Throw-IfFailed $ok '安装驱动' } finally { [void][QhsusbOne]::SetupDiDestroyDriverInfoList($set, [ref]$dev, [QhsusbOne]::SPDIT_COMPATDRIVER) } } finally { [void][QhsusbOne]::SetupDiDestroyDeviceInfoList($set) } Write-Output 'OK'