Following is the code:
#include <windows.h>
#include <iostream>
using namespace std;
#define WIN_32_LEAN_AND_MEAN
void readValueFromRegistry(void)
{
HKEY hKey;
DWORD lRv;
LPCWSTR subKey = L"SYSTEM\\CurrentControlSet\\Services\\HRM";
lRv = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
subKey,
0,
KEY_READ ,
&hKey
);
if (lRv == ERROR_SUCCESS)
{
DWORD BufferSize = sizeof(DWORD);
DWORD dwRet;
DWORD cbData = 10;
DWORD lpType;
wchar_t cbVal[10];
cout<<"Value before calling RegQueryValueEx is " << cbVal << endl;
dwRet = RegQueryValueEx(
hKey,
L"DataBaseIn",
NULL,
&lpType,
reinterpret_cast<LPBYTE>(cbVal),
&cbData
);
if( lpType == REG_SZ )
cout << "Reg_SZ" <<endl;
if( dwRet == ERROR_SUCCESS )
cout<<"Value is " << cbVal << endl;
else cout<<"RegQueryValueEx failed " << dwRet << endl;
}
}
int main()
{
readValueFromRegistry();
cin.get();
return 0;
}
The output is :
Value before calling RegQueryValueEx is 0030F810
Reg_SZ
Value is 0030F810
So RegQueryValueEx returns ERROR_SUCCESS, and returns the type of the value also correctly in the lpType(the Reg_SZ
). But I dont get the value in the buffer.
It always seems to hold the garbage values.
What could be the issue and how to solve?
FYI: The key I am trying to access is created by a windows service developed by me. And DataBaseIn is the value I am trying to access:
The char buffer should be initialized not only declared.
wchar_t cbVal[10] = L"";
Should use wcout to print unicode strings:
wcout << cbVal ;