0

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.

  1. Can I make a cross platform code using C++ with Libraries that can create and modify and delete the path variables ? If not
  2. 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;
}
CocoCrisp
  • 807
  • 1
  • 9
  • 26

1 Answers1

1

This has nothing to with Boost, and actually very little with environment variables.

It has to do with System configuration instead.

So, given you have the required permissions to alter the user/system environment you can use these methods to make the changes stick:

Most of the samples around favour tools like Windows Scripting Host, PowerShell and .NET to do the job:

But you can probably translate it to C++ if you must.

PS. On Linux, you'd alter some files like /etc/profile, /etc/default/... etc.

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • I have gone through the link on Microsoft Page, any piece of code is appreciated. @sehe – CocoCrisp Oct 07 '16 at 11:13
  • 1
    There are many pieces of code on the linked question. Search and you will find. At least now you know /what/ it is you wanted to do – sehe Oct 07 '16 at 11:26