Created
May 1, 2020 14:55
-
-
Save ArunTS96/3b1368e87eafda984ec05098a9031439 to your computer and use it in GitHub Desktop.
Get Environment variables as std::map windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::map<std::wstring, std::wstring> GetEnvAsMap() | |
{ | |
std::map<std::wstring, std::basic_string<wchar_t>> env; | |
auto free = [](wchar_t* p) { FreeEnvironmentStringsW(p); }; | |
const auto envBlock = std::unique_ptr<wchar_t, decltype(free)>{ | |
GetEnvironmentStringsW(), free }; | |
for (auto i = envBlock.get(); *i != L'\0'; ++i) { | |
std::wstring key; | |
std::wstring value; | |
for (; *i != L'='; ++i) | |
key += *i; | |
++i; | |
for (; *i != L'\0'; ++i) | |
value += *i; | |
env[key] = value; | |
} | |
return env; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment