How to programatically detect and locate the Windows 10 SDK?

回眸只為那壹抹淺笑 提交于 2019-12-02 17:19:22

I don't have an authoritative source for it, but it traces back from the uninstaller of the Windows 10 SDK, and it checked out on a couple of machines I looked at:

HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows Kits\Installed Roots
KitsRoot10="C:\Program Files (x86)\Windows Kits\10\"

The exact version is a bit trickier to guess, since for example 10.0.10150.0, 10.0.10240.0 and 10.0.10586.0 are all subdirectories under C:\Program Files (x86)\Windows Kits\10\Include.

As of now, the best way I've found is to look at how Visual Studio's dev command does it. Currently (2019) this is found in winsdk.bat under .\Common7\Tools\vsdevcmd\core\ in your Visual Studio directory. This should help if things are changed again in the future. Even if winsdk.bat is renamed or reorganised, hopefully similar scripts will still be included.

On my system the relevant section currently looks like this:

:GetWin10SdkDir

call :GetWin10SdkDirHelper HKLM\SOFTWARE\Wow6432Node > nul 2>&1
if errorlevel 1 call :GetWin10SdkDirHelper HKCU\SOFTWARE\Wow6432Node > nul 2>&1
if errorlevel 1 call :GetWin10SdkDirHelper HKLM\SOFTWARE > nul 2>&1
if errorlevel 1 call :GetWin10SdkDirHelper HKCU\SOFTWARE > nul 2>&1
if errorlevel 1 exit /B 1
exit /B 0


:GetWin10SdkDirHelper

@REM `Get Windows 10 SDK installed folder`
for /F "tokens=1,2*" %%i in ('reg query "%1\Microsoft\Microsoft SDKs\Windows\v10.0" /v "InstallationFolder"') DO (
    if "%%i"=="InstallationFolder" (
        SET WindowsSdkDir=%%~k
    )
)

@REM `get windows 10 sdk version number`
setlocal enableDelayedExpansion

@REM `Due to the SDK installer changes beginning with the 10.0.15063.0 (RS2 SDK), there is a chance that the
@REM Windows SDK installed may not have the full set of bits required for all application scenarios.
@REM We check for the existence of a file we know to be included in the "App" and "Desktop" portions
@REM of the Windows SDK, depending on the Developer Command Prompt's -app_platform configuration.
@REM If "windows.h" (UWP) or "winsdkver.h" (Desktop) are not found, the directory will be skipped as
@REM a candidate default value for [WindowsSdkDir].`
set __check_file=winsdkver.h
if /I "%VSCMD_ARG_APP_PLAT%"=="UWP" set __check_file=Windows.h

if not "%WindowsSdkDir%"=="" for /f %%i IN ('dir "%WindowsSdkDir%include\" /b /ad-h /on') DO (
    @REM `Skip if Windows.h|winsdkver (based upon -app_platform configuration) is not found in %%i\um.`
    if EXIST "%WindowsSdkDir%include\%%i\um\%__check_file%" (
        set result=%%i
        if "!result:~0,3!"=="10." (
            set SDK=!result!
            if "!result!"=="%VSCMD_ARG_WINSDK%" set findSDK=1
        )
    )
)

My Windows Kits key did not contain any information about the v10 Windows SDK. However, I found information about it at the following registry path:

HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Microsoft SDKs\NETFXSDK

Inside, there are subkeys for installed .NET versions. I had 4.6, 4.6.1, and 4.6.2.

Each of those keys contains a value InstallationFolder which points to the folder where the SDK is installed to (this would be C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\), and a value KitsInstallationFolder which contains the path to the kit containing the libraries (C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6\).

More interestingly (for me at least), each of those keys also contains three more keys: WinSDK-NetFx40Tools, WinSDK-NetFx40Tools-x64, and WinSDK-NetFx40Tools-x86. And each of those keys has a value InstallationFolder that contains the absolute path to the tools folder of the specified SDK version.

So for my use case, I’m now doing the following in PowerShell to get the folder to the latest SDK Tools:

$WindowsSdkToolsPath = gci 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Microsoft SDKs\NETFXSDK' |
    select -Last 1 |
    gci | ? { $_.PSChildName -eq 'WinSDK-NetFx40Tools' } |
    Get-ItemPropertyValue -Name InstallationFolder
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!