Trouble with setting environment variables. WIN32 using system();

Discussion in 'C++' started by Henaro, May 7, 2007.

  1. Henaro

    Henaro Guest

    Hello~

    I am having trouble setting environment variables in C++ on win32.

    The code that is not working is:


    char prxy[500];
    char pf_cmd1[500] = "set http_proxy=";
    ....
    cout <<"What proxy will you be using? Please input it as
    IPADDRESS:pORT\n";
    cin.getline(prxy,499);
    strcat(pf_cmd1, prxy);
    system(pf_cmd1);

    It compiles correctly; using Dev-C++.

    But it just doesn't work.
    :-(
    Henaro, May 7, 2007
    #1
    1. Advertising

  2. * Henaro:
    >
    > I am having trouble setting environment variables in C++ on win32.
    >
    > The code that is not working is:
    >
    >
    > char prxy[500];
    > char pf_cmd1[500] = "set http_proxy=";
    > ...
    > cout <<"What proxy will you be using? Please input it as
    > IPADDRESS:pORT\n";
    > cin.getline(prxy,499);
    > strcat(pf_cmd1, prxy);


    Possible buffer overflow.


    > system(pf_cmd1);


    This is system-specific, but just for the record, as you've noted, it
    won't work. Use the relevant system-specific calls. To get help with
    that, post to a relevant group (see the FAQ's list of group).


    > It compiles correctly; using Dev-C++.
    >
    > But it just doesn't work.
    > :-(



    In C++, instead of C, it would look like this:

    std::string proxy;
    std::cout << "What proxy will you be using? Please input "
    "it as IPADDRESS:pORT ";
    std::getline( cin, proxy );
    // Here you should check whether input succeeded or failed, then
    someSystemCall( "http_proxy", proxy.c_str() );

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, May 7, 2007
    #2
    1. Advertising

  3. Re: Trouble with setting environment variables. WIN32 using system();

    On May 6, 8:08 pm, Henaro <> wrote:
    > Hello~
    >
    > I am having trouble setting environment variables in C++ on win32.
    >
    > The code that is not working is:
    >
    > char prxy[500];
    > char pf_cmd1[500] = "set http_proxy=";
    > ...
    > cout <<"What proxy will you be using? Please input it as
    > IPADDRESS:pORT\n";
    > cin.getline(prxy,499);
    > strcat(pf_cmd1, prxy);
    > system(pf_cmd1);
    >
    > It compiles correctly; using Dev-C++.
    >
    > But it just doesn't work.
    > :-(



    It probably does work, but not in the way that you have in mind.

    A "system" call probably *WILL* run a command interpreter and probably
    *WILL* issue the command that sets the environment variable, but,
    unless you issue subsequent "system" calls to the same command
    interpreter, it likely won't have the intended effect. It is
    implementation dependant and would be approximately equally common to
    have subsequent "system" calls start a separate command interpreter or
    to issue them to the same command interpreter.

    "putenv" is the more common library function that does what you likely
    intend. It is also common to use some form of exec that allows you to
    specify a set of enviroment variables.
    Michael Angelo Ravera, May 7, 2007
    #3
  4. Henaro

    Guest

    Re: Trouble with setting environment variables. WIN32 using system();

    On May 6, 11:08 pm, Henaro <> wrote:
    > Hello~
    >
    > I am having trouble setting environment variables in C++ on win32.


    This approach is probably not going to work on any system.other than
    one (such as VMS) where the shell is part of the process address apce.

    What you are essentially doing is

    Process 1: From your current shell running your program, creating
    Process 2:
    Process 2: From within your program you create another shell [Process
    3], that executes the SETENV command, changing the environment in
    [Process 3].

    Process 3 terminates.
    Process 2 terminates

    Process 1's environment is unchanged. (You changed the environment in
    Process 3.)

    I'm going to suggest that you learn how to use C++ strings rather than
    C-strings.

    >
    > The code that is not working is:
    >
    > char prxy[500];
    > char pf_cmd1[500] = "set http_proxy=";
    > ...
    > cout <<"What proxy will you be using? Please input it as
    > IPADDRESS:pORT\n";
    > cin.getline(prxy,499);
    > strcat(pf_cmd1, prxy);
    > system(pf_cmd1);
    >
    > It compiles correctly; using Dev-C++.
    >
    > But it just doesn't work.
    > :-(
    , May 7, 2007
    #4
  5. Henaro

    Henaro Guest

    Re: Trouble with setting environment variables. WIN32 using system();

    On May 7, 9:59 am, wrote:
    > On May 6, 11:08 pm, Henaro <> wrote:
    >
    > > Hello~

    >
    > > I am having trouble setting environment variables in C++ on win32.

    >
    > This approach is probably not going to work on any system.other than
    > one (such as VMS) where the shell is part of the process address apce.
    >
    > What you are essentially doing is
    >
    > Process 1: From your current shell running your program, creating
    > Process 2:
    > Process 2: From within your program you create another shell [Process
    > 3], that executes the SETENV command, changing the environment in
    > [Process 3].
    >
    > Process 3 terminates.
    > Process 2 terminates
    >
    > Process 1's environment is unchanged. (You changed the environment in
    > Process 3.)
    >
    > I'm going to suggest that you learn how to use C++ strings rather than
    > C-strings.
    >
    >
    >
    > > The code that is not working is:

    >
    > > char prxy[500];
    > > char pf_cmd1[500] = "set http_proxy=";
    > > ...
    > > cout <<"What proxy will you be using? Please input it as
    > > IPADDRESS:pORT\n";
    > > cin.getline(prxy,499);
    > > strcat(pf_cmd1, prxy);
    > > system(pf_cmd1);

    >
    > > It compiles correctly; using Dev-C++.

    >
    > > But it just doesn't work.
    > > :-(


    What do you mean by C++ strings? Unless you mean the string data
    type, I thought I was using C++ strings.

    The problem that I see with the string data type is that it won't work
    with either system() or putenv(), both of which require a char for
    their argument.

    As for setting the environment variable it seems to work with the
    putenv() function.
    Henaro, May 7, 2007
    #5
  6. Henaro

    red floyd Guest

    Re: Trouble with setting environment variables. WIN32 using system();

    Henaro wrote:
    > [redacted]
    >
    > What do you mean by C++ strings? Unless you mean the string data
    > type, I thought I was using C++ strings.

    No, you're using C strings. Yes, he means the std::string data type.

    >
    > The problem that I see with the string data type is that it won't work
    > with either system() or putenv(), both of which require a char for
    > their argument.


    That's what the the c_str() method of std::string is for. It gives you
    a const char* to hand to all those pesky OS functions.
    red floyd, May 7, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    634
  2. Afaque Ahmad
    Replies:
    2
    Views:
    662
    Brian Schröder
    May 26, 2005
  3. Martin DeMello
    Replies:
    2
    Views:
    128
    bbiker
    Oct 30, 2007
  4. Thierry
    Replies:
    2
    Views:
    142
    sisyphus
    Jun 28, 2008
  5. Aryeh M. Friedman
    Replies:
    1
    Views:
    184
    Aryeh M. Friedman
    Jul 5, 2013
Loading...

Share This Page