通过应用程序获取环境变量我们主要使用一个关键的全局变量,不同的版本下对应的环境变量是不一样的。
ASCII版的环境变量:_environ。
Unicode版的环境变量:_wenviron。
两个不同的环境变量。这两个环境都是指向字符串的指针。具体用法如下:
ASCII版的_environ环境变量的获取:
#include <windows.h>
#include <tchar.h>
int main()
{
//获取ASCII环境变量
char** p = _environ;
while ((*p)!= NULL )
{
printf("%s\n", *p);
p++;
}
return0;
}
Unicode版的_wenviron环境变量的获取:
#include "windows.h"
#include "tchar.h"
int _tmain() //或者写成int wmain(),都可以
{
//获取Unicode环境变量
TCHAR** p =_wenviron;
while ((*p)!= NULL)
{
_tprintf(L"%s\n", *p);
p++;
}
return0;
}
两者输出的结果一样,都是如下:
ALLUSERSPROFILE=C:\ProgramData
ANDROID_SDK_ROOT=D:\software\cocos2dx\adt-bundle-windows-x86_64-20131030\sdk
ANT_ROOT=D:\software\cocos2dx\apache-ant-1.9.4\bin
APPDATA=C:\Users\Lee\AppData\Roaming
Catalina_Home=D:\apache-tomcat-7.0.63-windows-x64\apache-tomcat-7.0.63
COCOS_CONSOLE_ROOT=D:\software\cocos2dx\cocos2d-js-v3.0-rc3\tools\cocos2d-console\bin
CommonProgramFiles=C:\Program Files (x86)\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=THINKER-PC
ComSpec=C:\WINDOWS\system32\cmd.exe
DXSDK_DIR=C:\Program Files (x86)\Microsoft DirectX SDK (February2010)\
FPS_BROWSER_APP_PROFILE_STRING=Internet Explorer
FPS_BROWSER_USER_PROFILE_STRING=Default
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=\Users\Lee
include=D:\Program Files (x86)\Microsoft VisualStudio\VC98\atl\include;D:\Program Files (x86)\Microsoft VisualStudio\VC98\mfc\include;D:\Program Files (x86)\Microsoft VisualStudio\VC98\include
Java_Home=D:\JDK7
lib=D:\Program Files (x86)\Microsoft VisualStudio\VC98\mfc\lib;D:\Program Files (x86)\Microsoft VisualStudio\VC98\lib
LOCALAPPDATA=C:\Users\Lee\AppData\Local
LOGONSERVER=\\THINKER-PC
MOZ_PLUGIN_PATH=D:\Program Files (x86)\Foxit Software\FoxitReader\plugins\
MSBuildLoadMicrosoftTarg etsReadOnly=true
MSDevDir=D:\Program Files (x86)\Microsoft VisualStudio\Common\MSDev98
NDK_ROOT=D:\software\cocos2dx\android-ndk-r10
NUMBER_OF_PROCESSORS=4
OneDrive=C:\Users\Lee\OneDrive
OnlineServices=Online Services
OPENCV=D:\opencv\build
OS=Windows_NT
PATH=C:\Perl\site\bin;C:\Perl\bin;D:\oracle\product\10.2.0\client_1\bin;D:\oracle\product\10.2.0\db_1\bin;C:\ProgramFiles (x86)\Common Files\NetSarang;c:\Program Files(x86)\Intel\iCLS Client\;c:\Program Files\Intel\iCLSClient\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\ProgramFiles\Intel\Intel(R) Management Engine Components\DAL;C:\ProgramFiles\Intel\Intel(R) Management Engine Components\IPT;C:\ProgramFiles (x86)\Intel\Intel(R) Management EngineComponents\DAL;C:\Program Files (x86)\Intel\Intel(R) ManagementEngine Components\IPT;C:\Program Files (x86)\WindowsLive\Shared;C:\Program Files (x86)\Microsoft SQLServer\100\Tools\Binn\;C:\Program Files\Microsoft SQLServer\100\Tools\Binn\;C:\Program Files\Microsoft SQLServer\100\DTS\Binn\;D:\Matlab2012a\runtime\win64;D:\Matlab2012a\bin;C:\ProgramFiles (x86)\Windows Kits\8.1\Windows PerformanceToolkit\;C:\Program Files\Microsoft SQLServer\110\Tools\Binn\;C:\Program Files (x86)\NVIDIACorporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\ProgramFiles (x86)\Windows Kits\10\Windows PerformanceToolkit\;D:\opencv\build\x86\vc12\bin;D:\Program Files (x86)\IDMComputerSolutions\UltraEdit;D:\software\cocos2dx\cocos2d-js-v3.0-rc3\tools\cocos2d-console\bin;D:\ProgramFiles (x86)\Microsoft Visual Studio\Common\Tools\WinNT;D:\ProgramFiles (x86)\Microsoft Visual Studio\Common\MSDev98\Bin;D:\ProgramFiles (x86)\Microsoft Visual Studio\Common\Tools;D:\Program Files(x86)\Microsoft VisualStudio\VC98\bin;D:\JDK7\bin;D:\apache-tomcat-7.0.63-windows-x64\apache-tomcat-7.0.63\bin;D:\mongodb\bin;D:\JDK7\jre\bin;D:\opencv\build\x86\vc12\bin;C:\ProgramFiles (x86)\Microsoft Visual Studio 12.0\;C:\Program Files(x86)\Microsoft Visual Studio 12.0\VC\bin
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYM
PERL5LIB=D:\oracle\product\10.2.0\db_1\perl\5.8.3\lib\MSWin32-x86;D:\oracle\product\10.2.0\db_1\perl\5.8.3\lib;D:\oracle\product\10.2.0\db_1\perl\5.8.3\lib\MSWin32-x86;D:\oracle\product\10.2.0\db_1\perl\site\5.8.3;D:\oracle\product\10.2.0\db_1\perl\site\5.8.3\lib;D:\oracle\product\10.2.0\db_1\sysman\admin\scripts;
PkgDefApplicationConfigF ile=C:\Users\Lee\AppData\Local\Microsoft\VisualStudio\12.0\devenv.exe.config
Platform=HPD
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_ARCHITEW6432=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 60 Stepping 3,GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=3c03
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files (x86)
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files
PROMPT=$P$G
PSModulePath=C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\
PUBLIC=C:\Users\Public
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\WINDOWS
TEMP=C:\Users\Lee\AppData\Local\Temp
TMP=C:\Users\Lee\AppData\Local\Temp
USERDOMAIN=Thinker-PC
USERDOMAIN_ROAMINGPROFILE=Thinker-PC
USERNAME=Lee
USERPROFILE=C:\Users\Lee
VisualStudioDir=C:\Users\Lee\Documents\Visual Studio 2013
VisualStudioEdition=Microsoft Visual Studio Ultimate 2013
VisualStudioVersion=12.0
VS120COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio12.0\Common7\Tools\
VSLANG=2052
windir=C:\WINDOWS
ASCII版的环境变量:_environ。
Unicode版的环境变量:_wenviron。
两个不同的环境变量。这两个环境都是指向字符串的指针。具体用法如下:
ASCII版的_environ环境变量的获取:
#include <windows.h>
#include <tchar.h>
int main()
{
}
Unicode版的_wenviron环境变量的获取:
#include "windows.h"
#include "tchar.h"
int _tmain()
{
}
ALLUSERSPROFILE=C:\ProgramData
ANDROID_SDK_ROOT=D:\software\cocos2dx\adt-bundle-windows-x86_64-20131030\sdk
ANT_ROOT=D:\software\cocos2dx\apache-ant-1.9.4\bin
APPDATA=C:\Users\Lee\AppData\Roaming
Catalina_Home=D:\apache-tomcat-7.0.63-windows-x64\apache-tomcat-7.0.63
COCOS_CONSOLE_ROOT=D:\software\cocos2dx\cocos2d-js-v3.0-rc3\tools\cocos2d-console\bin
CommonProgramFiles=C:\Program Files (x86)\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=THINKER-PC
ComSpec=C:\WINDOWS\system32\cmd.exe
DXSDK_DIR=C:\Program Files (x86)\Microsoft DirectX SDK (February2010)\
FPS_BROWSER_APP_PROFILE_STRING=Internet Explorer
FPS_BROWSER_USER_PROFILE_STRING=Default
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=\Users\Lee
include=D:\Program Files (x86)\Microsoft VisualStudio\VC98\atl\include;D:\Program Files (x86)\Microsoft VisualStudio\VC98\mfc\include;D:\Program Files (x86)\Microsoft VisualStudio\VC98\include
Java_Home=D:\JDK7
lib=D:\Program Files (x86)\Microsoft VisualStudio\VC98\mfc\lib;D:\Program Files (x86)\Microsoft VisualStudio\VC98\lib
LOCALAPPDATA=C:\Users\Lee\AppData\Local
LOGONSERVER=\\THINKER-PC
MOZ_PLUGIN_PATH=D:\Program Files (x86)\Foxit Software\FoxitReader\plugins\
MSBuildLoadMicrosoftTarg
MSDevDir=D:\Program Files (x86)\Microsoft VisualStudio\Common\MSDev98
NDK_ROOT=D:\software\cocos2dx\android-ndk-r10
NUMBER_OF_PROCESSORS=4
OneDrive=C:\Users\Lee\OneDrive
OnlineServices=Online Services
OPENCV=D:\opencv\build
OS=Windows_NT
PATH=C:\Perl\site\bin;C:\Perl\bin;D:\oracle\product\10.2.0\client_1\bin;D:\oracle\product\10.2.0\db_1\bin;C:\ProgramFiles (x86)\Common Files\NetSarang;c:\Program Files(x86)\Intel\iCLS Client\;c:\Program Files\Intel\iCLSClient\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\ProgramFiles\Intel\Intel(R) Management Engine Components\DAL;C:\ProgramFiles\Intel\Intel(R) Management Engine Components\IPT;C:\ProgramFiles (x86)\Intel\Intel(R) Management EngineComponents\DAL;C:\Program Files (x86)\Intel\Intel(R) ManagementEngine Components\IPT;C:\Program Files (x86)\WindowsLive\Shared;C:\Program Files (x86)\Microsoft SQLServer\100\Tools\Binn\;C:\Program Files\Microsoft SQLServer\100\Tools\Binn\;C:\Program Files\Microsoft SQLServer\100\DTS\Binn\;D:\Matlab2012a\runtime\win64;D:\Matlab2012a\bin;C:\ProgramFiles (x86)\Windows Kits\8.1\Windows PerformanceToolkit\;C:\Program Files\Microsoft SQLServer\110\Tools\Binn\;C:\Program Files (x86)\NVIDIACorporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\ProgramFiles (x86)\Windows Kits\10\Windows PerformanceToolkit\;D:\opencv\build\x86\vc12\bin;D:\Program Files (x86)\IDMComputerSolutions\UltraEdit;D:\software\cocos2dx\cocos2d-js-v3.0-rc3\tools\cocos2d-console\bin;D:\ProgramFiles (x86)\Microsoft Visual Studio\Common\Tools\WinNT;D:\ProgramFiles (x86)\Microsoft Visual Studio\Common\MSDev98\Bin;D:\ProgramFiles (x86)\Microsoft Visual Studio\Common\Tools;D:\Program Files(x86)\Microsoft VisualStudio\VC98\bin;D:\JDK7\bin;D:\apache-tomcat-7.0.63-windows-x64\apache-tomcat-7.0.63\bin;D:\mongodb\bin;D:\JDK7\jre\bin;D:\opencv\build\x86\vc12\bin;C:\ProgramFiles (x86)\Microsoft Visual Studio 12.0\;C:\Program Files(x86)\Microsoft Visual Studio 12.0\VC\bin
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYM
PERL5LIB=D:\oracle\product\10.2.0\db_1\perl\5.8.3\lib\MSWin32-x86;D:\oracle\product\10.2.0\db_1\perl\5.8.3\lib;D:\oracle\product\10.2.0\db_1\perl\5.8.3\lib\MSWin32-x86;D:\oracle\product\10.2.0\db_1\perl\site\5.8.3;D:\oracle\product\10.2.0\db_1\perl\site\5.8.3\lib;D:\oracle\product\10.2.0\db_1\sysman\admin\scripts;
PkgDefApplicationConfigF
Platform=HPD
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_ARCHITEW6432=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 60 Stepping 3,GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=3c03
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files (x86)
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files
PROMPT=$P$G
PSModulePath=C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\
PUBLIC=C:\Users\Public
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\WINDOWS
TEMP=C:\Users\Lee\AppData\Local\Temp
TMP=C:\Users\Lee\AppData\Local\Temp
USERDOMAIN=Thinker-PC
USERDOMAIN_ROAMINGPROFILE=Thinker-PC
USERNAME=Lee
USERPROFILE=C:\Users\Lee
VisualStudioDir=C:\Users\Lee\Documents\Visual Studio 2013
VisualStudioEdition=Microsoft Visual Studio Ultimate 2013
VisualStudioVersion=12.0
VS120COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio12.0\Common7\Tools\
VSLANG=2052
windir=C:\WINDOWS