环境变量ProgramFiles
、ProgramW6432Dir
、ProgramFilesDir (x86)
、CommonProgramFiles
和ProgramData
从哪里获取数据?
为什么不可能使用_putenv()
更改这些环境变量的值?例如:
_putenv( "ProgramFiles=D:\\MyProgs" );
发布于 2020-12-30 02:32:53
在64位Windows系统上,读取各种环境变量和一些Windows是重定向到不同的源,这取决于读取的进程是64位还是32位。
下表列出了这些数据来源:
X = HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion
Y = HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion
Z = HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
READING ENVIRONMENT VARIABLES: Source for 64-bit process Source for 32-bit process
-------------------------------|----------------------------------------|--------------------------------------------------------------
%ProgramFiles% : X\ProgramW6432Dir X\ProgramFilesDir (x86)
%ProgramFiles(x86)% : X\ProgramFilesDir (x86) X\ProgramFilesDir (x86)
%ProgramW6432% : X\ProgramW6432Dir X\ProgramW6432Dir
%CommonProgramFiles% : X\CommonW6432Dir X\CommonFilesDir (x86)
%CommonProgramFiles(x86)% : X\CommonFilesDir (x86) X\CommonFilesDir (x86)
%CommonProgramW6432% : X\CommonW6432Dir X\CommonW6432Dir
%ProgramData% : Z\ProgramData Z\ProgramData
READING REGISTRY VALUES: Source for 64-bit process Source for 32-bit process
-------------------------------|----------------------------------------|--------------------------------------------------------------
X\ProgramFilesDir : X\ProgramFilesDir Y\ProgramFilesDir
X\ProgramFilesDir (x86) : X\ProgramFilesDir (x86) Y\ProgramFilesDir (x86)
X\ProgramFilesPath : X\ProgramFilesPath = %ProgramFiles% Y\ProgramFilesPath = %ProgramFiles(x86)%
X\ProgramW6432Dir : X\ProgramW6432Dir Y\ProgramW6432Dir
X\CommonFilesDir : X\CommonFilesDir Y\CommonFilesDir
X\CommonFilesDir (x86) : X\CommonFilesDir (x86) Y\CommonFilesDir (x86)
X\CommonW6432Dir : X\CommonW6432Dir Y\CommonW6432Dir
例如,对于32位进程,%ProgramFiles%
和%ProgramFiles(x86)%
环境变量的数据源是Registry HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir (x86)
。
但是,对于64位进程,%ProgramFiles%
环境变量的数据源是注册表值HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramW6432Dir
...and,%ProgramFiles(x86)%
环境变量的数据源是注册表值HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir (x86)
。
大多数默认的Windows安装都会将类似于C:\Program Files (x86)
的字符串放入注册表值HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir (x86)
中,但这一点可以更改(其他也可以)。
下面是这些环境变量和注册表值中的典型数据:
READING ENVIRONMENT VARIABLES: Typical data for 64-bit process Typical data for 32-bit process
-------------------------------|----------------------------------------|-----------------------------------------------
%ProgramFiles% = C:\Program Files C:\Program Files (x86)
%ProgramFiles(x86)% = C:\Program Files (x86) C:\Program Files (x86)
%ProgramW6432% = C:\Program Files C:\Program Files
%CommonProgramFiles% = C:\Program Files\Common Files C:\Program Files (x86)\Common Files
%CommonProgramFiles(x86)% = C:\Program Files (x86)\Common Files C:\Program Files (x86)\Common Files
%CommonProgramW6432% = C:\Program Files\Common Files C:\Program Files\Common Files
%ProgramData% = C:\ProgramData C:\ProgramData
READING REGISTRY VALUES: Typical data for 64-bit process Typical data for 32-bit process
------------------------------|----------------------------------------|-----------------------------------------------
X\ProgramFilesDir = C:\Program Files C:\Program Files (x86)
X\ProgramFilesDir (x86) = C:\Program Files (x86) C:\Program Files (x86)
X\ProgramFilesPath = %ProgramFiles% => C:\Program Files %ProgramFiles(x86)% => C:\Program Files (x86)
X\ProgramW6432Dir = C:\Program Files C:\Program Files
X\CommonFilesDir = C:\Program Files\Common Files C:\Program Files (x86)\Common Files
X\CommonFilesDir (x86) = C:\Program Files (x86)\Common Files C:\Program Files (x86)\Common Files
X\CommonW6432Dir = C:\Program Files\Common Files C:\Program Files\Common Files
X = HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion
最终,Windows Explorer将在登录时将输入到这些Windows注册表值中的任何内容读入相应的环境变量,然后复制到随后生成的任何子进程中。子进程可以使用_putenv()
更改自己的环境变量。
注册表值HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesPath
特别值得注意,因为大多数%ProgramFiles%
安装都将字符串%ProgramFiles%
放入其中,由64位进程读取。该字符串引用环境变量%ProgramFiles%
,后者从Registry HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramW6432Dir
...unless获取其数据--一些程序事先更改了该环境变量的值。
我编写了一个小实用程序,它显示64位和32位进程的这些环境变量。你可以下载它这里。
VisualStudio 2017的源代码包括在内,编译的64位和32位二进制可执行文件分别位于..\x64\Release
和..\x86\Release
目录中。
https://stackoverflow.com/questions/65501289
复制相似问题