update CRT environment block in wxSetEnv() too

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@60728 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2009-05-24 15:33:33 +00:00
parent 5508feb8f7
commit 787de19a13
4 changed files with 66 additions and 21 deletions

View File

@@ -342,6 +342,7 @@ All (GUI):
MSW: MSW:
- Allow changing the height of wxChoice and wxComboBox. - Allow changing the height of wxChoice and wxComboBox.
- Update CRT environment block in wxSetEnv() too.
i18n: i18n:

View File

@@ -169,8 +169,9 @@ void wxInfoMessageBox(wxWindow parent = NULL);
wxChar* wxGetenv(const wxString& var); wxChar* wxGetenv(const wxString& var);
/** /**
Returns the current value of the environment variable @c var in @c value. Returns the current value of the environment variable @a var in @a value.
@c value may be @NULL if you just want to know if the variable exists and
@a value may be @NULL if you just want to know if the variable exists and
are not interested in its value. are not interested in its value.
Returns @true if the variable exists, @false otherwise. Returns @true if the variable exists, @false otherwise.
@@ -180,10 +181,24 @@ wxChar* wxGetenv(const wxString& var);
bool wxGetEnv(const wxString& var, wxString* value); bool wxGetEnv(const wxString& var, wxString* value);
/** /**
Sets the value of the environment variable @c var (adding it if necessary) Sets the value of the environment variable @a var (adding it if necessary)
to @c value. to @a value.
Returns @true on success. Notice that under Windows platforms the program may have two different
environment blocks: the first one is that of a Windows process and is
always present, but the CRT may maintain its own independent copy of the
environment. wxSetEnv() will always update the first copy, which means that
wxGetEnv(), which uses it directly, will always return the expected value
after this call. But wxSetEnv() only updates the second copy for some
compilers/CRT implementations (currently only MSVC) and so using wxGetenv()
(notice the difference in case) may not return the updated value.
@param var
The environment variable to be set, must not contain @c '=' character.
@param value
New value of the variable.
@return
@true on success or @false if changing the value failed.
@see wxUnsetEnv() @see wxUnsetEnv()
@@ -192,8 +207,9 @@ bool wxGetEnv(const wxString& var, wxString* value);
bool wxSetEnv(const wxString& var, const wxString& value); bool wxSetEnv(const wxString& var, const wxString& value);
/** /**
Removes the variable @c var from the environment. wxGetEnv() will return Removes the variable @a var from the environment.
@NULL after the call to this function.
wxGetEnv() will return @NULL after the call to this function.
Returns @true on success. Returns @true on success.

View File

@@ -598,24 +598,43 @@ bool wxGetEnv(const wxString& WXUNUSED_IN_WINCE(var),
#endif // WinCE/32 #endif // WinCE/32
} }
bool wxDoSetEnv(const wxString& WXUNUSED_IN_WINCE(var), bool wxDoSetEnv(const wxString& var, const wxChar *value)
const wxChar *WXUNUSED_IN_WINCE(value))
{ {
// some compilers have putenv() or _putenv() or _wputenv() but it's better
// to always use Win32 function directly instead of dealing with them
#ifdef __WXWINCE__ #ifdef __WXWINCE__
// no environment variables under CE // no environment variables under CE
wxUnusedVar(var);
wxUnusedVar(value);
return false; return false;
#else #else // !__WXWINCE__
// update the CRT environment if possible as people expect getenv() to also
// work and it is not affected by Win32 SetEnvironmentVariable() call (OTOH
// the CRT does use Win32 call to update the process environment block so
// there is no need to call it)
//
// TODO: add checks for the other compilers (and update wxSetEnv()
// documentation in interface/wx/utils.h accordingly)
#if defined(__VISUALC__)
// notice that Microsoft _putenv() has different semantics from POSIX
// function with almost the same name: in particular it makes a copy of the
// string instead of using it as part of environment so we can safely call
// it here without going through all the troubles with wxSetEnvModule as in
// src/unix/utilsunx.cpp
wxString envstr = var;
envstr += '=';
if ( value )
envstr += value;
_putenv(envstr);
#else // other compiler
if ( !::SetEnvironmentVariable(var.t_str(), value) ) if ( !::SetEnvironmentVariable(var.t_str(), value) )
{ {
wxLogLastError(_T("SetEnvironmentVariable")); wxLogLastError(_T("SetEnvironmentVariable"));
return false; return false;
} }
#endif // compiler
return true; return true;
#endif #endif // __WXWINCE__/!__WXWINCE__
} }
bool wxSetEnv(const wxString& variable, const wxString& value) bool wxSetEnv(const wxString& variable, const wxString& value)

View File

@@ -69,15 +69,24 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CrtTestCase, "CrtTestCase" );
void CrtTestCase::SetGetEnv() void CrtTestCase::SetGetEnv()
{ {
#define TESTVAR_NAME _T("WXTESTVAR")
wxString val; wxString val;
wxSetEnv(_T("TESTVAR"), _T("value")); wxSetEnv(TESTVAR_NAME, _T("value"));
CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), &val) == true ); CPPUNIT_ASSERT( wxGetEnv(TESTVAR_NAME, &val) );
CPPUNIT_ASSERT( val == _T("value") ); CPPUNIT_ASSERT_EQUAL( "value", val );
wxSetEnv(_T("TESTVAR"), _T("something else")); CPPUNIT_ASSERT_EQUAL( "value", wxString(wxGetenv(TESTVAR_NAME)) );
CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), &val) );
CPPUNIT_ASSERT( val == _T("something else") ); wxSetEnv(TESTVAR_NAME, _T("something else"));
CPPUNIT_ASSERT( wxUnsetEnv(_T("TESTVAR")) ); CPPUNIT_ASSERT( wxGetEnv(TESTVAR_NAME, &val) );
CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), NULL) == false ); CPPUNIT_ASSERT_EQUAL( "something else", val );
CPPUNIT_ASSERT_EQUAL( "something else", wxString(wxGetenv(TESTVAR_NAME)) );
CPPUNIT_ASSERT( wxUnsetEnv(TESTVAR_NAME) );
CPPUNIT_ASSERT( !wxGetEnv(TESTVAR_NAME, NULL) );
CPPUNIT_ASSERT( !wxGetenv(TESTVAR_NAME) );
#undef TESTVAR_NAME
} }
void CrtTestCase::Strcmp() void CrtTestCase::Strcmp()