遍历注册表某一子键下的所有键值
// 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] = {'\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);
//找不到
if (ERROR_SUCCESS != lRet)
{
//
}
TCHAR *pszpath=szLocation;
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);
}
}