Is it possible to set the environment variables of a process "A" in a way that they will be valid only for its current run (process "A" will be started by my process) using C++?
Asked
Active
Viewed 1,680 times
2 Answers
3
Assuming you are prepared to rely on the Windows API, when you call the CreateProcess
function to launch a process, you have the lpEnvironment
parameter.
Normally you pass NULL
which means, use the environment of the creating process. However, you can supply an environment block which will be used by the new process.
The environment block that you pass is a null-terminated block of null-terminated strings. For example:
"MyVar=MyValue\0MyOtheVar=MyOtherValue\0\0"
defines two separate variables.

David Heffernan
- 601,492
- 42
- 1,072
- 1,490
0
If changing the environment variables does not cause problems for your current process, please look at Setting Environment Variables in C++.
Tip: any programs forked by your process will only have the environment variables and values you provide for them.

Community
- 1
- 1

Mark Leighton Fisher
- 5,609
- 2
- 18
- 29