Brad
Brad

Reputation: 4202

Programmatically finding the VS2017 installation directory

With previous versions of VS you could query the registry to determine the installation directory for VS:

HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0

However, this doesn't seem to work with the VS2017 RC. We have scripts that detect the latest installed VS and then do "the right thing", and so far I'm having issues plugging VS2017 into those systems.

Does anyone know how to programmatically determine the installation location for VS2017?

Upvotes: 11

Views: 15603

Answers (10)

BeErikk
BeErikk

Reputation: 94

Well, vswhere.exe doesn't really supply more than the Visual Studio edition installation path. Here's my .profile file Interix snippet from 2008 doing the same with a minor update (shell script):

if [[ -n $PROCESSOR_ARCHITEW6432 || $PROCESSOR_ARCHITECTURE != "x86" ]]; then
  hkeybase='HKLM\SOFTWARE\Wow6432Node\Microsoft\'
else
  hkeybase='HKLM\SOFTWARE\Microsoft\'
fi
for vsver in "15.0" "14.0" "12.0" "11.0" "10.0" "9.0" "8.0"; do
  _vsinstalldir=$(reg.exe query ${hkeybase}'VisualStudio\SxS\VS7' -v $vsver 2>/dev/null \
   | sed -n 's|.*REG_SZ *\([ [:print:]]*\).*|\1|p' | sed 's|\\|/|g')
if [[ -n $_vsinstalldir ]]; then break; fi
done; unset vsver

That's enumerating Visual Studio installations favouring the latest in registry key

HKLM\SOFTWARE\Microsoft\VisualStudio\SxS\VS7

Still working for Visual Studio 2017. Would be easy to translate to cmd syntax. To query the registry is simpler and doesn't require vswhere.exe in your path, thus favourable IMO.

Now finding the current Visual C++ instance and the SDKs is another task entirely. :D

Common output in case you wonder:

C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/

Edit 2025-01-30:

To find Visual Studio paths and Windows SDK. Here is my current WSL Bash .profile, which give access to the MS-compiler in WSL. Useful if you are using clang in WSL for Windows compiles. It also gives proper INCLUDE and LIB environment. Useful parts can be translated to CMD.

# shellcheck shell=bash 
# ~/.profile: executed by the command interpreter for login shells.
# Jerker Bäck 2017

export TMP=$TMPDIR  # override
vswhere='/mnt/c/Program Files (x86)/Microsoft Visual Studio/Installer/vswhere.exe'
vsinstalldir=$(wslpath "$("$vswhere" -latest -property installationPath 2>/dev/null | sed 's:\\:/:g' | tr -d '\r\n')")
vctoolsversion=$(head -n 1 "${vsinstalldir}/VC/Auxiliary/Build/Microsoft.VCToolsVersion.default.txt" | tr -d '\r\n')
export VCToolsInstallDir="$vsinstalldir/VC/Tools/MSVC/$vctoolsversion"  # used by clang
kitskey='HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows Kits\Installed Roots'
kitsroot10="$(wslpath "$(/mnt/c/Windows/System32/reg.exe query "$kitskey" -f 'KitsRoot10' 2>/dev/null | sed -n 's:.*REG_SZ *\([[:print:]]*\).*:\1:p' | sed 's:\\:/:g')")"
kitsversion=$(sed -n -e '/PlatformIdentity/ s:.*Version\=*::p' "${kitsroot10}SDKManifest.xml" | sed 's:"[[:space:]]*::g')
export kitsroot10
export kitsversion
# clang Windows support expect semicolon delimiters => WSLENV fails
export INCLUDE="$VCToolsInstallDir/include;${kitsroot10}Include/$kitsversion/ucrt;$winsdkalt1;$winsdkalt2;$winsdkalt3;$winsdkalt4;$winsdkalt5;${kitsroot10}Include/$kitsversion/um;${kitsroot10}Include/$kitsversion/shared;${kitsroot10}Include/$kitsversion/km;${kitsroot10}Include/$kitsversion/winrt;${kitsroot10}Include/$kitsversion/cppwinrt;$VCToolsInstallDir/atlmfc/include;/mnt/f/libraries/include"
export LIB="$VCToolsInstallDir/lib/x64;${kitsroot10}Lib/$kitsversion/ucrt/x64;${kitsroot10}Lib/$kitsversion/um/x64;${kitsroot10}Lib/$kitsversion/shared/x64;/mnt/f/libraries/lib/x64"
export CL="-O1 -Ois -EHsc -utf-8 -std:c++latest -MP -permissive- -Zc:__cplusplus -Zc:enumTypes -Zc:externConstexpr -Zc:inline -Zc:lambda -Zc:preprocessor -Zc:throwingNew"
export _CL_="-link -subsystem:console -machine:x64"
# export CFLAGS="--target=x86_64-pc-windows-msvc -std=c++20 -isystem"$VCToolsInstallDir/include" -isystem"${kitsroot10}Include/$kitsversion/ucrt" -isystem"${kitsroot10}Include/$kitsversion/um" -isystem"${kitsroot10}Include/$kitsversion/shared" -isystem"${kitsroot10}Include/$kitsversion/km" -isystem"${kitsroot10}Include/$kitsversion/winrt" -isystem"${kitsroot10}Include/$kitsversion/cppwinrt" -isystem"$VCToolsInstallDir/atlmfc/include" -isystem"/mnt/f/libraries/include""
export WSLENV=$WSLENV:INCLUDE/l:LIB/l:CL:_CL_:TMPDIR/p:TMP/p
export CMAKE_TOOLCHAIN_FILE="/mnt/f/libraries/buildsystem/vcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake"
# strip path to Ubuntu system to avoid pollution of Windows versions of tools
wsladd="$HOME/.nvm/versions/node/v14.17.3/bin:/mnt/c/Program Files/WindowsApps/MicrosoftCorporationII.WindowsSubsystemForLinux_1.1.3.0_x64__8wekyb3d8bbwe:/snap/bin"
winpath="/mnt/c/Windows/System32:/mnt/c/Windows:/mnt/c/Program Files/PowerShell/7:/mnt/c/dev/vs.code/bin"
ubuntusys="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
msdevpath="$VCToolsInstallDir/bin/Hostx64/x64:${kitsroot10}bin/$kitsversion/x64"
export PATH="$ubuntusys:$msdevpath:$winpath:$wsladd"
unset vswhere vsinstalldir vctoolsversion kitskey ubuntusys msdevpath winpath wsladd

Upvotes: 2

it3xl
it3xl

Reputation: 2672

We have only 3 Visual Studio editions.

  • \Community
  • \Professional
  • \Enterprise

As such we can simplify everything a bit.
Here some tested CMD script.

@SET env_all_vs2017_root=%ProgramFiles(x86)%\Microsoft Visual Studio\2017
@SET env_vs2017_path="%env_all_vs2017_root%\Professional"
@IF NOT EXIST %env_vs2017_path%  SET env_vs2017_path="%env_all_vs2017_root%\Community"
@IF NOT EXIST %env_vs2017_path%  SET env_vs2017_path="%env_all_vs2017_root%\Enterprise"
@REM Let's fail laudly
@IF NOT EXIST %env_vs2017_path%  SET "env_vs2017_path=Visual Studio 2017 install path was not found by %~nx0"

@REM  You may want to remove quotes
@SET unquoted=%env_vs2017_path:"=%


@REM  And now let's see the result and PAUSE

@ECHO VS 2017 install path is
@ECHO %unquoted%

@PAUSE

Upvotes: 0

TarmoPikaro
TarmoPikaro

Reputation: 5273

See following answer in here:

https://stackoverflow.com/a/55754831/2338477

You can use either command line tool for querying Visual studio location, but I also provide programmatic way to query Visual Studio location. Code is based upon vswhere source code, but simplified.

Upvotes: 0

mdrissel
mdrissel

Reputation: 31

I had a devil of time trying to modify Srekel's answer to search for only VS2017. Note: If you put the "for" statement below inside an "if" block it will wreck the escape characters and won't work.

SETLOCAL EnableDelayedExpansion

if not exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
  echo "WARNING: You need VS 2017 version 15.2 or later (for vswhere.exe)"
)

set vswherestr=^"!ProgramFiles(x86)!\Microsoft Visual Studio\Installer\vswhere.exe^" -version [15.0,16.0^^) -products * -requires Microsoft.Component.MSBuild -property installationPath
for /f "usebackq tokens=*" %%i in (`!vswherestr!`) do (  
  set BUILDVCTOOLS=%%i\Common7\Tools
  echo BUILDVCTOOLS: !BUILDVCTOOLS!
  if not exist !BUILDVCTOOLS!\VsDevCmd.bat (
    echo Error: Cannot find VS2017 Build Tools
    goto :buildfailed
  )
  call "!BUILDVCTOOLS!\VsDevCmd.bat"
)    

Upvotes: 1

KindDragon
KindDragon

Reputation: 6881

You can use vswhere tool to get VS2017 location.

Example:

@echo off

rem VS2017U2 contains vswhere.exe
if "%VSWHERE%"=="" set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"

for /f "usebackq tokens=*" %%i in (`"%VSWHERE%" -latest -products * -requires Microsoft.Component.MSBuild -property installationPath`) do (
  set InstallDir=%%i
)

if exist "%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" (
  "%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" %*
)

You can read more about it here: https://blogs.msdn.microsoft.com/heaths/2017/02/25/vswhere-available/

Upvotes: 15

Srekel
Srekel

Reputation: 2303

KindDragon's solution didn't quite work for me due to batch's "delayed expansion" "feature". (WAT)

Here is my code, compatible with VS 2017 15.2 (for the vswhere.exe installation)

SETLOCAL EnableDelayedExpansion

if not exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
  echo "WARNING: You need VS 2017 version 15.2 or later (for vswhere.exe)"
)

for /f "usebackq tokens=*" %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -requires Microsoft.Component.MSBuild -property installationPath`) do (
  set InstallDir=%%i
)

if exist "!InstallDir!\VC\Auxiliary\Build\vcvars64.bat" (
  call "!InstallDir!\VC\Auxiliary\Build\vcvars64.bat"
) else (
  echo "Could not find !InstallDir!\VC\Auxiliary\Build\vcvars64.bat"
)

Especially note usage of SETLOCAL EnableDelayedExpansion and !InstallDir!

Upvotes: 1

DOMZE
DOMZE

Reputation: 1369

I use powershell like KindDragon suggested

$Is64bitOs = $env:PROCESSOR_ARCHITEW6432 -eq 'AMD64';
if ($Is64bitOs){
    $registryPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0";
}
else {
    $registryPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\14.0";
}

$VSInstallDir = (Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0 -Name ShellFolder).ShellFolder;

Upvotes: -3

Refael Ackermann
Refael Ackermann

Reputation: 1578

May I recommend my package get-vs2017-path it uses only built-in Windows tools (and although it's built as an NPM package, it has no dependencies, and the tools folder works standalone)

Upvotes: 0

KindDragon
KindDragon

Reputation: 6881

You can use this PowerShell snippet for finding the VS2017 installation directory:

$vssetup_path = "$([Environment]::GetFolderPath("MyDocuments"))\WindowsPowerShell\Modules\VSSetup"
if (-not (Test-Path $vssetup_path -pathType container))
{
    iwr https://github.com/Microsoft/vssetup.powershell/releases/download/1.0.36-rc/VSSetup.zip -OutFile "$env:TEMP\VSSetup.zip"
    Expand-Archive "$env:TEMP\VSSetup.zip" $vssetup_path
}
$vs2017 = Get-VSSetupInstance -All | Select-VSSetupInstance -Require 'Microsoft.VisualStudio.Workload.NativeDesktop' -Version '[15.0,16.0)' -Latest
"Installation Path: " + $vs2017.InstallationPath
#`vsdevcmd.bat -arch=x86` or `vsdevcmd.bat -arch=amd64` can be used to setup path's to VC++ compiler
"VsDevCmd.bat Path: " + $vs2017.InstallationPath + "\Common7\Tools\VsDevCmd.bat"

Upvotes: -2

sean e
sean e

Reputation: 11935

Visual Studio 2017 supports no-registry, side-by-side installations of all SKUs (Enterprise, Professional and Community).

MSI installlers can query via APIs described here: https://blogs.msdn.microsoft.com/heaths/2016/09/15/changes-to-visual-studio-15-setup/

Examples are here:

Upvotes: 5

Related Questions