Traversing all registry keys under a subkey

// C ++ 11Demo.cpp: defining the entry point console application.

//

 

#include "stdafx.h"

#include <iostream>

using namespace std;

 

 

// QueryKey - Enumerates the subkeys of key and its associated values.

//     hKey - Key whose subkeys and values are to be enumerated.

 

#include <windows.h>

#include <stdio.h>

#include <tchar.h>

 

#define MAX_KEY_LENGTH 255

#define MAX_VALUE_NAME 16383

 

 

 

TCHAR pszSubPath[MAX_VALUE_NAME]=_T("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall");

 

// registry. Synchronization information

struct tagRegOper

{

      tagRegOper ()

      {

           //pszAutoAccreditMark=_T("C:\\Program Files (x86)\\Kingsoft\\SecManage\\Path");

           //pszSubPath=_T("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall");

           pszKeyName=_T("DisplayName");

           dwRegType = REG_SZ;

           hkeyRootPath=HKEY_LOCAL_MACHINE;

           RegAccess=KEY_READ;

      }

      // Tcःahri * Psshautoachchreditnark;

      // Tcःahri * Psshsubfth;

      Tcःahri * Psshkeyhnme;

      DWORD  dwRegType;

      HKEY   hkeyRootPath;

      REGSAM RegAccess;

};

 

 

 

// Get the synchronization information, that is, find the corresponding key from the registry

BOOL GetSynchroInfo(TCHAR* pszSubPath)

{

      tagRegOper regOper;

      HKEY hKey;

      TCHAR szLocation[MAX_PATH] = {'\0'};

      DWORD dwSize = sizeof(DWORD);

      DWORD dwIndex=0;

 

      LONG lRet = RegOpenKeyEx(regOper.hkeyRootPath,pszSubPath, 0,regOper.RegAccess, &hKey);

 

      if(ERROR_SUCCESS!=lRet)

      {

           //assert(0);

           return FALSE;

      }

 

 

      lRet = RegQueryValueEx(hKey,regOper.pszKeyName, 0, &(regOper.dwRegType), NULL, &dwSize);

 

      lRet = RegQueryValueEx(hKey, regOper.pszKeyName, NULL,&(regOper.dwRegType), (LPBYTE)&szLocation, &dwSize);

 

      // can not find

      if (ERROR_SUCCESS != lRet)

      {

           //

      }

      Tcःahri * Psspth = Sshlaoction;

 

      if(_tcsclen(szLocation))

      {

           printf("%s\n",pszpath);

           return TRUE;

      }

      return false;

 

}

 

 

TCHAR szLocation[MAX_PATH] = {'\0'};

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;

      LONG lRet;

 

      TCHAR achValue[MAX_VALUE_NAME];

      DWORD cchValue = MAX_VALUE_NAME;

      DWORD dwRegType = REG_SZ;

      DWORD dwSize = sizeof(DWORD);

 

      // Get the class name and the value count.

      retCode = RegQueryInfoKey(

           hKey,                    // key handle

           achClass,                // buffer for class name

           &cchClassName,           // size of class string

           NULL,                    // reserved

           &cSubKeys,               // number of subkeys

           &cbMaxSubKey,            // longest subkey size

           &cchMaxClass,            // longest class string

           &cValues,                // number of values for this key

           &cchMaxValue,            // longest value name

           &cbMaxValueData,         // longest value data

           &cbSecurityDescriptor,   // security descriptor

           &ftLastWriteTime);       // last write time

 

      // Enumerate the subkeys, until RegEnumKeyEx fails.

 

      if (cSubKeys)

      {

           printf( "\nNumber of subkeys: %d\n", cSubKeys);

 

           for (i=0; i<cSubKeys; i++)

           {

                 _tcscpy(pszSubPath,_T("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"));

                 cbName = MAX_KEY_LENGTH;

                 retCode = RegEnumKeyEx(hKey, i,

                      achKey,

                      &cbName,

                      NULL,

                      NULL,

                      NULL,

                      &ftLastWriteTime);

                 if (retCode == ERROR_SUCCESS)

                 {

                      _tprintf(TEXT("(%d) %s\n"), i+1, achKey);

                      _tcscat(pszSubPath,achKey);

                      GetSynchroInfo(pszSubPath);

                 }

           }

      }

}

 

void _tmain(void)

{

      HKEY hTestKey;

 

      if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,

           TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"),

           0,

           KEY_READ,

           &hTestKey) == ERROR_SUCCESS

           )

      {

           QueryKey(hTestKey);

      }

}

 

Guess you like

Origin www.cnblogs.com/gd-luojialin/p/10962955.html