观察“Visual Studio 2005 命令提示”的属性,发现运行“Visual Studio 2005 命令提示”即运行
%comspec% /k ""d:/Microsoft Visual Studio 8/VC/vcvarsall.bat"" x86
其中 comspec即%SystemRoot%/system32/cmd.exe,故以上命令行即“以x86为参数,运行vcvarsall.bat批处理文件”
查看vcvarsall.bat文件的源代码
if " %1 " == "" goto x86
if not " %2 " == "" goto usage
if /i % 1 == x86 goto x86
if /i % 1 == amd64 goto amd64
if /i % 1 == x64 goto amd64
if /i % 1 == ia64 goto ia64
if /i % 1 == x86_amd64 goto x86_amd64
if /i % 1 == x86_ia64 goto x86_ia64
goto usage
:x86
if not exist " %~dp0binvcvars32.bat " goto missing
call " %~dp0binvcvars32.bat "
goto :eof
:amd64
if not exist " %~dp0binamd64vcvarsamd64.bat " goto missing
call " %~dp0binamd64vcvarsamd64.bat "
goto :eof
:ia64
if not exist " %~dp0binia64vcvarsia64.bat " goto missing
call " %~dp0binia64vcvarsia64.bat "
goto :eof
:x86_amd64
if not exist " %~dp0bin_amd64vcvarsx86_amd64.bat " goto missing
call " %~dp0bin_amd64vcvarsx86_amd64.bat "
goto :eof
:x86_ia64
if not exist " %~dp0bin_ia64vcvarsx86_ia64.bat " goto missing
call " %~dp0bin_ia64vcvarsx86_ia64.bat "
goto :eof
:usage
echo Error in script usage. The correct usage is:
echo % 0 [ option ]
echo where [ option ] is: x86 ^| ia64 ^| amd64 ^| x86_amd64 ^| x86_ia64
echo:
echo For example:
echo % 0 x86_ia64
goto :eof
:missing
echo The specified configuration type is missing. The tools for the
echo configuration might not be installed.
goto :eof
不难发现,实际运行的代码是
:x86
if not exist "%~dp0bin/vcvars32.bat" goto missing
call "%~dp0bin/vcvars32.bat"
goto :eof
即运行当前目录下的bin目录下的vcvars32.bat文件,这个文件的内容非常简单,如下:
"%VS80COMNTOOLS%vsvars32.bat"
其中VS80COMNTOOLS为系统变量,在我的机器上取值d:/Microsoft Visual Studio 8/Common7/Tools/
故找到该目录,并查看其下的vsvars32.bat,代码如下
@SET VCINSTALLDIR = d:Microsoft Visual Studio 8 VC
@SET FrameworkDir = C:WINDOWSMicrosoft.NETFramework
@SET FrameworkVersion = v2 .0.50727
@SET FrameworkSDKDir = d:Microsoft Visual Studio 8 SDKv2 .0
@if " %VSINSTALLDIR% " == "" goto error_no_VSINSTALLDIR
@if " %VCINSTALLDIR% " == "" goto error_no_VCINSTALLDIR
@echo Setting environment for using Microsoft Visual Studio 2005 x86 tools.
@rem
@rem Root of Visual Studio IDE installed files.
@rem
@set DevEnvDir = d:Microsoft Visual Studio 8 Common7IDE
@set PATH = d:Microsoft Visual Studio 8 Common7IDE ; d:Microsoft Visual Studio 8VCBIN;d:Microsoft Visual Studio 8Common7Tools;d:Microsoft Visual Studio 8Common7Toolsin;d:Microsoft Visual Studio 8VCPlatformSDKin;d:Microsoft Visual Studio 8SDKv2.0in;C:WINDOWSMicrosoft.NETFrameworkv2.0.50727;d:Microsoft Visual Studio 8VCVCPackages;%PATH%
@set INCLUDE = d:Microsoft Visual Studio 8 VCATLMFCINCLUDE ; d:Microsoft Visual Studio 8VCINCLUDE;d:Microsoft Visual Studio 8VCPlatformSDKinclude;d:Microsoft Visual Studio 8SDKv2.0include;%INCLUDE%
@set LIB = d:Microsoft Visual Studio 8 VCATLMFCLIB ; d:Microsoft Visual Studio 8VCLIB;d:Microsoft Visual Studio 8VCPlatformSDKlib;d:Microsoft Visual Studio 8SDKv2.0lib;%LIB%
@set LIBPATH = C:WINDOWSMicrosoft.NETFrameworkv2 .0.50727 ; d:Microsoft Visual Studio 8VCATLMFCLIB
@goto end
:error_no_VSINSTALLDIR
@echo ERROR: VSINSTALLDIR variable is not set.
@goto end
:error_no_VCINSTALLDIR
@echo ERROR: VCINSTALLDIR variable is not set.
@goto end
:end
不能发现此文件的作用即为完成与VS2005相关的一些系统变量的设置,如:
VSINSTALLDIR——VS2005的安装路径;
VCINSTALLDIR——VC的安装路径;
FrameworkDir——Framework2.0的安装路径;
等等。
这样,如果我们需要自己写一个批处理文件,完成一些VS2005相关的设置,调用VS2005提供的一些工具(如gacutil,该工具在 D:/Microsoft Visual Studio 8/SDK/v2.0/Bin目录下,该目录没有被Path系统变量引用,故该命令无法直接在cmd中运行),就可以在批处理文件的开头先调用 vsvars32.bat,然后在调用VS2005的相关工具。代码举例如下:
call "%VS80COMNTOOLS%vsvars32.bat"
gacutil /i YejunWorkshop.IDUControlLibrary.Design.dll
该批处理文件直接运行即可将YejunWorkshop.IDUControlLibrary.Design.dll程序集添加到GAC中。