开发者

Remove environmental variable programmatically

I need to write a unit test for some C++ code that checks for the presence of an environmental variable. I'm using MSVS 2008 and gtest as my framework. I add the environmental 开发者_如何学Cvariable using putenv, I check the environmental variable using getevn, but I can't figure out how to remove it so that no other test will see it. I realize this is probably easy, but I can't find the answer on the internet. Thanks


Calling putenv again specifying "SOME_VAR=" as parameter will delete environment variable SOME_VAR. btw, Microsoft recommends using _putenv as putenv is deprecated.


You could always fork/exec a subprocess to do just the putenv/getenv testing, and then when it terminates there isn't any stray environment left around.


How about setting the env var to an empty string?

From cmd.exe, this works:

set SOMEVAR=something
echo %SOMEVAR%
set SOMEVAR=
echo %SOMEVAR%

Where the last one shows it has been deleted.


you can use the unsetenv function.

If vc2008 lacks this function, you can directly access the environment using getenv_s, and remove the entry manually, simulating unsetenv.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜