Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 875
Brad Avatar asked Dec 12 '16 17:12

Brad


5 Answers

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/

like image 152
KindDragon Avatar answered Oct 20 '22 00:10

KindDragon


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"
)    
like image 22
mdrissel Avatar answered Oct 20 '22 00:10

mdrissel


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/
like image 30
BeErikk Avatar answered Oct 19 '22 23:10

BeErikk


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:

  • https://code.msdn.microsoft.com/Visual-Studio-Setup-0cedd331
  • https://github.com/microsoft/vs-setup-samples
like image 31
sean e Avatar answered Oct 19 '22 23:10

sean e


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!

like image 36
Srekel Avatar answered Oct 20 '22 00:10

Srekel