简体   繁体   English

如何让注册表读取 function 正常工作? (c++)

[英]How to get a registry read function to work properly? (c++)

When I compile and run the following code snippet (in visual studio 2019), it runs but returns an incorrect value of当我编译并运行以下代码片段(在 Visual Studio 2019 中)时,它运行但返回的值不正确

Key Value: 000000D1E14FF920 and actual bytes read was: 12

instead of代替

Key Value: 18363 and actual bytes read was: 6

here is the code这是代码

#include <Windows.h>
#include <iostream>
#include <string>
#include <tchar.h>

wchar_t value[255];
DWORD BufferSize = sizeof(value);
RegGetValue(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\"), _T("CurrentBuildNumber"), RRF_RT_ANY, NULL, (PVOID)&value, &BufferSize);
std::cout << "Key Value: " << value << " and actual bytes read was: " << BufferSize << std::endl;

I tried multiple methords of fixing this problem but I couldnt get it to work properly with visual studio (probably because it uses unicode)我尝试了多种解决此问题的方法,但我无法让它与 Visual Studio 一起正常工作(可能是因为它使用 unicode)

You are mixing char and TCHAR APIs.您正在混合使用charTCHAR API。 It is clear that RegGetValue() is returning a Unicode wide string, because 12 bytes is correct for a 6-character null-terminated wchar_t string.很明显, RegGetValue()正在返回一个 Unicode 宽字符串,因为 12 个字节对于 6 字符空终止wchar_t字符串是正确的。 So TCHAR is mapping to wchar_t in your build, and thus RegGetValue() is calling RegGetValueW() .所以TCHAR在你的构建中映射到wchar_t ,因此RegGetValue()正在调用RegGetValueW() You are trying to print out that wide string using std::cout , which does not support wchar_t strings, so you are actually calling the void* overload of std::basic_osteam::operator<< .您正在尝试使用不支持wchar_t字符串的std::cout打印出该宽字符串,因此您实际上是在调用std::basic_osteam::operator<<void*重载。 That is why you are seeing a memory address being printed out.这就是为什么您会看到打印出 memory 地址的原因。

To deal with TCHAR correctly, try this instead:要正确处理TCHAR ,请尝试以下操作:

#include <Windows.h>
#include <iostream>
#include <string>
#include <tchar.h>

#ifdef UNICODE
#define tcout std::wcout
#else
#define tcout std::cout
#endif

TCHAR value[255] = {};
DWORD BufferSize = sizeof(value);
RegGetValue(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\"), _T("CurrentBuildNumber"), RRF_RT_REG_SZ, NULL, &value, &BufferSize);

tcout << _T("Key Value: ") << value << _T(" and actual bytes read was: ") << (BufferSize / sizeof(TCHAR)) << std::endl;

However, you really should forget that TCHAR exists, it has no place in modern coding anymore.但是,您真的应该忘记TCHAR的存在,它不再在现代编码中占有一席之地。 Just go full Unicode only:仅 go 完整 Unicode 仅:

#include <Windows.h>
#include <iostream>
#include <string>

WCHAR value[255] = {}
DWORD BufferSize = sizeof(value);
RegGetValueW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\", L"CurrentBuildNumber", RRF_RT_REG_SZ, NULL, &value, &BufferSize);

std::wcout << L"Key Value: " << value << L" and actual bytes read was: " << (BufferSize / sizeof(WCHAR)) << std::endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM