Visual Studio 2022 + Windows SDK 22621 安装血泪史

📅 2026/7/11 6:35:37 👁️ 阅读次数 📝 编程学习
Visual Studio 2022 + Windows SDK 22621 安装血泪史

记录一次非标准路径安装 VS2022 和 Windows SDK 的踩坑全过程,以及 WPT Ghost 残留问题的终极解决方案。

背景

目标很简单:

  • 安装Visual Studio 2022 CommunityE:\TOOLS\vs2022Comunity

  • 安装Windows SDK 10.0.22621

  • 排除不需要的 SDK 版本(26100)和 Windows Performance Toolkit(WPT)

看起来是标准操作,但实际过程充满了各种诡异的错误。


第一关:WPT Ghost 弹窗地狱

现象

在安装过程中,每次涉及 WPT(Windows Performance Toolkit)的操作都会弹出一个 Windows Installer 错误框:

The path 'C:\ProgramData\Package Cache\{EC12C121-3208-5E92-FCB0-0591769632F9}\ v10.1.18362.1\Installers\WPTx64-x86_en-us.msi' cannot be found.

点了无数次"取消"按钮,弹窗又冒出来,简直是无限弹窗地狱。

原因分析

系统之前安装过旧版 WPT(v10.1.18362.1),但 MSI 安装源文件被清理了(C:\ProgramData\Package Cache中的文件已被删除)。Windows 留下了两个ghost 产品残留在 MSI 数据库中:

Ghost ID产品
{EC12C121-3208-5E92-FCB0-0591769632F9}WPTx64
{A8E21603-6CBA-D168-ADF7-108A1DA16DB5}WPT Redistributables

每当安装程序试图修复、升级或卸载这些产品时,MSI 就会尝试查找安装源文件,找不到就弹窗。

尝试过的方案(均失败)

1. 清理注册表 Uninstall 项

删除了HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall中对应的条目。

结果:无效。这些只是显示层面的清理,MSI 内部数据库仍然记录着这些产品。

2. 在 VS Installer 中排除 WPT 组件
setup.exe modify --installPath "E:\TOOLS\vs2022Comunity" ` --remove Microsoft.VisualStudio.Component.Windows11Sdk.WindowsPerformanceToolkit

结果:WPT 标记为 NotSelected,但其他操作仍然会触发 ghost 的修复尝试。

3. 创建假 MSI 文件

C:\ProgramData\Package Cache\{EC12C121-...}\下放置了一个占位 MSI 文件,试图骗过 MSI。

结果:MSI 验证文件签名/哈希失败,弹窗继续。

4. winget 安装 SDK
winget install Microsoft.WindowsSDK.10.0.22621

结果:错误 1618 — 另一个 MSI 安装正在进行中(WPT ghost 的残留进程占用了 MSI 互斥锁)。

5. 独立 SDK 安装器

下载并运行winsdksetup.exe /quiet

结果:静默安装后无报错,但 SDK 并未实际安装,同样被 WPT ghost 阻塞。


终极方案:深度清理 MSI 数据库

经过研究,WPT ghost 的根源不在注册表 Uninstall 项,而在MSI 内部数据库的三个位置:

  1. HKLM\SOFTWARE\Classes\Installer\Products\<ProductCode>

  2. HKLM\SOFTWARE\Classes\Installer\Features\<ProductCode>

  3. HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\<ComponentGUID>

ProductCode 计算规则

MSI 注册表中的 ProductCode 是原始 GUID 的反转字节序形式。

原始: {EC12C121-3208-5E92-FCB0-0591769632F9} 反转: 121C21CE 8023 29E5 CF0B 50196769239F

即:将前 3 部分的每个 4 位十六进制字节序反转,后 2 部分保持原样。

深度清理脚本

# deep_clean_wpt.ps1 ​ $wptGuid = "{EC12C121-3208-5E92-FCB0-0591769632F9}" ​ # 计算反转的 ProductCode function Reverse-GuidBytes { param([string]$guid) $clean = $guid -replace '[{}]', '' $parts = $clean -split '-' $p1 = -join ($parts[0][6],$parts[0][7],$parts[0][4],$parts[0][5], $parts[0][2],$parts[0][3],$parts[0][0],$parts[0][1]) $p2 = -join ($parts[1][2],$parts[1][3],$parts[1][0],$parts[1][1]) $p3 = -join ($parts[2][2],$parts[2][3],$parts[2][0],$parts[2][1]) return "$p1$p2$p3$($parts[3])$($parts[4])" } ​ $reversedCode = Reverse-GuidBytes -guid $wptGuid ​ # 1. 删除 Products 键 $productPath = "HKLM:\SOFTWARE\Classes\Installer\Products\$reversedCode" if (Test-Path $productPath) { Remove-Item -Path $productPath -Recurse -Force Write-Host "[OK] Removed Products key" } ​ # 2. 删除 Features 键 $featurePath = "HKLM:\SOFTWARE\Classes\Installer\Features\$reversedCode" if (Test-Path $featurePath) { Remove-Item -Path $featurePath -Recurse -Force Write-Host "[OK] Removed Features key" } ​ # 3. 删除 Components 关联 $componentsPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components" Get-ChildItem $componentsPath | ForEach-Object { $value = (Get-ItemProperty -Path $_.PSPath -Name $reversedCode -ErrorAction SilentlyContinue).$reversedCode if ($value) { Remove-ItemProperty -Path $_.PSPath -Name $reversedCode -Force Write-Host "[OK] Removed Component: $($_.PSChildName)" } }

执行结果

[OK] Removed Products key [OK] Removed Features key [OK] Removed Component: B79AA12C5E96CCC4D9B263E0B3BED119 ​ SUCCESS: WPTx64 completely removed

3 个残留注册表项全部清除。另一个 ghost{A8E21603-...}在之前的操作中已被清除。


第二关:SDK 终于安装

WPT ghost 清除后,通过 VS Installer 的 modify 命令安装 SDK 22621:

& "C:\Program Files (x86)\Microsoft Visual Studio\Installer\setup.exe" modify ` --installPath "E:\TOOLS\vs2022Comunity" ` --quiet --norestart ` --add Microsoft.VisualStudio.Component.Windows11SDK.22621

这一次,安装成功完成,没有弹窗!

安装位置说明

SDK 被安装到了D:\Windows Kits\10\(而非默认的C:\Program Files (x86)\Windows Kits\10\),因为注册表中KitsRoot10已经指向 D 盘。

目录结构:

D:\Windows Kits\10\ ├── Include\10.0.22621.0\ │ ├── shared\ (共享头文件) │ ├── um\ (User Mode) │ ├── winrt\ (WinRT) │ └── cppwinrt\ (C++/WinRT) └── Lib\10.0.22621.0\ └── um\x64\ (64位库文件)

第三关:VS 无法自动找到 SDK

现象

使用vcvars64.bat激活 MSVC 环境后,cl.exe编译时找不到windows.h

main.cpp(1): fatal error C1083: 无法打开包括文件: "windows.h": No such file or directory

原因

vcvars64.bat默认在$(VC_VC_ProgramFilesx86)\Windows Kits\10\Include下查找 SDK,但实际 SDK 在D:\Windows Kits\10\

解决方案

方案一:手动指定路径

cl /EHsc /std:c++17 ^ /I"D:\Windows Kits\10\Include\10.0.22621.0\um" ^ /I"D:\Windows Kits\10\Include\10.0.22621.0\shared" ^ /I"D:\Windows Kits\10\Include\10.0.22621.0\winrt" ^ /I"D:\Windows Kits\10\Include\10.0.22621.0\cppwinrt" ^ main.cpp ^ /link /LIBPATH:"D:\Windows Kits\10\Lib\10.0.22621.0\um\x64"

方案二:CMake 配置

cmake_minimum_required(VERSION 3.20) project(HelloWorld) # 手动指定 SDK 版本和路径 set(CMAKE_SYSTEM_VERSION 10.0.22621.0) add_executable(helloworld main.cpp)

方案三:修改 Windows SDK 注册表

如果希望vcvars自动识别,可以在HKLM\SOFTWARE\Microsoft\Windows Kits\Installed Roots中确认KitsRoot10指向正确路径,并确保版本子键10.0.22621.0存在。


验证结果

编译并运行 Hello World 程序:

Hello World from Visual Studio 2022! Windows SDK Version: 167772172 Build timestamp: Jul 10 2026 15:02:48
组件版本状态
MSVC 编译器19.44.35228
Windows SDK10.0.22621.0
头文件shared/um/winrt/cppwinrt
库文件um/x64

总结:关键经验

1. MSI Ghost 问题本质

MSI 产品残留存储在多个注册表位置,仅删除 Uninstall 项是不够的。必须清理:

  • HKLM\SOFTWARE\Classes\Installer\Products\<ReversedGUID>

  • HKLM\SOFTWARE\Classes\Installer\Features\<ReversedGUID>

  • HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\<GUID>

⚠️注意:MSI 注册表中的 ProductCode 使用字节反转格式,不是原始 GUID 格式!

2. 排查思路

弹窗错误 → 确定 MSI 产品 GUID → 计算反转 ProductCode → 搜索 Products/Features/Components 三个位置 → 逐个删除 → 重启 → 重新安装

3. 工具链建议

  • MSI 清理:使用msiexec /x {GUID}正常卸载,不要手动删文件

  • SDK 安装:优先通过 VS Installer 安装,而非独立安装器

  • 非标准路径:注意注册表KitsRoot10指向的位置

  • 编译验证:先写一个简单程序验证工具链完整性

4. 时间线

阶段耗时状态
初始安装 VS2022正常
WPT ghost 弹窗~2h 各种尝试
深度清理 MSI 数据库5min
SDK 22621 安装正常
编译验证2min

总计折腾时间:约 2-3 小时,其中 90% 的时间花在 WPT ghost 问题的排查和无效尝试上。


参考

  • MSI ProductCode 格式说明

  • Windows SDK 安装器命令行选项

  • VS Installer 命令行参数


写于 2026-07-10,希望这篇记录能帮助遇到同样问题的人少走弯路。