Enviroment variable on Linux _and_ Windows

A

Andrej Litowka

Hi All!

If anybody know, how I cann set an enviroment variable (with ANSI C) on
Windows similar like on Linux. I tried setenv, but the function doesn't work
correct on Windows - it sets env. variables only for calling process or
session (in my case, the program will called in cmd.exe), but I need its
global. There is no putenv on Windows, only _putenv and there is no _putenv
#if WIN32
#define putenv _putenv
#endif
and then compile with -DWIN32, but I would use a funktion, that implements
(and works correct - in my case, sets env. variables global for whole OS) as
well on Linux as on Windows.

Any help will be approtiate.
Thanks.
Andrey Litovka.
 
J

Jens.Toerring

Andrej Litowka said:
If anybody know, how I cann set an enviroment variable (with ANSI C) on
Windows similar like on Linux. I tried setenv, but the function doesn't work
correct on Windows - it sets env. variables only for calling process or
session (in my case, the program will called in cmd.exe), but I need its
global. There is no putenv on Windows, only _putenv and there is no _putenv

and then compile with -DWIN32, but I would use a funktion, that implements
(and works correct - in my case, sets env. variables global for whole OS) as
well on Linux as on Windows.

Sorry, neither setenv() nor putenv() nor environment variables or
the possible differences between Linux and Windows are on-topic
here because they're outside of the realm of standard C.

<OT>
You can't set an environment variable in a program and then have it
being set also in the calling process - the environment is a per-
process resource, not something global for all processes. So a
change of an environment variable always applies only to the
current process and its descendants (when created afterwards),
but never to its ancestors. There's no difference between setenv()
or putenv() in this respect. What you want to do can be achieved
by e.g. having your program outputting a string with what the
environment variable is to be set to and the calling program then
setting the variable to this string, but it always requires the
active participation of the calling process.
</OT>
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
J

Jack Klein

Hi All!

If anybody know, how I cann set an enviroment variable (with ANSI C) on

You can't set environment variables in ANSI/ISO C at all, on any
operating system. setenv() is not a standard C function. There is no
standard C function for setting environment variables at all.

What you need is a compiler/OS extension, which is off-topic here.
Ask in a Windows programming group such as
comp.os.ms-windows.win32.programmer.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top