如何从C ++更改Windows shell(cmd.exe)环境变量? [英] How can I change Windows shell (cmd.exe) environment variables from C++?

查看:171
本文介绍了如何从C ++更改Windows shell(cmd.exe)环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to write a program that sets an environment variable in an instance of the shell (cmd.exe) it was called from. The idea is that I could store some state in this variable and then use it again on a subsequent call.

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

I know there are commands like SetEnvironmentVariable, but my understanding is that those only change the variable for the current process and won't modify the calling shell's variables.

具体来说,我想要做的是创建一个可以在两个目录之间反弹的命令。 Pushd / Popd可以转到目录并返回,但没有办法第二次返回到最初推送的目录。

Specifically what I would like to be able to do is create a command that can bounce between two directories. Pushd/Popd can go to a directory and back, but don't have a way of returning a 2nd time to the originally pushed directory.

推荐答案

一个常见的技术是编写一个env文件,然后从脚本中调用。

A common techniques is the write an env file, that is then "call"ed from the script.

del env.var
foo.exe ## writes to env.var
call env.var

这篇关于如何从C ++更改Windows shell(cmd.exe)环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆