title: RegOpenKeyEx and RegSetValueEx fail, but I dont know why
body: <p>As a starting C++ programmer I want to set a value in the windows registry. I created this textbook implementation to accomplish this, but I always get error 998 back. I guess I am missing something very simple and straightforward, but I can't figure out what it is. </p>
  
  <p>Running this code as a regular user or administrator makes no difference. </p>
  
  <pre><code>#define LEDPORT 3
  #define SUBKEY "SOFTWARE\\PATH\\OTHERPATH\\"
  
      HKEY key;
      if(RegCreateKey(HKEY_LOCAL_MACHINE,  TEXT(SUBKEY), &amp;key) == ERROR_SUCCESS)
      {
          HKEY createKey;
          DWORD value = LEDPORT;
  
          if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(SUBKEY), NULL, KEY_ALL_ACCESS, &amp;createKey) == ERROR_SUCCESS){
  
              // retVal returns error 998 and the value isn't set
              int retVal = RegSetValueEx(createKey, TEXT("PortNumber"), NULL, REG_DWORD, (BYTE *)value, sizeof(value));
  
  
              RegCloseKey(createKey);
          }
      }
  </code></pre>
  
  <p>In effect this creates the mentioned key at LocalMachine\Software\Path\OtherPath but the DWORD value "PortNumber" isnt. </p>
  
  <p>Again, I think it is something straightforward, but I spend a couple of hours of thinking what it could be and I can't figure it out.</p>