mirror of
https://git.dev.opencascade.org/repos/occt.git
synced 2025-04-16 10:08:36 +03:00
0027345: Foundation Classes, OSD_Environment - use consistent way to retrieve environment variable value on Windows
OSD_Environment::Value() now uses GetEnvironmentVariableW() instead of _wgetenv() which might provide outdated variable value cached by C runtime library.
This commit is contained in:
parent
85719f0ef9
commit
e87d0237d8
@ -287,32 +287,36 @@ TCollection_AsciiString OSD_Environment::Value()
|
|||||||
Standard_Mutex::Sentry aLock (THE_ENV_LOCK);
|
Standard_Mutex::Sentry aLock (THE_ENV_LOCK);
|
||||||
THE_ENV_MAP.Find (myName, myValue);
|
THE_ENV_MAP.Find (myName, myValue);
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
// msvc C-runtime (_wputenv()) puts variable using WinAPI internally (calls SetEnvironmentVariableW())
|
||||||
|
// and also caches its value in its own map,
|
||||||
|
// so that _wgetenv() ignores WinAPI and retieves variable from this cache.
|
||||||
|
//
|
||||||
|
// Using _wgetenv() might lead to awkward results in context when several C-runtimes are used
|
||||||
|
// at once within application or WinAPI is used directly for setting environment variable.
|
||||||
|
//
|
||||||
|
// Using _wputenv() + GetEnvironmentVariableW() pair should provide most robust behavior in tricky scenarios.
|
||||||
|
// So that _wgetenv() users will retrieve proper value set by OSD_Environment if used C-runtime library is the same as used by OCCT,
|
||||||
|
// and OSD_Environment will retreieve most up-to-date value of environment variable nevertheless C-runtime version used (or not used at all) for setting value externally,
|
||||||
|
// considering msvc C-runtime implementation details.
|
||||||
SetLastError (ERROR_SUCCESS);
|
SetLastError (ERROR_SUCCESS);
|
||||||
wchar_t* anEnvVal = NULL;
|
|
||||||
NCollection_UtfWideString aNameWide (myName.ToCString());
|
NCollection_UtfWideString aNameWide (myName.ToCString());
|
||||||
DWORD aSize = GetEnvironmentVariableW (aNameWide.ToCString(), NULL, 0);
|
DWORD aSize = GetEnvironmentVariableW (aNameWide.ToCString(), NULL, 0);
|
||||||
if ((aSize == 0 && GetLastError() != ERROR_SUCCESS)
|
if (aSize == 0 && GetLastError() != ERROR_SUCCESS)
|
||||||
|| (anEnvVal = _wgetenv (aNameWide.ToCString())) == NULL)
|
|
||||||
{
|
{
|
||||||
_set_error (myError, ERROR_ENVVAR_NOT_FOUND);
|
_set_error (myError, ERROR_ENVVAR_NOT_FOUND);
|
||||||
return myValue;
|
return myValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
NCollection_Utf8String aValue;
|
NCollection_Utf8String aValue;
|
||||||
if (anEnvVal != NULL)
|
aSize += 1; // NULL-terminator
|
||||||
{
|
wchar_t* aBuff = new wchar_t[aSize];
|
||||||
aValue.FromUnicode (anEnvVal);
|
GetEnvironmentVariableW (aNameWide.ToCString(), aBuff, aSize);
|
||||||
}
|
aBuff[aSize - 1] = L'\0';
|
||||||
else
|
aValue.FromUnicode (aBuff);
|
||||||
{
|
delete[] aBuff;
|
||||||
aSize += 1; // NULL-terminator
|
Reset();
|
||||||
wchar_t* aBuff = new wchar_t[aSize];
|
|
||||||
GetEnvironmentVariableW (aNameWide.ToCString(), aBuff, aSize);
|
|
||||||
aBuff[aSize - 1] = L'\0';
|
|
||||||
aValue.FromUnicode (aBuff);
|
|
||||||
delete[] aBuff;
|
|
||||||
Reset();
|
|
||||||
}
|
|
||||||
myValue = aValue.ToCString();
|
myValue = aValue.ToCString();
|
||||||
#endif
|
#endif
|
||||||
return myValue;
|
return myValue;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user