简体   繁体   English

如何使用C在Windows中单独获取用户和系统环境变量

[英]How to separately get user and system environment variables in Windows with C

I would like to be able to separately query user and system environment variables. 我希望能够单独查询用户和系统环境变量。
Like they are shown in the Environment Variables dialog. 就像它们显示在Environment Variables对话框中一样。

By using the GetEnvironmentVariable and *getenv* functions I can only get values obtained after merging the two sets of variables, with the user-defined ones taking precedence. 通过使用GetEnvironmentVariable*getenv*函数,我只能获得合并两组变量后获得的值,其中用户定义的变量优先。
Just like the set command shows on the command line. 就像在命令行上显示set命令一样。

This capability would be useful to avoid silently falling back on a system-defined variable when a user-defined one with the same name doesn't exist. 当用户定义的具有相同名称的变量不存在时,此功能可用于避免以静默方式回退到系统定义的变量。

Even an undocumented API would be fine. 即使是未记录的API也没关系。

ExpandEnvironmentStringsForUser ( CreateEnvironmentBlock ) will only use the system variables if you pass a NULL token handle but I don't think there is a function that only gives you the user variables so you have to manually read them from the registry. 如果传递NULL标记句柄, ExpandEnvironmentStringsForUserCreateEnvironmentBlock )将仅使用系统变量,但我认为没有一个函数只提供用户变量,因此您必须从注册表中手动读取它们。

Remember that there are two user variable keys in the registry: Environment and Volatile Environment . 请记住,注册表中有两个用户变量键: Environment Volatile Environment The volatile key even has sub keys on some versions of Windows. 易失性键甚至在某些版本的Windows上具有子键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM