- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个将 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/
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 7 年前。 Improve t
我有一个 const char* 指向十六进制格式的数据,我需要找到我正在检查的数据的长度 NUL-terminator 但是当\x00 出现时它检测到它作为 NUL 终止符返回不正确的长度。 我该如
如果 nul 永远不能成为 Windows 中的一个文件,那么如果使用对 nul 没有相同规则的操作系统在所有目录中创建一个名为“nul”的文件会怎么样。例如,如果在 CMD 中执行“echo 123
我知道 strlen() 不计算 NUL 终止字符。我真的知道这是事实。因此,这个问题并不是要问为什么 strlen() 可能“大概”不返回正确的字符串长度,这个问题已经在 StackOverfl
虽然 CStr 通常用于 FFI,但我正在读取 &[u8],它以 NUL 结尾并确保是有效的 UTF-8,所以没有需要检查。 但是 NUL 终止符不一定在切片的末尾。将此作为 &str 获取的好方法是
虽然 CStr 通常用于 FFI,但我正在读取 &[u8],它以 NUL 结尾并确保是有效的 UTF-8,所以没有需要检查。 但是 NUL 终止符不一定在切片的末尾。将此作为 &str 获取的好方法是
我正在使用 Java BufferedWriter 逐行写入文件。问题是,当我写完文件时,它在整个文件中显示“nul”。 我不认为问题出在我正在写的内容上,因为我用System.out.print仔细
我正在使用 R 使用 fread 读取一个文件,该文件包含许多列和行。该文件如下所示: 1_17118 1_18353 1_21882 1_21955 1_22054 Ind0001 -1
c89 海合会 (海湾合作委员会) 4.6.3 你好, 我在 *search = '\0' 上得到一个堆栈转储;我认为可以用 nul 终止一个字符串。 char *ptr_name = "hello@
这个问题在这里已经有了答案: NUL undeclared- first use in this function (4 个答案) 关闭 8 年前。 char word[5]={"fayed"};
我曾经使用以下函数写入很多不同的文件 using (FileStream fs = new FileStream(Settings.PsLog, FileMode.Truncate, System.S
我正在编写一个下载器,它将 url 拆分为多个部分并使用线程下载,可能我不会使用“join”,因为 join = 无法流式传输(如果所有线程未完成,则无法写入文件) 但是问题是 f.seek 和 wr
这是我服务器端的方法: void send_err(int socket_fd, char *msg) { /// To send an error message (`ERR`).
我需要创建一个将标准错误流重定向到某个文件的新进程。创建子进程的代码没有可用的控制台,因此在某些情况下 GetStdHandle(any) 将返回 0。子进程将出于某种原因尝试复制其所有标准 IO 句
我的 Notepad++ 中有这样的字符 当我尝试复制整行时,我实际上正在复制所有内容,直到“NUL”: File:1 我想做的就是替换那些空的,什么都没有,这样我就可以复制我的整行。也许有任何关键字
我正在通过 Java 中的 UDP 接收带有字节数组的数据包。我知道最大可能的字节大小,但我不知道当前接收的大小。 如果我从这个字节数组创建一个 String 实例,那么该字符串在有用的有效负载之后将
来自 the web我知道 C 包含 NUL 关键字。 编译时出现错误 NUL undeclared first use in this function' 我的代码片段: for (;;) {
#include #include int main() { char x[] = "Happy birthday to You"; char y[25]; char z[
我看到了here如果它不是 NULL 终止的,则不可能使用 strlen 找出 (unsigned char *) 字符串长度,因为 strlen 函数将遍历字符串但不会找到任何 '\0',因此运行-
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我是一名优秀的程序员,十分优秀!