GetEnvironmentStrings
returns a (read-only!) Pointer to the beginning of the environment block for the process.
The block is a continuous C-style string containing key=value
pairs with zero completion. The block terminates with an additional null termination.
To make access more convenient, use something like the following function:
typedef std::basic_string<TCHAR> tstring; // Generally convenient typedef std::map<tstring, tstring> environment_t; environment_t get_env() { environment_t env; auto free = [](LPTCH p) { FreeEnvironmentStrings(p); }; auto env_block = std::unique_ptr<TCHAR, decltype(free)>{ GetEnvironmentStrings(), free}; for (LPTCH i = env_block.get(); *i != T('\0'); ++i) { tstring key; tstring value; for (; *i != T('='); ++i) key += *i; ++i; for (; *i != T('\0'); ++i) value += *i; env[key] = value; } return env; }
Of course, a proper implementation would encapsulate this into a class and probably use std::stringstream
instead of manually repeating by characters, concatenating strings by char
at a time. But lazy.
Usage looks like this:
environment_t env = get_env();
source share