title: RegQueryValueEx returns ERROR_SUCCESS but it does not get me the data buffer. Why?
body: <p>Following is the code:</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;iostream&gt;
  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 ,
      &amp;hKey
    );
  
    if (lRv == ERROR_SUCCESS)
    {
      DWORD BufferSize = sizeof(DWORD);
      DWORD dwRet;
      DWORD cbData = 10;
      DWORD lpType;
      wchar_t cbVal[10];
      cout&lt;&lt;"Value before calling RegQueryValueEx is " &lt;&lt; cbVal &lt;&lt; endl;
  
      dwRet = RegQueryValueEx(
        hKey,
        L"DataBaseIn",
        NULL,
        &amp;lpType,
        reinterpret_cast&lt;LPBYTE&gt;(cbVal),
        &amp;cbData
      );
      if( lpType == REG_SZ )
          cout &lt;&lt; "Reg_SZ" &lt;&lt;endl;
      if( dwRet == ERROR_SUCCESS )
        cout&lt;&lt;"Value is " &lt;&lt; cbVal &lt;&lt; endl;
      else cout&lt;&lt;"RegQueryValueEx failed " &lt;&lt; dwRet &lt;&lt; endl;
    }
  }
  
  int main()
  {
    readValueFromRegistry();
    cin.get();
    return 0;
  }
  </code></pre>
  
  <p>The output is :</p>
  
  <pre><code>Value before calling RegQueryValueEx is 0030F810
  Reg_SZ
  Value is 0030F810
  </code></pre>
  
  <p>So RegQueryValueEx returns ERROR_SUCCESS, and returns the type of the value also correctly in the lpType(the <code>Reg_SZ</code>). 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?</p>
  
  <p>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:
  <a href="https://i.stack.imgur.com/3LUE2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3LUE2.png" alt="Some of the values in the HRM key"></a></p>