RegSetValueEx 仅显示写入第一个字符 [英] RegSetValueEx Only shows writes first character

查看:63
本文介绍了RegSetValueEx 仅显示写入第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,RegSetValueEx 只写了我的字符串的第一个字母.我尝试将大小更改为我能想到的任何大小,但我只得到第一个字符串.任何帮助表示赞赏.

In the following code, RegSetValueEx is only writing the first letter of my string. I've tried changing the sizes to just about anything I can think of, and I only ever get the first string. Any help is appreciated.

LPCWSTR path = L"Test String";
size_t size = wclsen(path) * sizeof(wchar_t);

DWORD dwResult = RegSetValueEx(HKEY_LOCAL_MACHINE,
                            "SOFTWARE\\My App",
                            0,
                            REG_SZ,
                            (LPBYTE)path,
                            test);

我尝试过使用 path.size() * sizeof(wchar_t) 和我能想到的任何其他尺寸,但似乎没有任何效果.有什么想法吗?

I've tried using path.size() * sizeof(wchar_t) and any number of other sizes I could think of, but nothing seems to work right. Any ideas?

推荐答案

RegSetValueEx() 期望 REG_SZ 数据以 const TCHAR* 形式提供>,在您的情况下,根据您的编译器设置是 const CHAR* - 从您能够将 char* 传递给第二个参数这一事实就很明显,这意味着您实际上是在调用 RegSetValueExA().由于您向 RegSetValueExA() 提供了 const WCHAR*,第一个 0x00 字节被解释为空终止符,因此只有一个字符值被写入.

RegSetValueEx() expects REG_SZ data to be provided as const TCHAR*, which in your case is const CHAR* per your compiler settings - as evident by the fact that you are able to pass a char* to the second parameter, which means you are actually calling RegSetValueExA(). Since you are providing a const WCHAR* to RegSetValueExA(), the first 0x00 byte gets interpreted as a null terminator, hence only a single character value gets written.

您的选择是:

  1. RegSetValueExW(..., (const BYTE*) path, ...

CString sPath(path);RegSetValueEx(..., (const BYTE*) (LPCTSTR) sPath, ...

将项目设置切换到 Unicode 构建

Switch project settings to Unicode build

这篇关于RegSetValueEx 仅显示写入第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆