Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsModel/WslDistroGenerator.cpp (C++)

Code before:
                  std::vector<std::wstring>& names)
{
    if (!wslRootKey)
    {
        return false;
    }
    for (const auto& guid : guidStrings)
    {
        wil::unique_hkey distroKey{ _openDistroKey(wslRootKey, guid) };
        if (!distroKey)
        {
            // return false;
            continue;
        }

        DWORD bufferCapacity = 0;
        auto result = RegQueryValueEx(distroKey.get(), RegKeyDistroName, 0, nullptr, nullptr, &bufferCapacity);
        // request regkey binary buffer capacity only
        if (result != ERROR_SUCCESS)
        {
            // return false;
            continue;
        }
        std::unique_ptr<wchar_t[]> buffer = std::make_unique<wchar_t[]>(bufferCapacity);
        // request regkey binary content
        result = RegQueryValueEx(distroKey.get(), RegKeyDistroName, 0, nullptr, reinterpret_cast<BYTE*>(buffer.get()), &bufferCapacity);
        if (result != ERROR_SUCCESS)
        {
            // return false;
            continue;
        }

        names.emplace_back(buffer.get());
    }
    return true;
}

// Method Description:
// - Generate a list of profiles for each on the installed WSL distros. This
//   will first try to read the installed distros from the registry. If that
//   fails, we'll fall back to the legacy way of launching WSL.exe to read the
//   distros from the commandline. Reading the registry is slightly more stable
//   (see GH#7199, GH#9905), but it is certainly BODGY
// Arguments:
// - <none>
// Return Value:
// - A list of WSL profiles.
std::vector<Profile> WslDistroGenerator::GenerateProfiles()
{
    static wil::unique_hkey wslRootKey{ _openWslRegKey() };
    if (wslRootKey)

Review comment (bug):
You can write this and drop the extra size-check above:
```cpp
std::wstring buffer;
result = wil::AdaptFixedSizeToAllocatedResult<std::wstring, 256>(buffer, [&](PWSTR value, size_t valueLength, size_t* valueLengthNeededWithNull) -> HRESULT {
	auto length = static_cast<DWORD>(valueLength);
    const auto hr = RegQueryValueExW(distroKey.get(), RegKeyDistroName, 0, nullptr, value, &length);
	*valueLengthNeededWithNull = length + 1;
	return hr == ERROR_MORE_DATA ? S_OK : hr;
});
names.emplace_back(std::move(buffer));
```

Code after:
        auto result = wil::AdaptFixedSizeToAllocatedResult<std::wstring, 256>(buffer, [&](PWSTR value, size_t valueLength, size_t* valueLengthNeededWithNull) -> HRESULT {
            auto length = static_cast<DWORD>(valueLength);
            const auto status = RegQueryValueExW(distroKey.get(), RegKeyDistroName, 0, nullptr, reinterpret_cast<BYTE*>(value), &length);
            // length will receive the number of bytes - convert to a number of
            // wchar_t's. AdaptFixedSizeToAllocatedResult will resize buffer to
            // valueLengthNeededWithNull
            *valueLengthNeededWithNull = (length / sizeof(wchar_t));
            // If you add one for another trailing null, then there'll actually
            // be _two_ trailing nulls in the buffer.
            return status == ERROR_MORE_DATA ? S_OK : HRESULT_FROM_WIN32(status);
        });

        if (result != S_OK)
        {
            continue;
        }
        names.emplace_back(std::move(buffer));
    }
    return true;
}

// Method Description:
// - Generate a list of profiles for each on the installed WSL distros. This
//   will first try to read the installed distros from the registry. If that
//   fails, we'll fall back to the legacy way of launching WSL.exe to read the
//   distros from the commandline. Reading the registry is slightly more stable
//   (see GH#7199, GH#9905), but it is certainly BODGY
// Arguments:
// - <none>
// Return Value:
// - A list of WSL profiles.
std::vector<Profile> WslDistroGenerator::GenerateProfiles()
{
    wil::unique_hkey wslRootKey{ openWslRegKey() };
    if (wslRootKey)
    {
        std::vector<std::wstring> guidStrings{};
        if (getWslGuids(wslRootKey, guidStrings))
        {
            std::vector<std::wstring> names{};
            names.reserve(guidStrings.size());
            if (getWslNames(wslRootKey, guidStrings, names))
            {
                return namesToProfiles(names);
            }
        }
    }

    return legacyGenerate();
}