]> git.saurik.com Git - wxWidgets.git/commitdiff
update CRT environment block in wxSetEnv() too
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 24 May 2009 15:33:33 +0000 (15:33 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 24 May 2009 15:33:33 +0000 (15:33 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@60728 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
interface/wx/utils.h
src/msw/utils.cpp
tests/strings/crt.cpp

index 2772b268103c43e943d70311a30cee50605e65bd..d94c3c86dd90be03d1bea5c211c45d1971a9f65a 100644 (file)
@@ -342,6 +342,7 @@ All (GUI):
 MSW:
 
 - Allow changing the height of wxChoice and wxComboBox.
+- Update CRT environment block in wxSetEnv() too.
 
 i18n:
 
index 096429dee5b0087a83ad04a84977aac3f3d6c2f9..d8a7bdd01c78116ae1bc061cfa9e31080fe4bb2e 100644 (file)
@@ -169,8 +169,9 @@ void wxInfoMessageBox(wxWindow parent = NULL);
 wxChar* wxGetenv(const wxString& var);
 
 /**
-    Returns the current value of the environment variable @c var in @c value.
-    @c value may be @NULL if you just want to know if the variable exists and
+    Returns the current value of the environment variable @a var in @a value.
+
+    @a value may be @NULL if you just want to know if the variable exists and
     are not interested in its value.
 
     Returns @true if the variable exists, @false otherwise.
@@ -180,10 +181,24 @@ wxChar* wxGetenv(const wxString& var);
 bool wxGetEnv(const wxString& var, wxString* value);
 
 /**
-    Sets the value of the environment variable @c var (adding it if necessary)
-    to @c value.
+    Sets the value of the environment variable @a var (adding it if necessary)
+    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()
 
@@ -192,8 +207,9 @@ bool wxGetEnv(const wxString& var, wxString* value);
 bool wxSetEnv(const wxString& var, const wxString& value);
 
 /**
-    Removes the variable @c var from the environment. wxGetEnv() will return
-    @NULL after the call to this function.
+    Removes the variable @a var from the environment.
+
+    wxGetEnv() will return @NULL after the call to this function.
 
     Returns @true on success.
 
index 97dd87449389d1e3da696c284777ca23577fe99b..b254d8d47fe15475ddd54a925f88236d97006357 100644 (file)
@@ -598,24 +598,43 @@ bool wxGetEnv(const wxString& WXUNUSED_IN_WINCE(var),
 #endif // WinCE/32
 }
 
-bool wxDoSetEnv(const wxString& WXUNUSED_IN_WINCE(var),
-                const wxChar *WXUNUSED_IN_WINCE(value))
+bool wxDoSetEnv(const wxString& var, const wxChar *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__
     // no environment variables under CE
+    wxUnusedVar(var);
+    wxUnusedVar(value);
     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) )
     {
         wxLogLastError(_T("SetEnvironmentVariable"));
 
         return false;
     }
+#endif // compiler
 
     return true;
-#endif
+#endif // __WXWINCE__/!__WXWINCE__
 }
 
 bool wxSetEnv(const wxString& variable, const wxString& value)
index 61dc5afcc6e05d403c1dfa45d1305ba2588ac779..e4e69caa1697fe2bd8a03d09afc8971be367704d 100644 (file)
@@ -69,15 +69,24 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CrtTestCase, "CrtTestCase" );
 
 void CrtTestCase::SetGetEnv()
 {
+#define TESTVAR_NAME _T("WXTESTVAR")
+
     wxString val;
-    wxSetEnv(_T("TESTVAR"), _T("value"));
-    CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), &val) == true );
-    CPPUNIT_ASSERT( val == _T("value") );
-    wxSetEnv(_T("TESTVAR"), _T("something else"));
-    CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), &val) );
-    CPPUNIT_ASSERT( val == _T("something else") );
-    CPPUNIT_ASSERT( wxUnsetEnv(_T("TESTVAR")) );
-    CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), NULL) == false );
+    wxSetEnv(TESTVAR_NAME, _T("value"));
+    CPPUNIT_ASSERT( wxGetEnv(TESTVAR_NAME, &val) );
+    CPPUNIT_ASSERT_EQUAL( "value", val );
+    CPPUNIT_ASSERT_EQUAL( "value", wxString(wxGetenv(TESTVAR_NAME)) );
+
+    wxSetEnv(TESTVAR_NAME, _T("something else"));
+    CPPUNIT_ASSERT( wxGetEnv(TESTVAR_NAME, &val) );
+    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()