By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,606 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,606 IT Pros & Developers. It's quick & easy.

Enviroment variable on Linux _and_ Windows

P: n/a
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
on Linux (or I'm wrong?). I cann use of course something like this:
#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.
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Andrej Litowka <a.*******@gmx.de> wrote:
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
on Linux (or I'm wrong?). I cann use of course something like this:
#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.


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
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #2

P: n/a
On Wed, 17 Sep 2003 09:14:19 +0200, "Andrej Litowka"
<a.*******@gmx.de> wrote in comp.lang.c:
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
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.