dev/EVAndPATH/Microsoft.Process.Environment/Microsoft.Process.Environment.EnvironmentManager.cpp
/// <summary>
/// Uses the stores m_Scope to figure out wheather to get the environment
/// variables for the user, or for the machine.
/// </summary>
/// <returns></returns>


Uses the stores m_Scope to figure out wheather to get the environment
variables for the user, or for the machine.
Uses the stores m_Scope to figure out wheather to get the environment variables for the user or for the machine .
StringMap EnvironmentManager::GetUserOrMachineEnvironmentVariables()
    {
        StringMap environmentVariables;
        wil::unique_hkey environmentVariablesHKey = GetRegHKeyForEVUserAndMachineScope();
        DWORD sizeOfLongestNameInCharacters;
        DWORD sizeOfLongestValueInCharacters;
        DWORD numberOfValues;
        THROW_IF_FAILED(RegQueryInfoKeyW(
            environmentVariablesHKey.get()
            , nullptr 
            , nullptr 
            , nullptr 
            , nullptr 
            , nullptr 
            , nullptr 
            , &numberOfValues 
            , &sizeOfLongestNameInCharacters
            , &sizeOfLongestValueInCharacters
            , nullptr 
            , nullptr)); 
        const DWORD NAME_LENGTH = sizeOfLongestNameInCharacters + 1;
        const DWORD VALUE_SIZE_IN_BYTES = sizeOfLongestValueInCharacters * sizeof(WCHAR);
        std::unique_ptr<wchar_t[]> environmentVariableName(new wchar_t[NAME_LENGTH]);
        std::unique_ptr<BYTE[]> environmentVariableValue(new BYTE[VALUE_SIZE_IN_BYTES]);
        for (int valueIndex = 0; valueIndex < numberOfValues; valueIndex++)
        {
            DWORD nameLength = NAME_LENGTH;
            DWORD valueSize = VALUE_SIZE_IN_BYTES;
            LSTATUS enumerationStatus = RegEnumValueW(environmentVariablesHKey.get()
                , valueIndex
                , environmentVariableName.get()
                , &nameLength
                , nullptr
                , nullptr
                , environmentVariableValue.get()
                , &valueSize);
            if (nameLength == 0)
            {
                continue;
            }
            if (enumerationStatus != ERROR_SUCCESS && enumerationStatus != ERROR_NO_MORE_ITEMS)
            {
                THROW_HR(HRESULT_FROM_WIN32(enumerationStatus));
            }
            environmentVariables.Insert(environmentVariableName.get(), reinterpret_cast<LPWSTR>(environmentVariableValue.get()));
            environmentVariableName.reset(new wchar_t[NAME_LENGTH]);
            environmentVariableValue.reset(new BYTE[VALUE_SIZE_IN_BYTES]);
        }
        return environmentVariables;
    }
StringMap EnvironmentManager :: GetUserOrMachineEnvironmentVariables ( ) { StringMap environmentVariables ; wil :: unique_hkey environmentVariablesHKey = GetRegHKeyForEVUserAndMachineScope ( ) ; DWORD sizeOfLongestNameInCharacters ; DWORD sizeOfLongestValueInCharacters ; DWORD numberOfValues ; THROW_IF_FAILED ( RegQueryInfoKeyW ( environmentVariablesHKey . get ( ) , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , & numberOfValues , & sizeOfLongestNameInCharacters , & sizeOfLongestValueInCharacters , nullptr , nullptr ) ) ; const DWORD NAME_LENGTH = sizeOfLongestNameInCharacters + 1 ; const DWORD VALUE_SIZE_IN_BYTES = sizeOfLongestValueInCharacters * sizeof ( WCHAR ) ; std :: unique_ptr < wchar_t [ ] > environmentVariableName ( new wchar_t [ NAME_LENGTH ] ) ; std :: unique_ptr < BYTE [ ] > environmentVariableValue ( new BYTE [ VALUE_SIZE_IN_BYTES ] ) ; for ( int valueIndex = 0 ; valueIndex < numberOfValues ; valueIndex ++ ) { DWORD nameLength = NAME_LENGTH ; DWORD valueSize = VALUE_SIZE_IN_BYTES ; LSTATUS enumerationStatus = RegEnumValueW ( environmentVariablesHKey . get ( ) , valueIndex , environmentVariableName . get ( ) , & nameLength , nullptr , nullptr , environmentVariableValue . get ( ) , & valueSize ) ; if ( nameLength == 0 ) { continue ; } if ( enumerationStatus != ERROR_SUCCESS && enumerationStatus != ERROR_NO_MORE_ITEMS ) { THROW_HR ( HRESULT_FROM_WIN32 ( enumerationStatus ) ) ; } environmentVariables . Insert ( environmentVariableName . get ( ) , reinterpret_cast < LPWSTR > ( environmentVariableValue . get ( ) ) ) ; environmentVariableName . reset ( new wchar_t [ NAME_LENGTH ] ) ; environmentVariableValue . reset ( new BYTE [ VALUE_SIZE_IN_BYTES ] ) ; } return environmentVariables ; }
<summary>
Uses the stores m_Scope to figure out wheather to get the environment
variables for the user, or for the machine.
<summary > Uses the stores m_Scope to figure out wheather to get the environment variables for the user or for the machine .
// While this way of calculating the max size of the names,
 // values, and total number of entries includes two calls
 // to the registry, I believe this is superior to
 // using a do/while or a while with a prime
 // because there is no chance of the loop iterating more than
 // is needed AND the size of the name and value arrays are
 // only as big as the bigges name or value.
 // lpClass
 // lpcchClass
 // lpReserved
 // lpcSubKeys
 // lpcbMaxSubKeyLen
 // lpcbMaxCLassLen
 //lpcValues
 // lpcmSecurityDescriptor
 // lpftLastWriteTime
 // +1 for null character
 // An empty name indicates the default value.
 // If there was an error getting the value
