c++ - 如何从 C++ 更改 Windows shell (cmd.exe) 环境变量?

标签 c++ windows cmd environment-variables

我想编写一个程序,在调用它的 shell (cmd.exe) 实例中设置一个环境变量。我的想法是,我可以在此变量中存储一些状态,然后在后续调用中再次使用它。

我知道有像 SetEnvironmentVariable 这样的命令,但我的理解是那些只会更改当前进程的变量,不会修改调用 shell 的变量。

具体来说,我希望能够创建一个可以在两个目录之间跳转的命令。 Pushd/Popd 可以转到一个目录并返回,但没有办法第二次返回到最初推送的目录。

最佳答案

MSDN states the following :

Calling SetEnvironmentVariable has no effect on the system environment variables. To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam 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/

相关文章:

c# - COleDispatchDriver 错误 : automation return value coercion failed, DISP_E_TYPEMISMATCH ($80020005)

windows - 限制批处理脚本中生成的进程数

java - 有人帮我在java中执行命令

c++ - 与模板特化成为 friend 时可能出现 gcc 错误

c++ - VS2010 RC - 调试器中只有 100 个 std::map 元素

c++ - 打开哈希,添加元素,使用迭代器查找元素

windows - 为什么我的 Qt 4.5 应用程序在 Windows 下打开一个控制台窗口?

C 程序无法获取软盘驱动器的句柄

windows - Visual Studio 2012 中的 SQL Server CE 问题

python - 无法从 CMD 启动 TensorBoard