我想编写一个程序,在调用它的 shell (cmd.exe) 实例中设置一个环境变量。我的想法是,我可以在此变量中存储一些状态,然后在后续调用中再次使用它。
我知道有像 SetEnvironmentVariable 这样的命令,但我的理解是那些只会更改当前进程的变量,不会修改调用 shell 的变量。
具体来说,我希望能够创建一个可以在两个目录之间跳转的命令。 Pushd/Popd 可以转到一个目录并返回,但没有办法第二次返回到最初推送的目录。
最佳答案
Calling
SetEnvironmentVariable
has no effect on the system environment variables. To programmatically add or modify system environment variables, add them to theHKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
registry key, then broadcast aWM_SETTINGCHANGE
message withlParam
set to the string "Environment". This allows applications, such as the shell, to pick up your updates. Note that the values of the environment variables listed in this key are limited to 1024 characters.
考虑到有两个级别的环境 - 系统和进程 - 更改 shell 中的那些将构成更改另一个进程的环境。我不相信这是可能的。
关于c++ - 如何从 C++ 更改 Windows shell (cmd.exe) 环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/774047/