使用 WinGet 安装 WDK

可以使用 Windows 包管理器 (WinGet) 工具在计算机上安装或更新 Windows 驱动程序工具包。 WinGet 还可以安装 WDK 的依赖项,例如 Visual Studio 和 Windows 软件开发工具包。 所有这些作都可以从命令行完成。

如果需要有关确保正确安装和配置 WinGet 的帮助,请参阅 WinGet 安装文档

使用 WinGet 逐步安装最新 WDK

此步骤需要 Visual Studio 2022 和最新的 Windows SDK。 有关详细信息,请参阅 工具包版本控制

步骤 1:安装 Visual Studio 2022

WDK 需要安装带有 Desktop development with C++ 工作负载的 Visual Studio 2022,同时还需要安装 VS 2022 C++ 生成工具及其各个体系结构所需的相应 VS 2022 C++ Spectre-缓解库(最新版本)组件,用于你计划构建驱动程序的每种体系结构。

可以使用 WinGet 安装 Visual Studio 2022 以及驱动程序开发所需的所有工作负载和组件。 为此,需要一个 Visual Studio 安装配置文件。 创建一个包含以下内容的 wdk.vsconfig 文件,并记下其位置:

{
  "version": "1.0",
  "components": [
    "Microsoft.Component.MSBuild",
    "Microsoft.VisualStudio.Component.CoreEditor",
    "Microsoft.VisualStudio.Component.NuGet",
    "Microsoft.VisualStudio.Component.Roslyn.Compiler",
    "Microsoft.VisualStudio.Component.TextTemplating",
    "Microsoft.VisualStudio.Component.VC.ASAN",
    "Microsoft.VisualStudio.Component.VC.ATL.ARM64.Spectre",
    "Microsoft.VisualStudio.Component.VC.ATL.ARM64",
    "Microsoft.VisualStudio.Component.VC.ATL.Spectre",
    "Microsoft.VisualStudio.Component.VC.ATL",
    "Microsoft.VisualStudio.Component.VC.ATLMFC.Spectre",
    "Microsoft.VisualStudio.Component.VC.ATLMFC",
    "Microsoft.VisualStudio.Component.VC.CoreIde",
    "Microsoft.VisualStudio.Component.VC.MFC.ARM64.Spectre",
    "Microsoft.VisualStudio.Component.VC.MFC.ARM64",
    "Microsoft.VisualStudio.Component.VC.Redist.14.Latest",
    "Microsoft.VisualStudio.Component.VC.Runtimes.ARM64.Spectre",
    "Microsoft.VisualStudio.Component.VC.Runtimes.ARM64EC.Spectre",
    "Microsoft.VisualStudio.Component.VC.Runtimes.x86.x64.Spectre",
    "Microsoft.VisualStudio.Component.VC.Tools.ARM64",
    "Microsoft.VisualStudio.Component.VC.Tools.ARM64EC",
    "Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
    "Microsoft.VisualStudio.Component.Windows10SDK",
    "Microsoft.VisualStudio.ComponentGroup.NativeDesktop.Core",
    "Microsoft.VisualStudio.Workload.CoreEditor",
    "Microsoft.VisualStudio.Workload.NativeDesktop"
  ]
}

根据要安装的版本,需要提供 winget 一组不同的安装程序参数,以及为自定义安装而创建的 .vsconfig 文件的路径。

注意

必须在以下命令中提供 wdk.vsconfig 文件的绝对路径。 否则,WinGet 可能无法安装 Visual Studio,同时仍报告成功。

Visual Studio Community 2022

winget install --source winget --exact --id Microsoft.VisualStudio.2022.Community --override "--passive --config <vsconfig-folder>\wdk.vsconfig"

Visual Studio Professional 2022

winget install --source winget --exact --id Microsoft.VisualStudio.2022.Professional --override "--passive --config <vsconfig-folder>\wdk.vsconfig""

Visual Studio Enterprise 2022

winget install --source winget --exact --id Microsoft.VisualStudio.2022.Enterprise --override "--passive --config <vsconfig-folder>\wdk.vsconfig"

有关详情,请查阅 Visual Studio 文档如何使用 WinGet 安装或修改 Visual Studio

步骤 2:安装 Windows SDK 和 WDK

可以通过运行以下命令从 WinGet 安装 Windows SDK 和 WDK:

Windows SDK

winget install --source winget --exact --id Microsoft.WindowsSDK.10.0.26100 --log $env:USERPROFILE/Desktop/sdk-install.log

Windows WDK

winget install --source winget --exact --id Microsoft.WindowsWDK.10.0.26100 --log $env:USERPROFILE/Desktop/wdk-install.log

如果使用 VS 17.11.0 或更高版本,请取消选中安装扩展复选框。

步骤 3:安装 WDK Visual Studio 扩展

注意

仅当使用低于 17.11.0 版本的 VS 时,此部分才适用。

从命令行安装 WDK 后,需要单独安装 Windows 驱动程序工具包 Visual Studio 扩展才能生成和测试驱动程序。 默认情况下,扩展位于 %ProgramFiles(x86)%\Windows Kits\10\Vsix\VS2022\10.0.26100.0\%PROCESSOR_ARCHITECTURE%\WDK.vsix

使用命令提示符:

for /f "usebackq tokens=*" %i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -nologo -latest -products * -property enginePath`) do (
  "%i\VSIXInstaller.exe" "%ProgramFiles(x86)%\Windows Kits\10\Vsix\VS2022\10.0.26100.0\%PROCESSOR_ARCHITECTURE%\WDK.vsix"
)

使用 PowerShell:

& $(& "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -nologo -latest -products * -property enginePath | Join-Path -ChildPath 'VSIXInstaller.exe') "${env:ProgramFiles(x86)}\Windows Kits\10\Vsix\VS2022\10.0.26100.0\${env:PROCESSOR_ARCHITECTURE}\WDK.vsix"

使用 WinGet 配置文件安装完整的驱动程序开发环境

通过使用 WinGet 配置文件,只需最少的手动干预,就能为驱动程序开发设置新计算机。

为方便起见,提供了用于安装 Windows 11 版本及其依赖项的 配置文件 。 此配置设置以下组件:

  • Visual Studio 2022 Community。
  • Visual Studio 开发驱动程序所需的工作流程和组件。
  • Windows 11,版本 24H2 SDK。
  • Windows 11,版本 24H2 WDK。
  • WDK Visual Studio 扩展。

配置文件在设置新计算机时效果最佳。 但是,也可以在已安装某些组件的计算机上使用此配置文件。 WinGet 会检测已安装的组件,并且仅安装缺少的组件。

提示

可以使用 PowerShell 直接下载并安装 WDK 配置文件。 安装最新版本的 WinGet 后,可以运行以下命令:

# Open a new PowerShell terminal if needed
powershell

# Download configuration file to install WDK with VS 2022 Community
Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/microsoft/Windows-driver-samples/main/configuration.dsc.yaml' -OutFile configuration.dsc.yaml

# Install VS, SDK, WDK and WDK VS extension using the configuration file
winget configure -f configuration.dsc.yaml

步骤 1:设置 WinGet

确保要配置的计算机上安装了 WinGet 1.6 或更高版本。 可以在终端上运行 winget --version,并检查输出的版本号是否为 v1.6.2631 或更高。 如果没有,或者终端出现错误提示命令不存在,则需要安装最新版本的 WinGet,然后再继续操作。

第 2 步:下载 WDK 配置文件

配置说明文件可从此处下载。 将此文件另存为 configuration.dsc.yml 并记下其位置。

提示

提供的配置文件安装 Visual Studio 2022 社区版。 如果需要不同的版本,可以编辑 Microsoft.VisualStudio.2022.CommunityMicrosoft.VisualStudio.Product.Community 产品 ID,并为要安装的版本设置不同的 ID(专业版:Microsoft.VisualStudio.2022.ProfessionalMicrosoft.VisualStudio.Product.Professional;企业版:Microsoft.VisualStudio.2022.EnterpriseMicrosoft.VisualStudio.Product.Enterprise)。

步骤 3A: 运行 WinGet configure 以安装 WDK Configuration 文件

在保存 WDK 配置文件的目录下的命令行上运行以下命令,以便使用该文件来配置计算机:

winget configure -f configuration.dsc.yaml

步骤 3B:使用开发人员主页安装 WDK 配置文件

或者,如果安装了开发人员主页,则可以用它来配置计算机。 若要打开下载的 WDK 配置文件,请从边栏中选择 “计算机配置 ”,然后在主屏幕上 设置开发环境>配置文件 。 确认要使用该文件来配置计算机后,安装将继续进行,等到安装完成,就可以拥有一个用于驱动程序开发的环境了。

另请参阅