title: Enumerate a key's subkeys and their value/data? C++
body: <p>I have been trying to get the code below to read the "SOFTWARE" registry key, enumerate its subkeys such as "SOFTWARE\XXXX" then read all key names and data within each subkey (not all subkeys, just 1 level below Software). I also want to use strstr when a specific string is found, to delete the key. I have a long way to go. I'm a nooblet as well so please include as much detail as possible :) The code will currently enumerate the subkeys and tell the names of each, but not display the values or data for them. </p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;tchar.h&gt;
  #include &lt;cstdio&gt;
  #include &lt;iostream&gt;
  
  #define MAX_KEY_LENGTH 255
  #define MAX_VALUE_NAME 16383
  
  using namespace std;
  
  void QueryKey(HKEY hKey)
  {
      TCHAR    achKey[MAX_KEY_LENGTH];   // buffer for subkey name
      DWORD    cbName;                   // size of name string 
      TCHAR    achClass[MAX_PATH] = TEXT("");  // buffer for class name 
      DWORD    cchClassName = MAX_PATH;  // size of class string 
      DWORD    cSubKeys = 0;               // number of subkeys 
      DWORD    cbMaxSubKey;              // longest subkey size 
      DWORD    cchMaxClass;              // longest class string 
      DWORD    cValues;              // number of values for key 
      DWORD    cchMaxValue;          // longest value name 
      DWORD    cbMaxValueData;       // longest value data 
      DWORD    cbSecurityDescriptor; // size of security descriptor 
      FILETIME ftLastWriteTime;      // last write time 
  
      DWORD i, retCode;
  
      TCHAR achValue[MAX_VALUE_NAME];
      DWORD cchValue = MAX_VALUE_NAME;
  
      // Get the class name and the value count. 
      retCode = RegQueryInfoKey(
          hKey,                    // key handle 
          achClass,                // buffer for class name 
          &amp;cchClassName,           // size of class string 
          NULL,                    // reserved 
          &amp;cSubKeys,               // number of subkeys 
          &amp;cbMaxSubKey,            // longest subkey size 
          &amp;cchMaxClass,            // longest class string 
          &amp;cValues,                // number of values for this key 
          &amp;cchMaxValue,            // longest value name 
          &amp;cbMaxValueData,         // longest value data 
          &amp;cbSecurityDescriptor,   // security descriptor 
          &amp;ftLastWriteTime);       // last write time 
  
      // Enumerate the subkeys, until RegEnumKeyEx fails.
  
      if (cSubKeys)
      {
          printf("\nNumber of subkeys: %d\n", cSubKeys);
  
          for (i = 0; i&lt;cSubKeys; i++)
          {
              cbName = MAX_KEY_LENGTH;
              retCode = RegEnumKeyEx(hKey, i, achKey, &amp;cbName, NULL, NULL, NULL, &amp;ftLastWriteTime);
              if (retCode == ERROR_SUCCESS)
              {
                  _tprintf(TEXT("(%d) %s\n"), i + 1, achKey);
              }
          }
      }
  
      // Enumerate the key values. 
  
      BYTE* buffer = new BYTE[cbMaxValueData];
      ZeroMemory(buffer, cbMaxValueData);
  
      if (cValues)
      {
          for (i = 0, retCode = ERROR_SUCCESS; i&lt;cValues; i++)
          {
              cchValue = MAX_VALUE_NAME;
              achValue[0] = '\0';
              retCode = RegEnumValue(hKey, i, achValue, &amp;cchValue, NULL, NULL, NULL, NULL);
  
              if (retCode == ERROR_SUCCESS)
              {
                  DWORD lpData = cbMaxValueData;
                  buffer[0] = '\0';
                  LONG dwRes = RegQueryValueEx(hKey, achValue, 0, NULL, buffer, &amp;lpData);
                  _tprintf(TEXT("(%d) %s : %s\n"), i + 1, achValue, buffer);
              }
          }
      }
      delete[] buffer;
  }
  
  int RegKeyCount = 0;
  
  int main(int argc, char *argv[])
  {
      HKEY hTestKey;
      const char* subkey = "SOFTWARE\\";
  
      if (RegOpenKeyEx(HKEY_CURRENT_USER, subkey, 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY, &amp;hTestKey) == ERROR_SUCCESS)
      {
          QueryKey(hTestKey);
          RegCloseKey(hTestKey);
          cin.get();
      }
  
      return 0;
  }
  </code></pre>