我是否必须用 0 字节终止空 REG_SZ 值?

发布于 2024-12-04 09:43:22 字数 168 浏览 1 评论 0原文

据我所知,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最偏执的依靠 2024-12-11 09:43:22

您必须始终以空字符终止该值。如果没有内容,则该值将是该一个空字符,仅此而已。

请注意,字符的长度可以是一个字节,也可以是两个字节,具体取决于您使用的是 ANSI 还是 Unicode API。

更新:RegSetValueEx声明其最后一个参数:

lpData参数指向的信息的大小,单位为
字节。如果数据的类型为 REG_SZ、REG_EXPAND_SZ 或 REG_MULTI_SZ,
cbData 必须包含终止空字符的大小或
字符。

注册表值类型参考还指出:

字符串值

如果数据具有 REG_SZ、REG_MULTI_SZ 或 REG_EXPAND_SZ 类型,则
字符串可能未使用正确的终止 null 进行存储
人物。因此,当从注册表中读取字符串时,您
必须确保字符串在使用前正确终止;
否则,它可能会覆盖缓冲区。 (请注意,REG_MULTI_SZ 字符串
应该有两个终止空字符。)

向注册表写入字符串时,必须指定字符串的长度
字符串,包括终止空字符 (\0)。一个常见的
错误是使用strlen函数来确定长度
字符串,但要忘记 strlen 仅返回数字
字符串中的字符,不包括终止 null。
因此,字符串的长度应计算如下:
strlen( 字符串 ) + 1

现在这一切还不是很清楚,但众所周知,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 size of the information pointed to by the lpData parameter, in
bytes. If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ,
cbData must include the size of the terminating null character or
characters.

The registry value types reference also says:

String Values

If data has the REG_SZ, REG_MULTI_SZ, or REG_EXPAND_SZ type, the
string may not have been stored with the proper terminating null
characters. Therefore, when reading a string from the registry, you
must ensure that the string is properly terminated before using it;
otherwise, it may overwrite a buffer. (Note that REG_MULTI_SZ strings
should have two terminating null characters.)

When writing a string to the registry, you must specify the length of
the string, including the terminating null character (\0). A common
error is to use the strlen function to determine the length of the
string, but to forget that strlen returns only the number of
characters in the string, not including the terminating null.
Therefore, the length of the string should be calculated as follows:
strlen( string ) + 1

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).

合约呢 2024-12-11 09:43:22

目前尚不清楚“无内容”是什么意思。注册表值如果存在的话总是有内容的。

如果您希望该值的查询失败,则不要使用 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).

只为守护你 2024-12-11 09:43:22

虽然规则是使用 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 条:

    [HKLM\SOFTWARE\Classes\.***\OpenWithProgids]
    "ACDSee Pro 5.***"=""

Disc Soft - Daemon Tools,20 条:

    [HKLM\SOFTWARE\Wow6432Node\Disc Soft\DAEMON Tools Pro\FileTypesSave\.***]
    "Type"=""

PSP Audioware - PSP MultiInstaller,105 条:

    [HKCU\Software\PSP-audioware\PSP_*]
    "CurrBank"=""
    "CurrDir"=""
    "CurrPreset"=""

但是,在 Windows 7 中(位于至少 SP1),还有一些非 null 终止的 REG_SZ 字符串,但是,这些是正常(默认)空字符串。.

Microsoft - Windows 7 SP1 AppEvents\Schemes,67 个条目:

    [HKCU\AppEvents\Schemes\Apps\.*\*\.None]
    @=""

还有那些空的 .Current,有这些:

    [HKCU\AppEvents\Schemes\Apps\.*\*\.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:

    [HKLM\SOFTWARE\Classes\.***\OpenWithProgids]
    "ACDSee Pro 5.***"=""

Disc Soft - Daemon Tools, 20 entries:

    [HKLM\SOFTWARE\Wow6432Node\Disc Soft\DAEMON Tools Pro\FileTypesSave\.***]
    "Type"=""

PSP Audioware - PSP MultiInstaller, 105 entries:

    [HKCU\Software\PSP-audioware\PSP_*]
    "CurrBank"=""
    "CurrDir"=""
    "CurrPreset"=""

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:

    [HKCU\AppEvents\Schemes\Apps\.*\*\.None]
    @=""

And also those .Current which are empty, have these:

    [HKCU\AppEvents\Schemes\Apps\.*\*\.Current]
    @=""

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文