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 += "\n";
                        ptr += strlen(ptr) + 1;

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());

The above only works if it's one word per line in my string.. Example of working:


Example of not working:

meh bleh ha

In the not working scenario, it sets the registry key to: "meh bleh ha" but leaves out the what :S

Edited by triumphost

4 Years
Discussion Span
Last Post by triumphost

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<<"> ";
        if (a=="q"){
        string b=(a=="b" || a=="B") ? (a=="b" ? "b":"B") :"somethign else";
    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.

Edited by Lucaci Andrew


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.

Edited by triumphost

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.