views:

220

answers:

2

I am storing some short user data strings in the Windows registry. This worked fine until one of my users tried to store a string containing a '\' character. This was then interpreted as a registry path delimiter.

What is the best practice for avoiding this problem? Some sort of simple encoding?

Note/ I am using the Qt QSetting class to access the registry via C++, so I would appreciate an answer that is generic, rather than specific to any particular MS tool/language.

+2  A: 

RegSetKeyValue breaks out the key, sub-key, and value into different parameters.

All the registry functions in MSDN.

Kevin Montrose
I am using the Qt API to access the registry, not the MS Win32 API. I prefer not to mix the two or rewrite it all in Win32 API.
Andy Brice
Ah. I took the "generic" request to mean "low level"/language-agnostic, rather than Qt-specific.
Kevin Montrose
+2  A: 

You should be able to use the QSettings::setValue() function directly. If that does not work (for the value, not the key name) then that sounds like a bug in Qt. The section on Key Syntax does explicitly mention that slashes should not be used for key names, but don't see what would be wrong with values.

Adam Batkin
Groan. On further examination it turns out that I was storing other data. So I used the user string as the final part of the key and stored the rest of the data as the value. So it is hardly surprising it messed up. My bad.
Andy Brice