|
3 | 3 |
|
4 | 4 | #include "precomp.h" |
5 | 5 | #include "inc/Environment.hpp" |
6 | | -#include "wil/token_helpers.h" |
7 | 6 |
|
8 | 7 | using namespace ::Microsoft::Console::Utils; |
9 | 8 |
|
10 | 9 | // We cannot use spand or not_null because we're dealing with \0\0-terminated buffers of unknown length |
11 | 10 | #pragma warning(disable : 26481 26429) |
12 | 11 |
|
13 | | -// Function Description: |
14 | | -// - Wraps win32's CreateEnvironmentBlock to return a smart pointer. |
15 | | -EnvironmentBlockPtr Microsoft::Console::Utils::CreateEnvironmentBlock() |
16 | | -{ |
17 | | - void* newEnvironmentBlock{ nullptr }; |
18 | | - auto processToken{ wil::open_current_access_token(TOKEN_QUERY | TOKEN_DUPLICATE) }; |
19 | | - if (!::CreateEnvironmentBlock(&newEnvironmentBlock, processToken.get(), FALSE)) |
20 | | - { |
21 | | - return nullptr; |
22 | | - } |
23 | | - return EnvironmentBlockPtr{ newEnvironmentBlock }; |
24 | | -} |
25 | | - |
26 | 12 | // Function Description: |
27 | 13 | // - Updates an EnvironmentVariableMapW with the current process's unicode |
28 | 14 | // environment variables ignoring ones already set in the provided map. |
29 | 15 | // Arguments: |
30 | 16 | // - map: The map to populate with the current processes's environment variables. |
31 | | -// - environmentBlock: Optional environment block to use when filling map. If omitted, |
32 | | -// defaults to the current environment. |
33 | 17 | // Return Value: |
34 | 18 | // - S_OK if we succeeded, or an appropriate HRESULT for failing |
35 | | -HRESULT Microsoft::Console::Utils::UpdateEnvironmentMapW(EnvironmentVariableMapW& map, void* environmentBlock) noexcept |
| 19 | +HRESULT Microsoft::Console::Utils::UpdateEnvironmentMapW(EnvironmentVariableMapW& map) noexcept |
36 | 20 | try |
37 | 21 | { |
38 | | - wchar_t const* activeEnvironmentBlock{ static_cast<wchar_t const*>(environmentBlock) }; |
| 22 | + LPWCH currentEnvVars{}; |
| 23 | + auto freeCurrentEnv = wil::scope_exit([&] { |
| 24 | + if (currentEnvVars) |
| 25 | + { |
| 26 | + FreeEnvironmentStringsW(currentEnvVars); |
| 27 | + currentEnvVars = nullptr; |
| 28 | + } |
| 29 | + }); |
39 | 30 |
|
40 | | - wil::unique_environstrings_ptr currentEnvVars; |
41 | | - if (!activeEnvironmentBlock) |
42 | | - { |
43 | | - currentEnvVars.reset(::GetEnvironmentStringsW()); |
44 | | - RETURN_HR_IF_NULL(E_OUTOFMEMORY, currentEnvVars); |
45 | | - activeEnvironmentBlock = currentEnvVars.get(); |
46 | | - } |
| 31 | + currentEnvVars = ::GetEnvironmentStringsW(); |
| 32 | + RETURN_HR_IF_NULL(E_OUTOFMEMORY, currentEnvVars); |
47 | 33 |
|
48 | 34 | // Each entry is NULL-terminated; block is guaranteed to be double-NULL terminated at a minimum. |
49 | | - for (wchar_t const* lastCh{ activeEnvironmentBlock }; *lastCh != '\0'; ++lastCh) |
| 35 | + for (wchar_t const* lastCh{ currentEnvVars }; *lastCh != '\0'; ++lastCh) |
50 | 36 | { |
51 | 37 | // Copy current entry into temporary map. |
52 | 38 | const size_t cchEntry{ ::wcslen(lastCh) }; |
|
0 commit comments