I am creating a C++ Application using Boost Library, so for I have developed modules but stuck here.
I want to Create and modify System Variables(Or Local Environment Variables that persist even after process has terminated) using boost library. There are methods like
setenv() putenv() getenv()
but they make local changes and the value gets disappeared after the process has terminated.
Well that's not the real issue. The real problem arises when i want the same code to run on linux and mac platform.
- Can I make a cross platform code using C++ with Libraries that can create and modify and delete the path variables ? If not
- Can I make a code for specific platform say Windows with the above requirements.
As of now I have this code that sets Environment Variable for windows but it give error
#include <string>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <process/boost/process.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/system/error_code.hpp>
namespace bp = boost::process;
namespace bpi = boost::process::initializers;
namespace bio = boost::iostreams;
int main()
{
char* str="SETX Name Value";
bp::pipe p = bp::create_pipe();
{
bio::file_descriptor_sink sink(p.sink, bio::close_handle);
boost::filesystem::path p("C:/Windows/System32/cmd.exe");
boost::system::error_code ec;
bp::windows::execute(bpi::run_exe(p),
bpi::set_cmd_line(str),
bpi::bind_stdout(sink),
bpi::set_on_error(ec)
);
}
bio::file_descriptor_source source(p.source, bio::close_handle);
bio::stream<bio::file_descriptor_source> is(source);
std::string s;
is >> s;
std::cout << s << std::endl;
std::cin.get();
return 0;
}