Setting environment variables for a specific run of a specific process

心不动则不痛 提交于 2020-01-06 19:36:52

问题


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++?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/13092481/setting-environment-variables-for-a-specific-run-of-a-specific-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!