title: convert std::string to const BYTE* for RegSetValueEx()
body: <p>I have a function that gets a std::string. That function calls</p>
  
  <p><a href="http://www.google.be/search?q=RegSetValueEx" rel="nofollow noreferrer">RegSetValueEx</a></p>
  
  <p>the 5th parameter is the value of the registry value and expects a variable of type const BYTE*.
  So I have to convert the std::string to const BYTE* and also give the length of the resulting array as the 6th parameter.</p>
  
  <p>I have found a way to do it, but it feels ugly and I don't really understand what is going on. Here is a slimmed down version of that function:</p>
  
  <pre><code>void function(const std::string& newValue)
  {
      HKEY keyHandle;
      if(RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("some key"),0,KEY_ALL_ACCESS,&keyHandle) == ERROR_SUCCESS)
      {
          std::wstring wNewValue;
          wNewValue.assign(newValue.begin(),newValue.end());
  
          if (RegSetValueEx(keyHandle, TEXT("some value"), NULL, REG_SZ, (const BYTE*)(LPCTSTR)(wNewValue.c_str()), wNewValue.size()*2)==ERROR_SUCCESS)
          {
              //do something
          }
          RegCloseKey(keyHandle);
      }
  }
  </code></pre>
  
  <p>As you can see, i first make a wide string (UNICODE is defined), then use a double cast, and for the length i have to do *2, else it will only set half of the input string.</p>
  
  <p>Is this form of cast the normal/best way to do it?</p>
  
  <p>Why the * 2, what would be a better way? </p>