title: Successive calls to RegGetValue return two different sizes for the same string
body: <p>In some code I use the Win32 <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms724868" rel="nofollow noreferrer"><strong><code>RegGetValue()</code></strong></a> API to read a string from the registry.</p>
  
  <p>I call the aforementioned API twice:</p>
  
  <ol>
  <li><p>The purpose of the first call is to <strong>get the proper size</strong> to allocate a destination buffer for the string.</p></li>
  <li><p>The second call <strong>reads the string</strong> from the registry into that buffer. </p></li>
  </ol>
  
  <p>What is odd is that I found that <code>RegGetValue()</code> returns <strong>different size</strong> values between the two calls.</p>
  
  <p>In particular, the size value returned in the second call is two bytes (equivalent to one <code>wchar_t</code>) less than the first call.</p>
  
  <p>It's worth noting that the size value compatible with the actual string length is the value returned by the <em>second call</em> (this corresponds to the actual string length, including the terminating <code>NUL</code>).<br>
  But I don't understand why the first call returns a size two bytes (one <code>wchar_t</code>) bigger than that.</p>
  
  <p>A screenshot of program output and Win32 C++ compilable repro code are attached.</p>
  
  <p><img src="https://i.stack.imgur.com/x1cev.png" alt="Different size values returned by RegGetValue()"></p>
  
  <hr>
  
  <p><strong>Repro Source Code</strong></p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  
  void PrintSize(const char* const message, const DWORD sizeBytes)
  {
      cout &lt;&lt; message &lt;&lt; ": " &lt;&lt; sizeBytes &lt;&lt; " bytes (" 
           &lt;&lt; (sizeBytes/sizeof(wchar_t)) &lt;&lt; " wchar_t's)\n";
  }
  
  
  int main()
  {
      const HKEY key = HKEY_LOCAL_MACHINE;
      const wchar_t* const subKey = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion";
      const wchar_t* const valueName = L"CommonFilesDir";
  
      //
      // Get string size
      //
      DWORD keyType = 0;
      DWORD dataSize = 0;
      const DWORD flags = RRF_RT_REG_SZ;
      LONG result = ::RegGetValue(
          key, 
          subKey,
          valueName, 
          flags, 
          &amp;keyType, 
          nullptr, 
          &amp;dataSize);
      if (result != ERROR_SUCCESS)
      {
          cout &lt;&lt; "Error: " &lt;&lt; result &lt;&lt; '\n';
          return 1;
      }
      PrintSize("1st call size", dataSize);
      const DWORD dataSize1 = dataSize; // store for later use
  
  
      //
      // Allocate buffer and read string into it
      //
      vector&lt;wchar_t&gt; buffer(dataSize / sizeof(wchar_t));
      result = ::RegGetValue(
          key, 
          subKey,
          valueName, 
          flags, 
          nullptr, 
          &amp;buffer[0], 
          &amp;dataSize);
      if (result != ERROR_SUCCESS)
      {
          cout &lt;&lt; "Error: " &lt;&lt; result &lt;&lt; '\n';
          return 1;
      }
      PrintSize("2nd call size", dataSize);
  
      const wstring text(buffer.data());
      cout &lt;&lt; "Read string:\n";
      wcout &lt;&lt; text &lt;&lt; '\n';
      wcout &lt;&lt; wstring(dataSize/sizeof(wchar_t), L'*')  &lt;&lt; "  &lt;-- 2nd call size\n";
      wcout &lt;&lt; wstring(dataSize1/sizeof(wchar_t), L'-') &lt;&lt; "  &lt;-- 1st call size\n"; 
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Operating System:</strong> Windows 7 64-bit with SP1 </p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>Some confusion seems to be arisen by the particular registry key I happened to read in the sample repro code.<br>
  So, let me clarify that I read that key from the registry just as a <em>test</em>. This is <em>not</em> production code, and I'm <em>not</em> interested in <em>that</em> particular key. Feel free to add a simple test key to the registry with some test string value.<br>
  Sorry for the confusion. </p>