Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to escape slashes in strings in Windows Registry?

This is a question mostly concerning WinAPI RegSetValueEx. If you look at its description in MSDN here you'd find:

lpData [in] The data to be stored.

REG_SZ, the string must be null-terminated. With the REG_MULTI_SZ data type, the string must be terminated with two null characters. A backslash must be preceded by another backslash as an escape character. For example, specify "C:\\mydir\\myfile" to store the string "C:\mydir\myfile".

The question I have, do I really need to escape slashes? Because I've never done that before and it worked perfectly fine.

like image 623
ahmd0 Avatar asked Apr 14 '12 07:04

ahmd0


2 Answers

This is indeed a documentation error. You do not need to escape backslashes here. The exact string that you send to this API is what will be stored in the registry. No processing of backslashes will be performed.

Now, it's true that in C and C++ you need to escape certain characters in string literals, but that's not pertinent to a Win32 API documentation. That's an issue for source code to object code translation for specific languages and quite beyond the remit of this documentation.

like image 164
David Heffernan Avatar answered Oct 09 '22 14:10

David Heffernan


Yes, because \ has a meaning in C++, whereas \\ means an ordinary backslash.

When \ appears in a string, C++ compiler will look at the next character and convert the combination into something (for example \n will be converted into a "newline" character). \\ will be converted into a regular backslash. This is called "escaping" (historically, on old terminals, the ESC+key combination was used for many keys that were not on the keyboard).

like image 43
littleadv Avatar answered Oct 09 '22 13:10

littleadv