473,320 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
5 3441
* 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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
2
by: Rick Kasten | last post by:
Here's what I need to do: 1) Write "Password:" to the console - I know how to do that 2) Accept input that is not written back to the console - I know how to do that (Term::ReadKey) 3) Set the...
4
by: Bill Davidson | last post by:
All: I've found the 'Environment.GetEnvironmentVariable()' method; but how do I create and/or set an environment variable? Thanks, Bill
9
by: Greg Linwood | last post by:
I'm having difficulty understanding Session state in ASP.Net. It's almost embarrassing asking this as I've been using ASP since it was first released & it really shouldn't be this hard to use -...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
8
by: Jack Vamvas | last post by:
I know that you can do : Request.ServerVariables("QUERY_STRING") but can you actually set a serverVariable? I know that sounds counter intuitive , but is it possible, via vbscipt/asp? -- ...
1
by: Tension | last post by:
Hi. I have a Perl script that starts a build process (plus a lot of other things). The build process needs a few environment variables set. This is done automatically in a buildEnv.bat script...
4
by: Krishna | last post by:
Environment variable set up is the most confusing part for me all the time. Please help me with the following questions: When I install python in a new system, I will go to environment variables...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.