From: Vadim Zeitlin Date: Mon, 27 Nov 2000 00:21:29 +0000 (+0000) Subject: wxSetEnv for Win32 (now seems to work) X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/308978f6aa40cd791d9ba0cd437a404e68d30734 wxSetEnv for Win32 (now seems to work) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@8840 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/latex/wx/function.tex b/docs/latex/wx/function.tex index 6c8dc48c2f..ca75cff21a 100644 --- a/docs/latex/wx/function.tex +++ b/docs/latex/wx/function.tex @@ -2977,12 +2977,26 @@ Win32 and POSIX-like systems (Unix). -\membersection{wxGetEnv}\label{wxgetenv} +\membersection{wxGetenv}\label{wxgetenvmacro} \func{wxChar *}{wxGetEnv}{\param{const wxString\&}{ var}} -Returns the current value of the environment variable {\it var} or {\tt NULL} -if it doesn't exist. +This is a macro defined as {\tt getenv()} or its wide char version in Unicode +mode. + +Note that under Win32 it may not return correct value for the variables set +with \helpref{wxSetEnv}{wxsetenv}, use \helpref{wxGetEnv}{wxgetenv} function +instead. + +\membersection{wxGetEnv}\label{wxgetenv} + +\func{bool}{wxGetEnv}{\param{const wxString\&}{ var}, \param{wxString *}{value}} + +Returns the current value of the environment variable {\it var} in {\it value}. +{\it value} may be {\tt NULL} if you just want to know if the variable exists +and are not interested in its value. + +Returns {\tt TRUE} if the variable exists, {\tt FALSE} otherwise. \membersection{wxSetEnv}\label{wxsetenv} diff --git a/include/wx/utils.h b/include/wx/utils.h index abb225b1ea..09094b4877 100644 --- a/include/wx/utils.h +++ b/include/wx/utils.h @@ -207,9 +207,9 @@ WXDLLEXPORT bool wxHandleFatalExceptions(bool doit = TRUE); // Environment variables // ---------------------------------------------------------------------------- -// wxGetenv is declared in wxchar.h, but define a wrapper/synonym for it for -// consistency with wxSetEnv -inline const wxChar *wxGetEnv(const wxString& var) { return wxGetenv(var); } +// returns TRUE if variable exists (value may be NULL if you just want to check +// for this) +WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value); // set the env var name to the given value, return TRUE on success WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value); diff --git a/samples/console/console.cpp b/samples/console/console.cpp index 39fade1b34..bf78c7d957 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -292,19 +292,31 @@ static void TestDllLoad() #include +static wxString MyGetEnv(const wxString& var) +{ + wxString val; + if ( !wxGetEnv(var, &val) ) + val = _T(""); + else + val = wxString(_T('\'')) + val + _T('\''); + + return val; +} + static void TestEnvironment() { const wxChar *var = _T("wxTestVar"); puts("*** testing environment access functions ***"); - printf("Initially getenv(%s) = '%s'\n", var, wxGetenv(var)); + printf("Initially getenv(%s) = %s\n", var, MyGetEnv(var).c_str()); wxSetEnv(var, _T("value for wxTestVar")); - printf("After wxSetEnv: getenv(%s) = '%s'\n", var, wxGetenv(var)); + printf("After wxSetEnv: getenv(%s) = %s\n", var, MyGetEnv(var).c_str()); wxSetEnv(var, _T("another value")); - printf("After 2nd wxSetEnv: getenv(%s) = '%s'\n", var, wxGetenv(var)); + printf("After 2nd wxSetEnv: getenv(%s) = %s\n", var, MyGetEnv(var).c_str()); wxUnsetEnv(var); - printf("After wxUnsetEnv: getenv(%s) = '%s'\n", var, wxGetenv(var)); + printf("After wxUnsetEnv: getenv(%s) = %s\n", var, MyGetEnv(var).c_str()); + printf("PATH = %s\n", MyGetEnv(_T("PATH"))); } #endif // TEST_ENVIRON diff --git a/src/msw/utils.cpp b/src/msw/utils.cpp index 0c647be3b5..3d64ac1705 100644 --- a/src/msw/utils.cpp +++ b/src/msw/utils.cpp @@ -457,6 +457,29 @@ bool wxDirExists(const wxString& dir) #endif // Win32/16 } +// ---------------------------------------------------------------------------- +// env vars +// ---------------------------------------------------------------------------- + +bool wxGetEnv(const wxString& var, wxString *value) +{ + // first get the size of the buffer + DWORD dwRet = ::GetEnvironmentVariable(var, NULL, 0); + if ( !dwRet ) + { + // this means that there is no such variable + return FALSE; + } + + if ( value ) + { + (void)::GetEnvironmentVariable(var, value->GetWriteBuf(dwRet), dwRet); + value->UngetWriteBuf(); + } + + return TRUE; +} + bool wxSetEnv(const wxString& var, const wxChar *value) { // some compilers have putenv() or _putenv() or _wputenv() but it's better diff --git a/src/unix/utilsunx.cpp b/src/unix/utilsunx.cpp index f7b404b19e..0c2e7f37d2 100644 --- a/src/unix/utilsunx.cpp +++ b/src/unix/utilsunx.cpp @@ -873,6 +873,21 @@ long wxGetFreeMemory() // env vars // ---------------------------------------------------------------------------- +bool wxGetEnv(const wxString& var, wxString *value); +{ + // wxGetenv is defined as getenv() + wxChar *p = wxGetenv(var); + if ( !p ) + return FALSE; + + if ( value ) + { + *value = p; + } + + return TRUE; +} + bool wxSetEnv(const wxString& variable, const wxChar *value) { #if defined(HAVE_SETENV)