gpt4 book ai didi

C++ RegSetValueEx 出现问题

转载 作者:行者123 更新时间:2023-11-28 05:54:32 26 4
gpt4 key购买 nike

我正在使用 RegSetValueEx 设置注册表项。问题是它只写前 2 个字符。我可以让 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 的最后一个参数必须是以字节为单位的值的大小,包括终止空值。您以字符为单位给出长度(每个字符需要两个字节)并且不包括空值。

我认为

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

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

关于C++ RegSetValueEx 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34481835/

26 4 0
文章推荐: c++ - 如何使用 Microsoft 链接器工具链接静态 MFC 库
文章推荐: html - 从数组中拉取数据到
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com