读取REG_SZ的值
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void PrintSize(const char* const message, const DWORD sizeBytes)
{
cout << message << ": " << sizeBytes << " bytes ("
<< (sizeBytes/sizeof(wchar_t)) << " wchar_t's)\n";
}
int main(int argc, char *argv[])
{
const HKEY key = HKEY_LOCAL_MACHINE;
const wchar_t* const subKey = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion";
const wchar_t* const valueName = L"CommonFilesDir";
DWORD dataSize = 256;
const DWORD flags = RRF_RT_ANY;
DWORD dataType;
vector<wchar_t> buffer(dataSize / sizeof(wchar_t));
LONG result= ::RegGetValue(
key,
subKey,
valueName,
flags,
&dataType,
&buffer[0],
&dataSize);
if (result != ERROR_SUCCESS)
{
cout << "Error: " << result << '\n';
return 1;
}
PrintSize("2nd call size", dataSize);
const wstring text(buffer.data());
cout << "Read string:\n";
wcout << text << '\n';
wcout << wstring(dataSize/sizeof(wchar_t), L'*') << " <-- 2nd call size\n";
return 0;
}
相关链接:
https://stackoverflow.com/questions/38623690/showing-values-data-with-reggetvalue
https://www.coder.work/article/825515