Skip to content

Instantly share code, notes, and snippets.

@ArunTS96
Created May 1, 2020 14:55
Show Gist options
  • Save ArunTS96/3b1368e87eafda984ec05098a9031439 to your computer and use it in GitHub Desktop.
Save ArunTS96/3b1368e87eafda984ec05098a9031439 to your computer and use it in GitHub Desktop.
Get Environment variables as std::map windows
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