c++枚举和搜索注册表

原文链接:http://blog.csdn.net/earbao/article/details/8486593


#include <stdio.h>
#include <windows.h>

#define SUBKEYS 1



//枚举注册表
void EnumRegKey(){
	
	HKEY hKey=NULL,h=NULL;
	char str[MAX_PATH];
	DWORD num=sizeof(str),index=0,rc;

#if SUBKEYS
	rc=::RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE",0,KEY_ALL_ACCESS,&hKey);
#else
	rc=::RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion",0,KEY_ALL_ACCESS,&hKey);
#endif
	if(rc==ERROR_SUCCESS)
	{

#if SUBKEYS
		while(RegEnumKeyEx(hKey,index,str,&num,NULL,NULL,NULL,NULL)==0)
#else
			while(RegEnumValue(hKey,index,str,&num,NULL,NULL,NULL,NULL)==0)
#endif
			{
				printf("%s \n",str);
				index++;
				num=MAX_PATH;
			}
			printf("\n\nNumber of index =%d\n\n",index);

	}else{

		printf("Can't open the key !\n");

	}
	RegCloseKey(h);
	RegCloseKey(hKey);
}

#define  MAINKEY HKEY_LOCAL_MACHINE
int ResultCount=0;

bool StealReg(char KeyValue[MAX_PATH],char Virus[MAX_PATH])
{

	if(strcmp(KeyValue,Virus)==0)
	{

		return true;
	}else{

		return false;
	}
}
//搜索注册表
// 该函数唯一的参数是SubKey,比如"software\\Microsoft"
void EnumReg(char SubKey[MAX_PATH])
{

	char temp[MAX_PATH];
	HKEY hKey = NULL;
	char str[MAX_PATH];	
	DWORD num = sizeof(str),index = 0,rc;
	
	rc = ::RegOpenKeyEx(MAINKEY,SubKey,0,KEY_ALL_ACCESS,&hKey);
	if(rc == ERROR_SUCCESS)
	{
		while( RegEnumValue(hKey,index,str,&num,NULL,NULL,NULL,NULL)==0 )
		{	//首先遍历值,进行处理
			printf("\t%s\n",str);
			if(StealReg(str,"E:\\Program Files\\Borland\\CBuilder6"))
			{	//上面第二个参数就是你要查找的值
				ResultCount++;
			}
			index++;
			num = MAX_PATH;
		}
		index = 0;
		while( RegEnumKeyEx(hKey,index,str,&num,NULL,NULL,NULL,NULL)==0 )
		{	//然后遍历子项后进行递归
			printf("%s\n",str);
			strcpy(temp,SubKey);
			strcat(temp,"\\");
			strcat(temp,str);
			EnumReg(temp);			//递归
			
			index++;
			num = MAX_PATH;
		}
	}
	else
	{
		printf("Can't Open The Key!\n");
	}
	
	RegCloseKey(hKey);


}

void main()
{

	//EnumRegKey();
	EnumReg("software");
	printf("\n\n符合条件的值共有:%d 条!\n\n",ResultCount);


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值