Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 注册表只写入4个字符_C++_Windows_Winapi_Visual C++ - Fatal编程技术网

C++ 注册表只写入4个字符

C++ 注册表只写入4个字符,c++,windows,winapi,visual-c++,C++,Windows,Winapi,Visual C++,当我试图写一个RegEdit密钥时,他只写文件路径的前4个字符 HKEY hKey; RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_SET_VALUE, &hKey); RegSetValueEx(hKey, "Test", 0, REG_SZ, (BYTE*)file, sizeof(file)); RegCloseKey(hKey); 显然,您的

当我试图写一个RegEdit密钥时,他只写文件路径的前4个字符

HKEY hKey;
RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_SET_VALUE, &hKey);
RegSetValueEx(hKey, "Test", 0, REG_SZ, (BYTE*)file, sizeof(file));
RegCloseKey(hKey);

显然,您的
文件是一个指针
sizeof(file)
是指针的大小(以字节为单位),在您的平台上为4

什么是
文件
?如何申报?它指的是什么?为什么要对它使用
sizeof
来确定要写入多少字节?如果
file
是指向C样式字符串的指针,则
sizeof
将无法帮助您确定该字符串的长度。您需要
strlen

sizeof(指针)
在32位系统上提供4。请改为尝试
string::size


如果文件是字符数组,则

RegSetValueEx(hKey, "Test", 0, REG_SZ, (BYTE*)file, strlen(file) + 1); // API says to include terminating NULL.
lpData参数指向的信息的大小,以字节为单位。如果数据类型为REG_SZ、REG_EXPAND_SZ或REG_MULTI_SZ,cbData必须包含终止的一个或多个空字符的大小


我无法复制此代码,因为未定义
文件。文件是字符array@noaaah数组还是指向数组的指针?@noaah:不太可能。显然,
file
是一个
char*
指针。
RegSetValueEx(hKey, "Test", 0, REG_SZ, (BYTE*)file, strlen(file) + 1); // API says to include terminating NULL.