473,774 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(prx y,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 3477
* 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(prx y,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(prx y,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(prx y,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...@ya hoo.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(prx y,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
4964
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
2
8786
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 PASSWD variable so that when the script exits, my env variables now include one called PASSWD that contains the text I typed at the "Password:" prompt. - what I need help with
4
4215
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
2385
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 - perhaps I'm just not very smart or perhaps MS is making this too hard for us sql bunnies to understand - I dunno, but I'd really appreciate someone explaining what I'm doing wrong here & perhaps suggest a better approach.. I'm familiar with use of...
1
6509
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" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
8
10096
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? -- Jack Vamvas ___________________________________ The latest IT jobs - www.ITjobfeed.com <a href="http://www.itjobfeed.com">UK IT Jobs</a>
1
2318
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 supplied by the build system vendor. My first idea was to write a .bat scrpt that first calls the buildEnv.bat script and then calls the perl script. This does not seem to work since the perl script never executes. The .bat file looks like this: ...
4
2339
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 (system variables) and set "path" pointing to C:\Python25 and thats all I do. I type python from "cmd" window and its converting to python window for python execution. All fine up to this point. Now, I want to drag and drop python (.py) files to...
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.