I need to construct a multi-string (REG_MULTI_SZ) in C++ from a list of values represented in a std::vector<std::wstring>.
This is what first came to mind but was of course incorrect:
std::wstring GetMultiString(std::vector<std::wstring> Values)
{
std::wstringstream ss;
for (const std::wstring& Value : Values) {
ss << Value;
ss << L"\0";
}
ss << L"\0";
return ss.str();
}
Upon inspection, the returned string didn't have any embedded NULL characters.
The solution is actually quite simple:
The reason why the original code failed to accomplish anything is that
ss << L"\0";is the same asss << L"";and therefore does nothing.You can also pass
0instead ofL'\0':Or use
<<and std::ends:Or use
<<and C++ string literal operator""s:Or even basic_ostream::write: