2 * Copyright (C) 2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
10 * \file platform\win10\Environment.cpp
11 * \brief Implements CEnvironment WinRT specified class functions.
14 #include "platform/Environment.h"
16 #include "platform/win32/CharsetConverter.h"
18 // --------------------- Internal Function ---------------------
21 * \fn int CEnvironment::win_setenv(const std::wstring &name, const std::wstring &value = L"",
22 * updateAction action = autoDetect)
23 * \brief Internal function used to manipulate environment variables on WinRT.
25 * This function make all dirty work with setting, deleting and modifying environment variables.
27 * \param name The environment variable name.
28 * \param value (optional) the new value of environment variable.
29 * \param action (optional) the action.
30 * \return Zero on success and -1 otherwise
32 int CEnvironment::win_setenv(const std::string
&name
, const std::string
&value
/* = "" */, enum updateAction action
/* = autoDetect */)
34 std::wstring Wname
= KODI::PLATFORM::WINDOWS::ToW(name
);
35 if (Wname
.empty() || name
.find('=') != std::wstring::npos
)
37 if ((action
== addOnly
|| action
== addOrUpdateOnly
) && value
.empty())
39 if (action
== addOnly
&& !getenv(name
).empty())
42 std::wstring Wvalue
= KODI::PLATFORM::WINDOWS::ToW(value
);
45 // Update process Environment used for current process and for future new child processes
46 if (action
== deleteVariable
|| value
.empty())
47 retValue
+= SetEnvironmentVariableW(Wname
.c_str(), nullptr) ? 0 : 4; // 4 if failed
49 retValue
+= SetEnvironmentVariableW(Wname
.c_str(), Wvalue
.c_str()) ? 0 : 4; // 4 if failed
54 std::string
CEnvironment::win_getenv(const std::string
&name
)
59 uint32_t varSize
= GetEnvironmentVariableA(name
.c_str(), nullptr, 0);
61 return ""; // Not found
63 std::string
result(varSize
, 0);
64 GetEnvironmentVariableA(name
.c_str(), const_cast<char*>(result
.c_str()), varSize
);