我是否必须用 0 字节终止空 REG_SZ 值?
据我所知,在 Windows 注册表中创建的字符串 (REG_SZ) 必须以 0 字节终止。 但是,如果您想以编程方式创建一个没有内容的注册表值,会发生什么情况呢?
a) 始终以 0 字节终止(注册表值是否有内容并不重要)
b) 如果有内容,则仅以 0 字节终止
谢谢!
as far as I know strings (REG_SZ) created in the windows registry must be terminated with a 0-Byte.
But what happens if you want to create programatically a registry value with no content?
a) always terminate with 0-Byte (doesn't matter if the registry value has content or not)
b) terminate only with 0-Byte if there is a content
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须始终以空字符终止该值。如果没有内容,则该值将是该一个空字符,仅此而已。
请注意,字符的长度可以是一个字节,也可以是两个字节,具体取决于您使用的是 ANSI 还是 Unicode API。
更新:
RegSetValueEx
声明其最后一个参数:
注册表值类型参考还指出:
现在这一切还不是很清楚,但众所周知,Unicode 字符串中的空终止符实际上是两个字节(BMP 中的每个其他 Unicode 字符也是如此)。
You must always terminate the value with a null character. If there is no content, then the value will be that one null character and nothing else.
Note that a character is either one or two bytes long, depending on whether you are using the ANSI or Unicode APIs.
Update: The documentation for
RegSetValueEx
states regarding its last parameter:The registry value types reference also says:
Now all this is not as clear as it could be, but it is well known that a null terminator in Unicode strings is actually two bytes (as is every other Unicode character in the BMP).
目前尚不清楚“无内容”是什么意思。注册表值如果存在的话总是有内容的。
如果您希望该值的查询失败,则不要使用
REG_SZ
创建它。相反,根本不创建它。如果您希望该值的查询返回空字符串,则可以使用一个空字节(如果是 ANSI)或两个空字节(如果是 UNICODE)创建
REG_SZ
。It's not clear what you mean by "no content". Registry values always have content if they exist at all.
If you want a query for the value to fail, then don't create it with
REG_SZ
. Instead, just don't create it at all.If you want a query for the value to return an empty string, then you create as
REG_SZ
with one null byte (if ANSI) or two null bytes (if UNICODE).虽然规则是使用 a) - 根据我的经验,a) 和 b) 实际上都有效。
通过使用 RegScanner 等工具,您可以搜索所有 REG_SZ 长度为“从 0 字节到 1 字节”的条目。
执行此操作时,您确实会看到大多数条目的长度为 1 字节(即它们是空字符串,以 null 结尾)。
在我的电脑上,总共有 75322 个“空”REG_SZ 条目,其中 74860 个条目是 1 字节。
然而,其中 462 个条目的大小为 0 字节,这意味着它们不是以 null 结尾,它们只是.. ..空,就像完全空一样。
这些“0 字节”条目中的大多数属于少数程序。
首先,这里有一些实际上创建了 REG_SZ 条目(带名称)的 3rd 方程序,只是它们根本不包含任何内容。
ACD Systems - ACDSee Pro 5 , 169 条:
Disc Soft - Daemon Tools,20 条:
PSP Audioware - PSP MultiInstaller,105 条:
但是,在 Windows 7 中(位于至少 SP1),还有一些非 null 终止的 REG_SZ 字符串,但是,这些是正常(默认)空字符串。.
Microsoft - Windows 7 SP1 AppEvents\Schemes,67 个条目:
还有那些空的 .Current,有这些:
所有这些非 null 终止的 REG_SZ 注册表设置似乎都在工作。 ..虽然实际上测试这一点并不容易,它们都是空的......但至少,当Windows或任何程序遍历这些注册表项时,没有来自Windows或任何程序的错误消息。
While the rule is to use a) - from my experience, both a) and b) will actually work.
By using a tool like RegScanner, you may search for all REG_SZ entries with a length "from 0 bytes to 1 bytes".
When doing this, you will indeed see that most entries are of 1 byte length (i.e. they are empty strings, terminated with a null).
On my pc, there are 75322 "empty" REG_SZ entries in total, and of those, 74860 entries are 1-byte.
However, 462 of the entries are 0 byte in size, meaning they are not null-terminated, they are just.. ..empty, as in completely empty.
Most of these "0 byte" entries belongs to a few programs.
First, here are some 3rd-party programs which have actually created REG_SZ entries (with names), it's just that they contain nothing at all..
ACD Systems - ACDSee Pro 5, 169 entries:
Disc Soft - Daemon Tools, 20 entries:
PSP Audioware - PSP MultiInstaller, 105 entries:
But, in Windows 7 (at least SP1), there are also a few non-null-terminated REG_SZ strings, however, these are the normal (Default) empty ones..
Microsoft - Windows 7 SP1 AppEvents \ Schemes, 67 entries:
And also those .Current which are empty, have these:
All of these non-null-terminated REG_SZ registry settings seems to be working.. ..although it's not so easy to actually test this, they being empty and all.. ..but at least, there is no error messages from Windows or any of the programs when they traverse these registry entries.