// EnumReg.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
int main(int argc, char* argv[])
{
HKEY hMainKey = HKEY_CURRENT_USER;
HKEY hSubKey;
//打开HKEY_CURRENT_USER\Control Panel键句柄,并获得枚举权限,以枚举其下面的子键
LONG ReturnValue = RegOpenKeyEx (hMainKey, "Control Panel",
0L, KEY_ENUMERATE_SUB_KEYS, &hSubKey);
if (ReturnValue == ERROR_SUCCESS) //如果成功打开
{
FILETIME ftLastWriteTime; //用以保存最后一次写入时间
SYSTEMTIME sysTime; //用以转化成系统时间
TCHAR tcKeyName[128]; //用于保存每个子键的名字的内存空间
DWORD dwKeyNameSize = 128;
int i = 0, retCode = 0;
printf("%-30s%-30s", "键名", "最后一次写入时间"); //打印标题行
printf("\n");
TCHAR tcBuffer[256] = {0};
while (1)
{ //获取每个子键的名称
retCode = RegEnumKeyEx(hSubKey, i, tcKeyName, &dwKeyNameSize,
NULL, NULL, NULL, &ftLastWriteTime);
if (retCode == ERROR_NO_MORE_ITEMS)
{ //如果获取结束则跳出循环
break;
}
//将文件时间转化成系统时间
FileTimeToSystemTime(&ftLastWriteTime, &sysTime);
sprintf(tcBuffer, "%02d-%02d-%02d %02d:%02d:%02d", sysTime.wYear,
sysTime.wMonth, sysTime.wDay,
sysTime.wHour, sysTime.wMinute, sysTime.wSecond);
printf("%-30s%-30s", tcKeyName, tcBuffer);
printf("\n");
dwKeyNameSize = 128;
i++;
}
}
return 0;
}
// EnumReg2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
int main(int argc, char* argv[])
{
HKEY hMainKey = HKEY_CURRENT_USER;
HKEY hSubKey;
//打开HKEY_CURRENT_USER\Control Panel键句柄,并获得枚举和设置权限。
LONG ReturnValue = ::RegOpenKeyEx (hMainKey, "Control Panel",
0L, KEY_ENUMERATE_SUB_KEYS|KEY_QUERY_VALUE, &hSubKey);
DWORD dwSubKeyCount = 0;
if (ReturnValue == ERROR_SUCCESS) //如果成功打开
{
//查询HKEY_CURRENT_USER\Control Panel键下子键个数,不需要其他信息,所以其他参数统统传入NULL值。
RegQueryInfoKey(hSubKey, NULL, NULL, NULL, &dwSubKeyCount,
NULL, NULL, NULL, NULL, NULL, NULL, NULL);
TCHAR tcKeyName[128] = {0}; //用于保存每个子键的名字的内存空间
DWORD dwKeyNameSize = 128;
for (int i=0; i<dwSubKeyCount; i++)
{ //循环获得每个子键的名字
RegEnumKeyEx(hSubKey, i, tcKeyName, &dwKeyNameSize,
NULL, NULL, NULL, NULL);
printf("%s\n", tcKeyName);
dwKeyNameSize=128;
}
}
return 0;
}
/* ************************************
*《精通Windows API》
* 示例代码
* reg.c
* 10.3 注册表
**************************************/
/* 头文件 */
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
/* 预定义 */
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
/* ************************************
* void QueryKey(HKEY hKey)
* 功能 列举指定注册表项的子键
**************************************/
void QueryKey(HKEY hKey)
{
TCHAR achKey[MAX_KEY_LENGTH];
DWORD cbName;
TCHAR achClass[MAX_PATH] = TEXT("");
DWORD cchClassName = MAX_PATH;
DWORD cSubKeys=0;
DWORD cbMaxSubKey;
DWORD cchMaxClass;
DWORD cValues;
DWORD cchMaxValue;
DWORD cbMaxValueData;
DWORD cbSecurityDescriptor;
FILETIME ftLastWriteTime;
DWORD i, retCode;
TCHAR achValue[MAX_VALUE_NAME];
DWORD cchValue = MAX_VALUE_NAME;
// 获取类名和数量
retCode = RegQueryInfoKey(
hKey, // 键的句柄
achClass, // 类名
&cchClassName, // 类名长度
NULL, // 保留
&cSubKeys, // 子键的数量
&cbMaxSubKey, // 子键长度
&cchMaxClass, // 类长度
&cValues, // 子键键值数量
&cchMaxValue, // 子键名长度
&cbMaxValueData, // 键值长度
&cbSecurityDescriptor, // 安全描述符
&ftLastWriteTime); // 最后写时间
// 列举子键
if (cSubKeys)
{
printf( "\nNumber of subkeys: %d\n", cSubKeys);
for (i=0; i<cSubKeys; i++)
{
cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(hKey, i,
achKey,
&cbName,
NULL,
NULL,
NULL,
&ftLastWriteTime);
if (retCode == ERROR_SUCCESS)
{
printf(TEXT("(%d) %s\n"), i+1, achKey);
}
}
}
// 列举键值
if (cValues)
{
printf( "\nNumber of values: %d\n", cValues);
for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++)
{
cchValue = MAX_VALUE_NAME;
achValue[0] = '\0';
retCode = RegEnumValue(hKey, i,
achValue,
&cchValue,
NULL,
NULL,
NULL,
NULL);
if (retCode == ERROR_SUCCESS )
{
printf(TEXT("(%d) %s\n"), i+1, achValue);
}
}
}
}
/* ************************************
* void AddKey(HKEY hKey)
* 功能 增加一个子键,并设置键值
**************************************/
void AddKey(HKEY hKey)
{
HKEY hSubKey;
DWORD dwKeyValue = 100;
// 创建键
RegCreateKey(hKey,"MySoftware",&hSubKey);
// 设置键值
if( ERROR_SUCCESS != RegSetValueEx(
hSubKey,
"TEST",
0,
REG_DWORD,
(LPBYTE)&dwKeyValue,
sizeof(DWORD)))
{
printf("error\n");
}
}
/* ************************************
* void main(void)
* 功能 打开键,获得键句柄
**************************************/
void main(void)
{
HKEY hTestKey;
if( RegOpenKeyEx( HKEY_CURRENT_USER,
TEXT("SOFTWARE"),
0,
KEY_READ | KEY_WRITE,
&hTestKey) == ERROR_SUCCESS
)
{
// 增加键
AddKey(hTestKey);
// 列举子键
QueryKey(hTestKey);
}
}
AddKey()函数: