Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » SetEnv()
SetEnv() [message #37243] Tue, 11 September 2012 10:28 Go to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello Mirek

I could not find a setenv function. This could be an implementation:
bool SetEnv(const char *id, const char *val) 
{
#ifdef PLATFORM_POSIX
	return setenv(id, val, 1) == 0;
#else
	return _wputenv(WString(id) + "=" + WString(val)) == 0;
#endif
}


Best regards
Iñaki
Re: SetEnv() [message #37245 is a reply to message #37243] Tue, 11 September 2012 13:05 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
I am afraid it is not so simple, consider

const VectorMap<String, String>& Environment();

I am afraid that adding it in this form could cause some confusion...
Re: SetEnv() [message #37246 is a reply to message #37245] Tue, 11 September 2012 13:43 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello Mirek

Environment() returns environment vars and values.

You can just choose a right name and interface for a function that sets an environment var.


Best regards
Iñaki
Re: SetEnv() [message #37247 is a reply to message #37246] Tue, 11 September 2012 17:13 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Sorry for not being clear enough. Problem I see is that Environment returns a reference to environment variables.

If you use SetEnv, you might expect this to change, which in practice is quite difficult to achieve (e.g. because of MT issues).

Mirek
Re: SetEnv() [message #37250 is a reply to message #37247] Wed, 12 September 2012 00:26 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello Mirek

In my case the program launches another program modifying previously PATH and adding some new variables.

I thought that this solution was easier than changing Sys() and LocalProcess functions to set environment variables for child programs.

Initially if somebody uses Environment(), has to be aware that using SetEnv() will change variables so this does not seem to be a problem.

About MT, I am not sure if environment variables are MT safe at least in Windows. Perhaps SetEnv() would have to be MT protected.


Best regards
Iñaki
Re: SetEnv() [message #37251 is a reply to message #37250] Wed, 12 September 2012 08:56 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
koldo wrote on Tue, 11 September 2012 18:26


About MT, I am not sure if environment variables are MT safe at least in Windows. Perhaps SetEnv() would have to be MT protected.


Once again, that is not what I was addressing. The problem is that I would want to fix Environment to reflect changes by SetEnv, then everything would be fine.

Unfortunately with current intefrace it is not quite possible...

Mirek
Re: SetEnv() [message #37253 is a reply to message #37251] Wed, 12 September 2012 11:19 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello Mirek

Now I understand. U++ programs read environment vars at the beginning in AppInitEnvironment__() and put a copy of them in EnvMap().

This way a SetEnv() function should update EnvMap(). For example:

bool SetEnv(const char *id, const char *val) 
{
	EnvMap().Put(WString(id), WString(val));
#ifdef PLATFORM_POSIX
	return setenv(id, val, 1) == 0;
#else
	return _wputenv(WString(id) + "=" + WString(val)) == 0;
#endif
}


However EnvMap() is not public.


Best regards
Iñaki

[Updated on: Wed, 12 September 2012 11:23]

Report message to a moderator

Re: SetEnv() [message #37254 is a reply to message #37253] Wed, 12 September 2012 11:45 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Yes, but that is not so simple in MT...

Consider that one thread takes a reference and other then uses SetEnv.

Do not get me wrong, I think SetEnv is a good idea, but there are issues to consider and think through...
Previous Topic: build Core to static library problem:Value.cpp | 168 | note:type was declared here
Next Topic: LocalProcess to read from stderr
Goto Forum:
  


Current Time: Fri Mar 29 07:55:39 CET 2024

Total time taken to generate the page: 0.01119 seconds