• 遍历注册表某一子键下的所有键值


    // C++11Demo.cpp : 定义控制台应用程序的入口点。

    //

    #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");

    //注册表。同步信息

    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;

          }

          //TCHAR* pszAutoAccreditMark;

          //TCHAR* pszSubPath;

          TCHAR* pszKeyName;

          DWORD  dwRegType;

          HKEY   hkeyRootPath;

          REGSAM RegAccess;

    };

    //获取同步信息,即从注册表寻找对应的键值

    BOOL GetSynchroInfo(TCHAR* pszSubPath)

    {

          tagRegOper regOper;

          HKEY hKey;

          TCHAR szLocation[MAX_PATH] = {''};

          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);

          //找不到

          if (ERROR_SUCCESS != lRet)

          {

               //

          }

          TCHAR *pszpath=szLocation;

          if(_tcsclen(szLocation))

          {

               printf("%s ",pszpath);

               return TRUE;

          }

          return false;

    }

    TCHAR szLocation[MAX_PATH] = {''};

    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( " Number of subkeys: %d ", 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 "), 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);

          }

    }

  • 相关阅读:
    HDUOJ--4565 So Easy!
    简单的java实验,涉及到 类继承以及接口问题,方法体的重写(区别于重载)
    java 快速求素数
    狄斯奎诺(dijkstra 模板)
    2014 蓝桥杯 阶梯报告(含代码 详细讲解)
    HDUOJ---汉洛塔IX
    小错误系列
    HDUOJ-----4510 小Q系列故事——为什么时光不能倒流
    HDUOJ----4509湫湫系列故事——减肥记II
    HDUOJ-----4506小明系列故事——师兄帮帮忙
  • 原文地址:https://www.cnblogs.com/gd-luojialin/p/10962955.html
Copyright © 2020-2023  润新知