Robust path to visual studio's vcvars64.bat - visual-studio

VisualStudio has vcvars64.bat that loads all necessary environment variables to compile visual studio projects from the command line.
In VisualStudio 2017 community edition, this file for example resides in
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat
How can I construct the path to this file in a batch script, only using system set environment variables, independently of which visual studio version is installed?

You can use vswhere to detect the install location of Visual Studio related files. This snippet from the wiki comes pretty close to what you need:
for /f "usebackq delims=" %%i in (`vswhere.exe -prerelease -latest -property installationPath`) do (
if exist "%%i\Common7\Tools\vsdevcmd.bat" (
%comspec% /k "%%i\Common7\Tools\vsdevcmd.bat" %*
exit /b
)
)
rem Instance or command prompt not found
exit /b 2
Changing that snippet to call vcvars should be easy.
If you have multiple Visual Studio installations that may not have the C++ workload installed, you can search for an instance with a specific workload or component, like this one looking for Team Explorer to be part of the installation:
vswhere -latest -products * -requires Microsoft.VisualStudio.TeamExplorer -property installationPath
The Microsoft C++ team wrote a blog about vswhere and alternative options. They also list all of the relevant workload and component identifiers.
The idea is that you ship vswhere.exe with your scripts or executable. Or downlead it on demand. I use this powershell snippet in a few places to grab vswhere when I need it:
$vswhereLatest = "https://github.com/Microsoft/vswhere/releases/latest/download/vswhere.exe"
$vswherePath = ".\vswhere.exe"
remove-item $vswherePath
invoke-webrequest $vswhereLatest -OutFile $vswherePath
The reason behind this, is that you can now install multiple instances of the same Visual Studio Version. Each stores its settings in a private registry file. There is no easy way to get to the InstallPath registry key that used to exist. More background can be found here. Complete with a few reg.exe commands to load the private registry hyves and query them. It you really can't package or fetch vswhere on demand, this may be your only option (add 16.*to include 2019)
for /d %%f in (%LOCALAPPDATA%\Microsoft\VisualStudio\15.0_*) do reg load HKLM\_TMPVS_%%~nxf "%%f\privateregistry.bin"
REM Use `reg query` to grab the InstallPath from the instance
for /d %%f in (%LOCALAPPDATA%\Microsoft\VisualStudio\15.0_*) do reg unload HKLM\_TMPVS_%%~nxf
Note: Looks like this may need to be updated to search for 15.*_* and 16.*_* to detect point releases as well as Visual Studio 2019.

Related

Visual studio 2017 Developer Command Prompt switches current directory

I am getting weird behavior of "Developer Command Prompt for VS 2017" command line tool. Normally in previous versions of visual studio this script (VsDevCmd.bat) was not messing current directory from where you run it. Now it seems to change it. One simple workflow would be just to start the shortcut "Developer Command Prompt for VS 2017" and it doesn't honor the "Start in" directory:
By any chance anyone seen this issue? It really bugs me because I used to have a shortcut with it and start CMD in my source directory, use TFS/msbuild commands afterwards.
You can set the VSCMD_START_DIR environment variable to have vsdevcmd.bat change to that directory when it finishes. Otherwise it will check if you have a %USERPROFILE%\source directory and change to that (which is what you see).
You can change the "Target" for the "Developer Command Prompt for VS 2017" to something like the following to have change to a particular directory:
%comspec% /k "set VSCMD_START_DIR=C:\temp && "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\VsDevCmd.bat""
Note that the path to vsdevcmd.bat needs to be put in an additional set of double quotes.
Alternatively, rename or remove the %USERPROFILE%\source directory (Note that this seems to be some kind of new "standard" directory for sources), that will make vsdevcmd.bat honor the "Start In"-value (i.e. "current directory").
:: Some rocket scientist in Redmond made the VS15 VsDevCmd.bat change the cwd; the pushd/popd fixes that.
pushd %CD%
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\VsDevCmd.bat"
popd
"C:\Program Files (x86)\CruiseControl.NET\server\ccnet.exe" %*
vsdevcmd_end.bat, REM out the line:
cd /d "%USERPROFILE%\Source"
#REM Set the current directory that users will be set after the script completes
#REM in the following order:
#REM 1. [VSCMD_START_DIR] will be used if specified in the user environment
#REM 2. [USERPROFILE]\source if it exists
#REM 3. current directory
if "%VSCMD_START_DIR%" NEQ "" (
cd /d "%VSCMD_START_DIR%"
) else (
if EXIST "%USERPROFILE%\Source" (
cd /d "%USERPROFILE%\Source"
)
)
If I see your quastion in right way that I'm used to work with EntityFramework Core with the help of CMD or PowerShell. For this you should right-click In the Solution Explorer panel then in the context menu, click Open Folder in File Explorer. In the address bar of the file explorer type cmd or powershell, you will start the command line from the project folder.

vsDiffMerge.exe /m merge option is working like /t option?

I am using Visual Studio 2013 Ultimate Update 5
I was using successfully vsDiffMerge.exe as the diff and merge tool both in Sourcetree as well as from command line (using the /t and the /m options)
All of a sudden (only thing I can note is Windows update "KB4015217") the vsDiffMerge stopped working in case of merge (the /m option) and instead it started behaving as if it is an automated merge followed by a diff !!
The command line syntax
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\vsDiffMerge.exe" "$fremote" "$flocal" "$fbase" "$fmerge" /m /t
would generate a strange window such as this
Output of the vsDiffMerge.exe with the /m /t option, same if we use only /m
We have tried all the tricks in the book: devenv /resetuserdata , reset all settings from import/export, reset Window layout, and doing a complete VS Repair
Any suggestion how to go about solving this problem ?
We ended moving to P4Merge to get a working tool for this.
I will close this thread.

Building a driver

I have some issue in a driver & I have to troubleshoot it, before that I need to build it. I have no experience in driver.
I have the source code & bat file. It might be using WDK 7(I'm not sure)
Can anyone guide me in building the driver project with below bat file.
Ofcourse I can understand bat file is setting environment variables & calling build.exe
My main questions are:
1. Which WDK i need to install, so that I can specify its path in bat file
2. How to run bat file, do i need to run in visual studio command prompt or how?
if you give some Some general idea on building a driver for a beginner that would be much appreciated. Thanks.
[if you need any info, which i can provide please feel free to ask]
bat fie:
rem #echo off
rem --------------------------------------------------------------------
rem SafeBoot Windows NT 32 bit driver build script
rem --------------------------------------------------------------------
set MC_ENV=fre
if "%1%"=="debug" goto dbg
if "%2%"=="debug" goto dbg
if "%3%"=="debug" goto dbg
goto nodbg
:dbg
echo **** DEBUG BUILD ****
set MC_ENV=chk
:nodbg
rem --------------------------------------------------------------------
rem We want the absolute path of this directory, so we use a little utility
rem that creates a batch file that sets this into a environment variable.
rem --------------------------------------------------------------------
..\Translations\Bin\setcd -d DRV_DIR > thisdir.bat
call thisdir
del thisdir.bat
if NOT "%DDK64%"=="" goto x1
set DDK64=D:\Tools\WinDDK\6000
rem set DDK64=C:\WinDDK\6001.18001
:x1
set MSTOOLS=%MSSdk%
set BASEDIR=
set DDKBUILDENV=
set NTDBGFILES=
set DDK_INC_PATH=
set DDK_LIB_DEST=
set DDK_LIB_PATH=
set CRT_INC_PATH=
set CRT_LIB_PATH=
set BUILD_ALT_DIR=
call %DDK64%\bin\setenv %DDK64% %MC_ENV% i386
echo %DRV_DIR%
cd %DRV_DIR%
if NOT "%1"=="/a" goto build
del /q objfre_wlh_x86\i386\*.*
del /q objchk_wlh_x86\i386\*.*
rmdir /Q /S driver32
md driver32
:build
rem set MSC_OPTIMIZATION=/Od
echo Building started...
%DDK64%\bin\x86\build.exe -f -z -E
echo Building completed...
if ERRORLEVEL 1 goto error
rem copy objfre_wlh_x86\i386\McPvDrv.sys driver32\McPvDrv.sys
if "%MC_ENV%"=="chk" (
md ..\..\..\build\Win32\Debug
copy objchk_wlh_x86\i386\McPvDrv.sys ..\..\..\build\Win32\Debug\McPvDrv.sys
echo chk
If you want to build a driver for Windows 7 or newer use Visual Studio 2015 which is integrated with the lastest WDK 10. Building drivers is way easier this way.
If you want to build drivers for Windows XP then you will have to use WDK 7 and use a scripts similar like yours. Wdk 7 has no integration with visual studio. You have to specify source file in a special file called sources. See example drivers from Wdk 7. You can find a good cmd for building such drivers https://www.osronline.com/article.cfm?article=43 This website has the best driver dev tips
If you do not need XP support I highly recommend you go the much much easier way using Visual Studio 2015, windows sdk and windows 10 wdk.
LE:
You can also use Visual Studio 2013 which supports integration with WDK 8 and WDK 8.1 but not with WDK 10.
From your .bat file, it has:
if NOT "%DDK64%"=="" goto x1
set DDK64=D:\Tools\WinDDK\6000
rem set DDK64=C:\WinDDK\6001.18001
:x1
So, it might be looking for that version. If so, here is a page: What is the Microsoft name for WinDDK version 6001.18001?
Also, see https://en.wikipedia.org/wiki/Windows_Driver_Kit Your .bat wants, by default, build 6000 (released: november 29, 2006 for Windows Vista)
You could also try to use the latest one. Here's the link for WDK 10 with download instructions. WDK 7 should be similar: https://msdn.microsoft.com/en-us/library/windows/hardware/ff557573%28v=vs.85%29.aspx so find the right page. This page also has links to other pages that may help answer your other questions.
In any case, you'll need to modify the .bat to point to the correct WinDDK directory by setting the DDK64 variable to point to it.
You can probably install multiple different versions to different directories, but my guess is that the latest will let you build backward compatible versions. So, if you're building for Win7 (e.g.), get the latest DDK for Win7 (see the table in the wiki page).

XCOPY and VS 2013 Post-Build Errors

As much as this topic has been talked about, I would think that someone else has run into the same issue as me and found a solution.
This XCOPY Script works in another environment using VS 2012 (with macros) and the same environment using the command line.
XCOPY "C:\Web.Forms\bin\Web.Forms.dll" "C:\inetpub\WebSite\bin" /Y /R
XCOPY "C:\Web.Forms\UserControls\*.ascx" "C:\inetpub\WebSite\usercontrols" /Y /R
It keeps throwing an error of 4 and does not copy within VS 2013. If I change it to "COPY" I get an error of 1. For the purpose of testing and to use it in the command line I stripped it of all Macro's. Actually, I used the [macro] generated directory strings from the error message successfully in the command line.
So why doesn't this work in Visual Studio 2013? VS is running as Administrator.

What are the command line arguments for TFS default merge tool for VS 2012?

I am trying to add a merge tool in VS (Tools -> Options, Select Source Control -> Visual Studio Team Foundation, Click Configure User Tools), so that I can run the diff and merge correctly from the command line. The default tool works great from the VS editor, but not the command line, unless we add these settings.
I have read a lot of good things about the new merge tool provided by VS 2012. The tool is located at:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\vsDiffMerge.exe
But I can't seem to get the command line arguments right.
As per the links provided by Jehan LINK1 and the information LINK2, the command line should be this:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\vsDiffMerge.exe
%1 %2 %3 %4 /m /t
However in the link above I noticed a comment mentioning that for VS2012, the person had to use the below command line to get it working:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\vsDiffMerge.exe
/t /m %1 %2 %3 %3 %3 %4
%1 = original
%2 = modified
%3 = base
%4 = merged
P.S: I dont have VS2012 and have not tested this. So cannot vouch for the answer, please test and let me know if it doesnt work so that it will help other people looking for the answer.
Please check below post which explains about vsDiffMerge.exe commands.
http://roadtoalm.com/2013/10/22/use-visual-studio-as-your-diff-and-merging-tool-for-local-files/

Resources