RegQueryValueEx returns ERROR_SUCCESS 但它没有给我数据缓冲区。为什么?

RegQueryValueEx returns ERROR_SUCCESS but it does not get me the data buffer. Why?

代码如下:

#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;
}

输出为:

Value before calling RegQueryValueEx is 0030F810
Reg_SZ
Value is 0030F810

所以 RegQueryValueEx returns ERROR_SUCCESS 和 returns 值的类型在 lpType(Reg_SZ) 中也正确。但是我没有得到缓冲区中的值。 它似乎总是持有垃圾值。 可能是什么问题以及如何解决?

仅供参考:我尝试访问的密钥是由我开发的 windows 服务创建的。 DataBaseIn 是我要访问的值:

char 缓冲区不仅要声明还要初始化。

wchar_t cbVal[10] = L"";

应该使用 wcout 打印 unicode 字符串:

wcout << cbVal ;