Currently I'm reading Multi-String values from the registry. These strings are double null terminated and single null delimiters are between each one. So below I have the following where these are declared as so:
std::string KeyToRead, Result; DWORD KeyType.
Now I'm returning an std::string aka Result. So I iterated my Buffer (Data) and appended each null delimited string to my Result.
case REG_MULTI_SZ:
{
char Data[dwSize]; //Not sure why Codeblocks allows this :S?
dwError = RegQueryValueEx(hKey, KeyToRead.c_str(), 0, &KeyType, (BYTE*)Data, &dwSize);
char* ptr = Data;
while (*ptr)
{
Result.append(ptr);
Result += "\n";
ptr += strlen(ptr) + 1;
}
}
break;
This all works fine and dandy but I'm not sure why it allows me to declare char Data[dwSize] without using the new operator.
Anyway doing the reverse is where I get difficulty. I'm trying to turn my string into Double Null terminated string so that I can write it back to the registry but it doesn't work at all. Instead, it writes strings with no spaces or newlines.
EDIT: I got it to add spaces and newlines but I'm not sure if it's a good way of doing it and not sure how to add the extra '\0' at the end.
if (KeyType == REG_MULTI_SZ)
{
char Buffer[NewValue.size()]; //again not sure how the compiler allows this :S
for (int I = 0; I < NewValue.size(); I++)
{
Buffer[I] = (NewValue[I] != '\n') ? NewValue[I] : '\0'; //This works but I'm not sure how to add the extra '\0' at the end.
}
dwError = RegSetValueEx(hKey, KeyToEdit.c_str(), 0, KeyType, (LPBYTE)&Buffer, NewValue.size());
}
EDIT 2:
The above only works if it's one word per line in my string.. Example of working:
meh
bleh
ahaha
what
Example of not working:
meh bleh ha
what
In the not working scenario, it sets the registry key to: "meh bleh ha" but leaves out the what :S