Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Trouble with setting environment variables. WIN32 using system();

Reply
Thread Tools

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

 
 
Henaro
Guest
Posts: n/a
 
      05-07-2007
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
IPADDRESSORT\n";
cin.getline(prxy,499);
strcat(pf_cmd1, prxy);
system(pf_cmd1);

It compiles correctly; using Dev-C++.

But it just doesn't work.


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-07-2007
* 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
> IPADDRESSORT\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 IPADDRESSORT ";
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?
 
Reply With Quote
 
 
 
 
Michael Angelo Ravera
Guest
Posts: n/a
 
      05-07-2007
On May 6, 8:08 pm, Henaro <(E-Mail Removed)> 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
> IPADDRESSORT\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.



 
Reply With Quote
 
faceman28208@yahoo.com
Guest
Posts: n/a
 
      05-07-2007
On May 6, 11:08 pm, Henaro <(E-Mail Removed)> 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
> IPADDRESSORT\n";
> cin.getline(prxy,499);
> strcat(pf_cmd1, prxy);
> system(pf_cmd1);
>
> It compiles correctly; using Dev-C++.
>
> But it just doesn't work.
>



 
Reply With Quote
 
Henaro
Guest
Posts: n/a
 
      05-07-2007
On May 7, 9:59 am, (E-Mail Removed) wrote:
> On May 6, 11:08 pm, Henaro <(E-Mail Removed)> 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
> > IPADDRESSORT\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.

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      05-07-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
70-284 Lab Environment, Need Virtual Environment brooklynbridge508@hotmail.com MCSA 4 05-02-2007 09:49 AM
Setting an environment variable from another environment variable marcwentink@hotmail.com Java 5 04-04-2007 10:39 PM
win32 process information, using win32 extension Java and Swing Python 1 10-24-2005 09:13 PM
RE: win32 process information, using win32 extension Tim Golden Python 0 10-21-2005 02:18 PM
setting environment variables using windows batch file Afaque Ahmad Ruby 2 05-26-2005 12:36 PM



Advertisments