I have been working on a program that reads and writes registry data
using RegOpenKeyEx(), RegQueryValueEx(), etc functions. The program
represents paths to things in the registry using delimted path strings
like:
"HKEY_LOCAL_MACHINE\software\Microsoft\whatever\wherever"
I (currently) use these paths to refer to both values and keys. I
recently came across a value with a backslash in the value name. I had
thought that backslashes were illegal in both key and value names.
Can anyone tell me:
a) Where can backslash characters legally exist in key names and value
names?
b) How can keys/values with backslashes in their names be accessed
properly through the Reg* functions?
I know that RegOpenKeyEx() will use backslashes as delimiters in the
lpSubKey parameter. What other Reg* functions express this behavior?
Thanks in advance
[snip]
> Can anyone tell me:
>
> a) Where can backslash characters legally exist in key names and value
> names?
path, and value if not numeric.
> b) How can keys/values with backslashes in their names be accessed
> properly through the Reg* functions?
Use an escape sequence (change backslash to some other value) and reverse it
when reading instead of writing.
> I know that RegOpenKeyEx() will use backslashes as delimiters in the
> lpSubKey parameter. What other Reg* functions express this behavior?
That information is available in the msdn libraies.
//eric
So backslash characters can be stored in key names and value names as
long as they are not numeric? So the following is legal?
"software\\Microsoft\\this\\is\\a\\key\\and\\this\\is\\a\\value",
Where "this\\is\\a\\key" is a single key and "this\\is\\a\\value" is a
value name? How do you mean "if not numeric"?
> > b) How can keys/values with backslashes in their names be accessed
> > properly through the Reg* functions?
>
> Use an escape sequence (change backslash to some other value) and reverse it
> when reading instead of writing.
I'm sorry, I must be dense. Can you give me an example to illustrate
that?
> > I know that RegOpenKeyEx() will use backslashes as delimiters in the
> > lpSubKey parameter. What other Reg* functions express this behavior?
>
> That information is available in the msdn libraies.
I've been looking around these pages:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/registry_reference.asp
The only definitive information I have found is that value names cannot
start with a backslash in their name. Is there some place where I
haven't been looking?
In path backslash is legal because it delimits, however use escaping where
backslash is not a part of the path.
In value its ok if the value is of a type that holds for example a string
value (except from in the beginning as you read on msdn).
With numeric i mean a key that can only hold numbers (DWORD and such)
>> Use an escape sequence (change backslash to some other value) and reverse
>> it
>> when reading instead of writing.
>
> I'm sorry, I must be dense. Can you give me an example to illustrate
> that?
Sure. Lets say you want to store this string "this\is\a\string"
after escaping it could look like "this\is\a\string"
meaning that "\" escapes to "\" and so on, and in reverse when reading.
That way you can use the backslash in for example path.
> I've been looking around these pages:
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/registry_reference.asp
>
> The only definitive information I have found is that value names cannot
> start with a backslash in their name. Is there some place where I
> haven't been looking?
Those pages should cover all registy. However, if it can not start with
backslash, you can assume that they mean elsewhere its ok. To be on the safe
side you can escape all backslash (of course not in the path to the key).
I think a small sample will illustrate it better:
HKEY hkey;
const char *path = "Software\\YourApp\\escaped\backslash";
const unsigned char *valuedata = "valueWith\blackslash";
RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_SET_VALUE, &hKey);
RegSetValueEx(hKey, "valueNameWith\backslash", 0, REG_SZ, valuedata,
sizeof(valuedata));
RegCloseKey(hKey);
By escaping i mean: simply replace "\" with for example "\"
//eric
I'm specifically talking about key names, paths and value names. I
understand that value data is (supposed) to be constrained by the type
(BINARY, DWORD, SZ, MULTI_SZ, LINK, etc).
> >> Use an escape sequence (change backslash to some other value) and reverse
> >> it
> >> when reading instead of writing.
> >
> > I'm sorry, I must be dense. Can you give me an example to illustrate
> > that?
>
> Sure. Lets say you want to store this string "this\is\a\string"
>
> after escaping it could look like "this\is\a\string"
>
> meaning that "\" escapes to "\" and so on, and in reverse when reading.
> That way you can use the backslash in for example path.
I'm familiar with the use of escapes in strings to encode special
values. I was specifically looking at where such values can legally be
stored in paths, key names and value names.
> > I've been looking around these pages:
> > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/registry_reference.asp
> >
> > The only definitive information I have found is that value names cannot
> > start with a backslash in their name. Is there some place where I
> > haven't been looking?
>
> Those pages should cover all registy. However, if it can not start with
> backslash, you can assume that they mean elsewhere its ok. To be on the safe
> side you can escape all backslash (of course not in the path to the key).
>
> I think a small sample will illustrate it better:
>
> HKEY hkey;
> const char *path = "Software\\YourApp\\escaped\backslash";
> const unsigned char *valuedata = "valueWith\blackslash";
> RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_SET_VALUE, &hKey);
> RegSetValueEx(hKey, "valueNameWith\backslash", 0, REG_SZ, valuedata,
> sizeof(valuedata));
> RegCloseKey(hKey);
>
> By escaping i mean: simply replace "\" with for example "\"
Okay, I think I'm on the right track now. This is how I see the whole
value name, key name, path situation now:
[bs] = '\\'
[char] = any character
[str] = "" | [char][str]
[nbs_char] = any character, excluding [bs]
[nbs_str] = "" || [nbs_char][nbs_str]
[value_name] := "" | [nbs_char][str]
[key_name] = [nbs_char][nbs_str]
[path] = [key_name][pathp]
[pathp] = "" | [bs][key_name][pathp]
Is there anything that I am missing now?
Thanks for the all your help. :)