1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <cstdio>
int main()
{
const std::size_t BUFF_SZ = 255 ;
{
// read reg value as normal (narrow) chars - use RegGetValueA
// "RegGetValueA converts the stored Unicode string to an ANSI string
// before copying it to the buffer" - MSDN
char value[BUFF_SZ] {} ;
DWORD buff_sz = sizeof(value); // size of the buffer
RegGetValueA( HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "SystemRoot",
RRF_RT_REG_SZ, nullptr, value, &buff_sz );
std::cout << "value: " << value << '\n' ;
}
{
// read reg value as wide chars - use RegGetValueW
wchar_t value[BUFF_SZ] {} ;
DWORD buff_sz = sizeof(value) ; // size of the buffer in bytes
RegGetValueW( HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", L"SystemRoot", RRF_RT_ANY, NULL,
value, &buff_sz );
std::wcout << L"value: " << value << L'\n' ; // print using wcout
// convert wide char to narrow char and print using cstdio
char u8_value[BUFF_SZ] {} ; // declare an array, which is large enough to store the result
std::wcstombs( u8_value, value, BUFF_SZ ) ;
std::printf( "%s\n", u8_value ) ;
std::puts(u8_value) ;
}
}
|