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

I would suggest a double condition "?" operation at

Buffer[I] = (NewValue[I] != '\n') ? NewValue[I] : '\0';

Here's an example of what I'm trying to say:

int main(){
    string a;
    for (;;){
        cout<<"> ";
        cin>>a;
        if (a=="q"){
            exit(0);
        }
        string b=(a=="b" || a=="B") ? (a=="b" ? "b":"B") :"somethign else";
        cout<<b<<endl;
    }
    return (0);
}

You can see that at line 9 in my example I use a double "?" operations:
In your code it should look like this:

Buffer[I] = (NewValue[I] != '\n') ? NewValue[I] : '\0';

Buffer[I] = (NewValue[I] != '\n') ? (NewValue[I]=='\0' ? '\0':NewValue[I]) : '\0';

I don't know for sure if it would work.

That didn't work :c

I solved it just now with:

                char Buffer[NewValue.size() + 1];
                for (size_t I = 0; I < (NewValue.size() + 1); I++)
                    Buffer[I] = NewValue[I] != 0xA ? NewValue[I] : 0;
                dwError = RegSetValueEx(hKey, KeyToEdit.c_str(), 0, KeyType, (LPBYTE)&Buffer, NewValue.size() + 1);

See that NewValue.size() + 1? I was using strlen(Buffer) + 1 instead but when I changed it to the above, it writes everything.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.