Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set environment variable in C

Status
Not open for further replies.

artskib

Programmer
Jul 17, 2002
8
US
I have a C program (using MS Visual C++ development toolkit).
The program runs in DOS window on Win XP platform.
I wnat the C program to set and environment variable, so it is available to other DOS batch programs, which run in the same window (called by the C program).

When I use:

system ("echo Hello There");

the command workst fine.

When I tried:

system ("set MYENV=test");

it did not work. Any idea why?

Conversally, how do I retrieve already set environment variable and display it?
 
It probably did work - but only for the duration of the system-call.
system() will load the command interpreter, do the command and set the new env. var.
However the command interpreter will die after this - and so will the env. var.

SetEnvironmentVariable() (Platform SDK) is a better choice.
GetEnvironmentVariable() will retrieve env. vars.

/JOlesen

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top