gpt4 book ai didi

c++ - 为什么 RegSetValueEx 即使在我违反有关计算长度中 NUL 终止的规则时仍然有效?

转载 作者:太空宇宙 更新时间:2023-11-04 01:09:09 26 4
gpt4 key购买 nike

我有一个将 calc.exe 添加到启动的简单程序:

#include <windows.h>
#include <tchar.h>

int main(){
_tprintf(TEXT("Adding calc.exe to SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run...\n"));

HKEY hRegRunKey;
LPCTSTR lpKeyName = TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
LPCTSTR lpKeyValue = TEXT("Calculator");

LPCTSTR lpProgram = TEXT("C:\\WINDOWS\\system32\\calc.exe");
DWORD cchProgram = _tcslen(lpProgram);

_tprintf(TEXT("Path: %s. \n"), lpProgram);
_tprintf(TEXT("Length: %d. \n"), cchProgram);

if(RegOpenKeyEx( HKEY_LOCAL_MACHINE, lpKeyName, 0, KEY_SET_VALUE, &hRegRunKey) == ERROR_SUCCESS){
if(RegSetValueEx(hRegRunKey, lpKeyValue, 0, REG_SZ, (const BYTE *)lpProgram, cchProgram * sizeof(TCHAR)) != ERROR_SUCCESS){
_tprintf(TEXT("ERROR: Can't set key value.\n"));
exit(1);
}else{
_tprintf(TEXT("Key has been added sucessfully.\n"));
}
}

Sleep(5000);
RegCloseKey(hRegRunKey);
}

对我来说,c/c++/WIN32API 的世界仍然充满了谜团……所以我没有什么问题。

<强>1。当我定义字符串时,它会自动以 null 终止吗?

LPCTSTR lpProgram = TEXT("C:\\WINDOWS\\system32\\calc.exe");

或者应该这样做:

LPCTSTR lpProgram = TEXT("C:\\WINDOWS\\system32\\calc.exe\0");

<强>2。在我的代码中,RegSetValueEx 的最终参数是否设置为正确的值?

来自 MSDN - RegSetValueEx function页:

cbData [in] 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.

cchProgram 设置为 28 个字符没有空终止符。在我的系统上(我认为是因为 UNICODE?)cchProgram * sizeof(TCHAR) = 56。

我不应该将它设置为 58 以添加空终止吗?


当我运行这个程序时,如上所示,没有任何修改,我将通过修改二进制日期检查注册表中的计算器值,我得到:

43 00 3A 00 5C 00 57 00 C.:.\.W.
49 00 4E 00 44 00 4F 00 I.N.D.O.
57 00 53 00 5C 00 73 00 W.S.\.s.
79 00 73 00 74 00 65 00 y.s.t.e.
6D 00 33 00 32 00 5C 00 m.3.2.\.
63 00 61 00 6C 00 63 00 c.a.l.c.
2E 00 65 00 78 00 65 00 ..e.x.e.
00 00 ..

它是 58 个字节,包括空终止。我很困惑:/

更新

在计算 cbData 时通过将字符串长度加 1 来计算 NULL 字符会产生与不添加它完全相同的结果。

cchProgram * sizeof(TCHAR) produces same data entry as (cchProgram + 1) * sizeof(TCHAR)

提供小于字符串长度的值不会添加 NULL 字节并复制给定的字节数。

27 * sizeof(TCHAR) as cbData produces:

43 00 3A 00 5C 00 57 00 C.:.\.W.
49 00 4E 00 44 00 4F 00 I.N.D.O.
57 00 53 00 5C 00 73 00 W.S.\.s.
79 00 73 00 74 00 65 00 y.s.t.e.
6D 00 33 00 32 00 5C 00 m.3.2.\.
63 00 61 00 6C 00 63 00 c.a.l.c.
2E 00 65 00 78 00 ..e.x.

我在一些旧的 XP 上,服务包天知道是什么,我不知道其他版本的 Windows 如何处理它。

最佳答案

1: 是的,不需要 \0 就可以空终止。

Double quoted strings (") are literal constants whose type is in fact a null-terminated array of characters. So string literals enclosed between double quotes always have a null character ('\0') automatically appended at the end.

2:_tcslen() 不包含空终止符。可以添加sizeof(TCHAR)来添加。

该值仍然有效的原因可能是因为即使输入不正确,Windows 也会尝试保持健壮。它可能会自动为您附加空终止符。但是,因为文档说您必须包含空终止符,所以它可能并不总是附加它。

关于c++ - 为什么 RegSetValueEx 即使在我违反有关计算长度中 NUL 终止的规则时仍然有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16531767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com