By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,353 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,353 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
:-(

May 7 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
* 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?
May 7 '07 #2

P: n/a
On May 6, 8:08 pm, Henaro <assb...@gmail.comwrote:
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.

May 7 '07 #3

P: n/a
On May 6, 11:08 pm, Henaro <assb...@gmail.comwrote:
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 '07 #4

P: n/a
On May 7, 9:59 am, faceman28...@yahoo.com wrote:
On May 6, 11:08 pm, Henaro <assb...@gmail.comwrote:
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.

May 7 '07 #5

P: n/a
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.
May 7 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.