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

H

Henaro

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.
:-(
 
A

Alf P. Steinbach

* 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() );
 
M

Michael Angelo Ravera

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.
 
F

faceman28208

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.
:-(
 
H

Henaro

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.
 
R

red floyd

Henaro said:
[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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top