title: Why does the RegQueryValueEx() function return ERROR_FILE_NOT_FOUND while trying to read from a registry key?
body: <p>System: Windows 7 32bit<br>
  Language: C++</p>
  
  <p>I have tried to access register <code>HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\Scsi\\Scsi Port 0</code>, key <code>Driver</code> (type <code>REG_SZ</code>) -- no problem.  </p>
  
  <p>The same for reading from <code>HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM</code>, all keys (types <code>REG_SZ</code>) got slashes, for example <code>\Device\Serial0</code>.  </p>
  
  <p>While reading such keys it always returns 2 (no such file) with following example code:</p>
  
  <pre><code>HKEY hKey = 0;
  DWORD dwType = REG_SZ;
  char buf[255] = {0};
  DWORD dwBufSize = sizeof(buf);
  
  if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, TEXT("HARDWARE\\DEVICEMAP\\SERIALCOMM"), 0, KEY_QUERY_VALUE, &amp;hKey ) == ERROR_SUCCESS ) 
  {
    auto ret = RegQueryValueEx( hKey, TEXT("\Device\Serial0"), 0, &amp;dwType, (LPBYTE)buf, &amp;dwBufSize );
    // ret always == 2 for key with slashes
  --- CUT ---
  </code></pre>
  
  <p>What is the proper way to read key values with slashes in name?</p>
  
  <p><hr>
  <strong>Above has been properly answered by Cody Gray.</strong>
  <hr>
  <strong>Below another issue.</strong></p>
  
  <hr>
  
  <p>Im getting the same problem when Im using variable instead of a text string.<br>
  Iv considered both approaches with single and double slashes:</p>
  
  <pre><code>HKEY hKey = 0;
  DWORD keyType = REG_SZ;
  TCHAR buf[255] = {0};
  DWORD bufSize = sizeof(buf);
  
  QSettings winReg("HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM", QSettings::NativeFormat);
  auto comsKey = winReg.allKeys();
  
  FOREACH( auto com, comsKey )
  {
    // FOREACH - boost macro
    // comsKey = QList&lt;QString&gt; (list of key names) [from Qt framework]
    // com = QString (single key name) [from Qt framework]
    if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("HARDWARE\\DEVICEMAP\\SERIALCOMM"), 0, KEY_QUERY_VALUE, &amp;hKey ) == ERROR_SUCCESS )
    {
      wchar_t* keyw = new wchar_t();
      //com.replace("/", "\\\\"); &lt;- checked both variants commented and not commented; com == /Device/Serial0 so im converting to \\Device\\Serial0
      int size = com.size();
      mbstowcs( keyw, com.toStdString().data(), size );
      //auto ret = RegQueryValueEx( hKey, TEXT("\\Device\\Serial0"), 0, &amp;keyType, (LPBYTE)buf, &amp;bufSize ); // &lt;- this works!
      auto ret = RegQueryValueExW( hKey, (LPCWSTR)&amp;keyw, 0, &amp;keyType, (LPBYTE)buf, &amp;bufSize ); // &lt;- this one not works!
  </code></pre>
  
  <p>I have tried all variants with "\Device..", "/Device", "\Device", etc.</p>