C++RegSetValueEx出现问题

C++ RegSetValueEx is giving problems

本文关键字:问题 C++RegSetValueEx      更新时间:2023-10-16

我正在使用RegSetValueEx设置注册表项。问题是它只写前两个字符。我可以使用RegSetValueExA(ANSI版本),但我的项目在属性中设置为Unicode,所以我想使用RegSetValueExRegSetValueExW

#include <iostream>
#include <Windows.h>
HKEY hKey;
int main()
{
    RegCreateKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\stuff", 0, NULL, NULL, KEY_ALL_ACCESS, NULL, &hKey, NULL);
    RegSetValueEx(hKey, L"Test", 0, REG_SZ, (const BYTE*)L"test", strlen("test"));
    system("PAUSE");
    return 0;
}

注册表中的输出是"te"而不是"test"

RegSetValueEx的最后一个参数必须是值的字节大小,包括终止的null。您给出的长度以字符为单位(每个字符需要两个字节),而不包括null。

我认为sizeof(L"test")会起作用,或者你可以使用(strlen("test")+1) * sizeof(wchar_t)

请参阅https://msdn.microsoft.com/en-us/library/windows/desktop/ms724923(v=vs.85).aspx