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


}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章