c++11 - Execute a Windows command with a controled environment in C++ -
i trying execute command in c++11 under windows, , want environment char** manually set.
i saw popen(), system() , createprocess() functions, cannot achieve theses functions. looking alternative unix exec* functions, allows precise environment.
you want lpenvironment
parameter of createprocess
:
lpenvironment [in, optional]
a pointer environment block new process. if parameter null, new process uses environment of calling process.
an environment block consists of null-terminated block of null-terminated strings. each string in following form:
name=value\0
example:
// example storing environment variables dynamically std::map<std::string, std::string> env = { {"name1", "value1"}, {"name2", "value2"} }; // example generating block of strings std::vector<char> envblock; std::for_each(env.begin(), env.end(), [&envblock](const std::pair<std::string, std::string> & p) { std::copy(p.first.begin(), p.first.end(), std::back_inserter(envblock)); envblock.push_back('='); std::copy(p.second.begin(), p.second.end(), std::back_inserter(envblock)); envblock.push_back('\0'); } ); envblock.push_back('\0'); // feed ::createprocess() lpvoid lpenvironment = (lpvoid)envblock.data();
Comments
Post a Comment