Violet Giraffe
Violet Giraffe

Reputation: 33579

How to set the default Windows kit (SDK) version?

I used to use Windows 8.1 SDK for my C++ application, and everything's working fine. Today I installed the Windows 10 SDK and I can't find a way to make it the default one.

I can hard-code the new SDK path in the Visual Studio project settings, but that is highly undesirable. I want the new kit to be used by default for every new project. There's no environment variable for the SDK, and I can't find anything in the registry, either.
More precisely, there are Windows SDK entries in the registry, but what I need - the C++ includes and libraries - is called the Windows Kit (located in C:\Program Files (x86)\Windows Kits).

Upvotes: 13

Views: 16218

Answers (3)

George Robinson
George Robinson

Reputation: 2125

Take a look at the small batch file listed below. It invokes another batch file winsdk.bat, which is written by Microsoft and part of the Visual Studio installation and is the "official way" which Visual Studio uses to detect and choose the installed SDKs. TL;DR: It uses a mix of registry and file system parsing.

Each version of Visual Studio uses a different winsdk.bat but they are very similar to each other after VS2015.

If you study the way this Microsoft's batch file works, you will discover methods to influence its SDK discovery and selection mechanisms.

@echo off
REM If you don't have it installed already, you can download VSWHERE.EXE from https://github.com/microsoft/vswhere

for /f "usebackq tokens=*" %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -legacy -property resolvedInstallationPath`) do (
  set VSinstallDir=%%i
)
echo The latest Visual Studio is installed in: %VSinstallDir%

set VSCMD_ARG_HOST_ARCH=x64
set VSCMD_ARG_TGT_ARCH=x64

REM This call actually does the job of detecting the SDKs that you have installed on your system and picks the default one.
call %VSinstallDir%\Common7\Tools\vsdevcmd\core\winsdk.bat

echo WindowsSDKVersion=%WindowsSDKVersion%
echo WindowsSDKLibVersion=%WindowsSDKLibVersion%
echo UCRTVersion=%UCRTVersion%
echo WindowsSdkDir=%WindowsSdkDir%
echo UniversalCRTSdkDir=%UniversalCRTSdkDir%
echo WindowsSdkBinPath=%WindowsSdkBinPath%
echo WindowsSdkVerBinPath=%WindowsSdkVerBinPath%
echo WindowsLibPath=%WindowsLibPath%

You can set the VSCMD_ARG_HOST_ARCH and VSCMD_ARG_TGT_ARCH variables to =x86 or =arm or =arm64 if you so desire.

You can step through and debug your winsdk.bat file with the CMDebug tool available at: https://jpsoft.com/all-downloads/all-downloads.html ( NOTE: the v25 works with all versions of Windows ). Remember to execute the set VSCMD_ARG_HOST_ARCH=x64 and set VSCMD_ARG_TGT_ARCH=x64 commands using the menu Debug --> Evaluate Command... inside the CMDebug.

Upvotes: 1

Jeembo Jones
Jeembo Jones

Reputation: 41

Ok, it seems not a lot of people face this problem, but I'll still post a workaround to set default SDK version. It worked with Visual Studio 2017 Community. Consider the following situation:

  • You've got a solution with projects you must not retarget

  • You had older VS installed earlier on your PC

  • When you open the solution, VS sets SDK version as 8.1 for some reason, while you use Windows SDK 10

  • When you try to build you have the following:

Error MSB8036 The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution".

because SDK 8.1 is not installed properly on your PC, and reinstalling it somehow does not solve the problem.

  • Others on your team don't have such a problem as the projects don't have Windows SDK version explicitly defined inside *.vcxproj files.

So, obviously, MS build system with multiple SDKs installed has some kind of confusion, and the worst part is that it results in defining corruptly installed SDK as your default. I used the following workaround to set default windows SDK explicitly:

  • Go to [VS installment path]\Common7\IDE\VC\VCTargets ,example:

C:\Program Files (x86)\Microsoft Visual Studio\2017\WDExpress\Common7\IDE\VC\VCTargets\Microsoft.Cpp.WindowsSDK.props)

  • Open file Microsoft.Cpp.WindowsSDK.props as an administrator
  • Find line
    <DefaultWindowsSDKVersion Condition="'$(DefaultWindowsSDKVersion)' == ''
    and '$(AppContainerApplication)' != 'true'">8.1</DefaultWindowsSDKVersion>

change 8.1 to the SDK version you're sure you have properly installed, then save the file. In my case it was 10.0.17763.0, so the final line was

    <DefaultWindowsSDKVersion Condition="'$(DefaultWindowsSDKVersion)' == ''
    and '$(AppContainerApplication)' != 'true'">10.0.17763.0</DefaultWindowsSDKVersion>

Now reopen your solution and try to build it. Should work fine.

Upvotes: 3

GuidedHacking
GuidedHacking

Reputation: 3923

My first troubleshooting tips would be to uninstall all the Visual Studio stuff you have, then reboot. Then install the latest version of Visual Studio. Then install any other SDKs that you need via the Visual Studio installer wizard. If you require to have Visual Studio 2015 and 2019 installed, go ahead and install 2015 before you install 2019.

If you want to change the project templates, you can in fact do that. The folder for the default templates is here:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\ProjectTemplates\VC\WindowsDesktop\

Let's say you want to modify the ConsoleApplication template. Create a new project, in this new project edit the Project Property "SDK version" to be 8.1. Save the project and then go to Project->Export Template. Export the template. It will be a zip file in Documents\Visual Studio 2019\My Exported Templates. Unzip it.

You will notice the .vstemplate file in this folder which is similar to the found in the directory above. You will see in the node it will reference a .vcxproj file. In this .vcxproj file you will find the property:

<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>

Therefore in order to modify the default templates to match the template you just exported you will need to add the necessary files and lines XML from the My Exported Templates files and overwrite the defaults in Program Files. These will probably be overwritten each time you update VS tho.

Alternatively just extract the zip file of template you exported to the folder:

ProjectTemplates\VC\WindowsDesktop\

And you will find it in your templates after you reload visual studio

Upvotes: 1

Related Questions