wxSetEnv for Win32 (now seems to work)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@8840 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2000-11-27 00:21:29 +00:00
parent 1fb454751d
commit 308978f6aa
5 changed files with 74 additions and 10 deletions

View File

@@ -2977,12 +2977,26 @@ Win32 and POSIX-like systems (Unix).
<wx/utils.h> <wx/utils.h>
\membersection{wxGetEnv}\label{wxgetenv} \membersection{wxGetenv}\label{wxgetenvmacro}
\func{wxChar *}{wxGetEnv}{\param{const wxString\&}{ var}} \func{wxChar *}{wxGetEnv}{\param{const wxString\&}{ var}}
Returns the current value of the environment variable {\it var} or {\tt NULL} This is a macro defined as {\tt getenv()} or its wide char version in Unicode
if it doesn't exist. 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} \membersection{wxSetEnv}\label{wxsetenv}

View File

@@ -207,9 +207,9 @@ WXDLLEXPORT bool wxHandleFatalExceptions(bool doit = TRUE);
// Environment variables // Environment variables
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxGetenv is declared in wxchar.h, but define a wrapper/synonym for it for // returns TRUE if variable exists (value may be NULL if you just want to check
// consistency with wxSetEnv // for this)
inline const wxChar *wxGetEnv(const wxString& var) { return wxGetenv(var); } WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value);
// set the env var name to the given value, return TRUE on success // set the env var name to the given value, return TRUE on success
WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value); WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value);

View File

@@ -292,19 +292,31 @@ static void TestDllLoad()
#include <wx/utils.h> #include <wx/utils.h>
static wxString MyGetEnv(const wxString& var)
{
wxString val;
if ( !wxGetEnv(var, &val) )
val = _T("<empty>");
else
val = wxString(_T('\'')) + val + _T('\'');
return val;
}
static void TestEnvironment() static void TestEnvironment()
{ {
const wxChar *var = _T("wxTestVar"); const wxChar *var = _T("wxTestVar");
puts("*** testing environment access functions ***"); 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")); 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")); 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); 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 #endif // TEST_ENVIRON

View File

@@ -457,6 +457,29 @@ bool wxDirExists(const wxString& dir)
#endif // Win32/16 #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) bool wxSetEnv(const wxString& var, const wxChar *value)
{ {
// some compilers have putenv() or _putenv() or _wputenv() but it's better // some compilers have putenv() or _putenv() or _wputenv() but it's better

View File

@@ -873,6 +873,21 @@ long wxGetFreeMemory()
// env vars // 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) bool wxSetEnv(const wxString& variable, const wxChar *value)
{ {
#if defined(HAVE_SETENV) #if defined(HAVE_SETENV)