C/C++

C++API注册表操作之总结文件

本文用于总结合并本主题所使用编写的函数, 编写成类 , 便用于调用. 编写环境为MFC .


struct regedit_enum_value{
	char** szBuff; //储存 多维 子键名或者子项文件NAME
	BYTE** szByte; //储存多维子键名的数据
	DWORD* type; //子键名类型:?
	/*
#define REG_NONE ? ? ? ? ? ? ? ? ? ?( 0 ) ? // No value type
#define REG_SZ ? ? ? ? ? ? ? ? ? ? ?( 1 ) ? // Unicode nul terminated string
#define REG_EXPAND_SZ ? ? ? ? ? ? ? ( 2 ) ? // Unicode nul terminated string
#define REG_BINARY ? ? ? ? ? ? ? ? ?( 3 ) ? // Free form binary
#define REG_DWORD ? ? ? ? ? ? ? ? ? ( 4 ) ? // 32-bit number
#define REG_DWORD_LITTLE_ENDIAN ? ? ( 4 ) ? // 32-bit number (same as REG_DWORD)
#define REG_DWORD_BIG_ENDIAN ? ? ? ?( 5 ) ? // 32-bit number
#define REG_LINK ? ? ? ? ? ? ? ? ? ?( 6 ) ? // Symbolic Link (unicode)
#define REG_MULTI_SZ ? ? ? ? ? ? ? ?( 7 ) ? // Multiple Unicode strings
#define REG_RESOURCE_LIST ? ? ? ? ? ( 8 ) ? // Resource list in the resource map
#define REG_FULL_RESOURCE_DESCRIPTOR ( 9 ) ?// Resource list in the hardware description
#define REG_RESOURCE_REQUIREMENTS_LIST ( 10 )
#define REG_QWORD ? ? ? ? ? ? ? ? ? ( 11 ) ?// 64-bit number
#define REG_QWORD_LITTLE_ENDIAN ? ? ( 11 ) ?// 64-bit number (same as REG_QWORD)
	*/
	DWORD count; //数量总量
};
class regedit{
public:
	regedit();
	~regedit();
	HKEY open(char* lpSubKey);
	bool RegEnumValue(char* lpSubKey,regedit_enum_value* p); //枚举子键名
	DWORD RegEnumKey(char* lpSubKey,regedit_enum_value* p); //枚举子项目 /返回项目数
	bool GetSubKeys(HKEY hKey,LPDWORD a);//获取子项文件夹数量
	bool GetValues(HKEY hKey,LPDWORD a);//获取子键名数量
	bool GetRegDword(char* lpSubKey,char* lpKeyName,DWORD* lpValue);//获取表项DWORD值 lpValue将接受值
	bool GetRegStr(char* lpSubKey,char* lpKeyName,char* lpSubStr); //获取表项字串符
	bool GetRegUnicodeStr(char* lpSubKey,char* lpKeyName,char* lpSubStr); //获取表项长字串符值 ?lpSubStr 装载数据
	bool GetRegUnicodeStrEx(char* lpSubKey,char* lpKeyName,char* lpSubStr); //获取扩充字串符
	bool GetRegBinary(char* lpSubKey,char* lpKeyName,char* lpSubStr); //获取表项二进制数据 lpSubStr 装载数据
	int GetValueType(char* lpSubKey,char* lpValue); //获取键名value数据类型?
	bool DelRegValue(char* lpSubKey,char* lpValue);//删除键名
	bool DelRegKey(char* lpSubKey); //删除项 . 自动删除所有子项 ?传入路劲即可
	bool RegIsValue(char* lpSubKey,char* lpKeyName = NULL); //验证项目或者键名是否存在
	bool RegBreak(char* lpSubKey); //刷新注册表, ?重复操作注册表的时候 会出现 多出来的数目 可用它刷新
	bool SetRegDword(char* lpSubKey,char* lpValue,DWORD value); //写入注册表, 写入DWORD 值
	bool SetRegUnicodeStr(char* lpSubKey,char* lpValue,char* value); ?// 写入注册表多字串符值
	bool SetRegUnicodeStrEx(char* lpSubKey,char* lpValue,char* value); //写入注册表扩充字串符值
	bool SetRegStr(char* lpSubKey,char* lpValue,char* value);			//写入注册表字串符值
	bool SetRegBinary(char* lpSubKey,char* lpValue,char* value); ?//写入二进制

	
};
#include "stdafx.h"
#include "regedit.h"

/*
#define HKEY_CLASSES_ROOT ? ? (( HKEY ) 0x80000000 )
#define HKEY_CURRENT_USER ? ? (( HKEY ) 0x80000001 )
#define HKEY_LOCAL_MACHINE ? ?(( HKEY ) 0x80000002 )
#define HKEY_USERS ? ? ? ? ? ?(( HKEY ) 0x80000003 )
#define HKEY_PERFORMANCE_DATA (( HKEY ) 0x80000004 )
#if(WINVER >= 0x0400)
#define HKEY_CURRENT_CONFIG ? (( HKEY ) 0x80000005 )
#define HKEY_DYN_DATA ? ? ? ? (( HKEY ) 0x80000006 )
*/
regedit::regedit(){

}
regedit::~regedit(){

}

HKEY regedit::open(char* lpSubKey){
	const int l = strlen(lpSubKey);
	char s[21]={0};
	char path[MAX_PATH]={0};
	bool z=false;
	int c=0;
	for(int i=0;iopen(lpSubKey);
	if(!hKey || !p)
		return NULL;
	GetValues(hKey,&p->count);
	p->szBuff = new char *[p->count];
	p->szByte = new BYTE *[p->count];
	p->type = new DWORD[p->count];
	

	DWORD nameSize = MAX_PATH;
	DWORD valueSize = MAX_PATH;
#ifdef _DEBUG
	CString test;
#endif
	for(DWORD i=0;icount;i++){
		p->szBuff[i]=new char[MAX_PATH];
		p->szByte[i]=new BYTE[MAX_PATH];
		nameSize = MAX_PATH;
		valueSize = MAX_PATH;

		if(::RegEnumValue(hKey,i,p->szBuff[i],&nameSize,NULL,&p->type[i],p->szByte[i],&valueSize )!= 0 )
			return false;//REG_BINARY

		#ifdef _DEBUG
			test.Format("循环子键名:%s ?%s ?%d\n",p->szBuff[i],p->szByte[i],p->type[i]);
		OutputDebugString(test);
#endif
		
	}
	RegCloseKey(hKey);
	return true;
}
DWORD regedit::RegEnumKey(char* lpSubKey,regedit_enum_value* p){
	HKEY hKey = this->open(lpSubKey);
	if(!hKey || !p)
		return 0;
	GetSubKeys(hKey,&p->count);
	p->szBuff = new char *[p->count];
#ifdef _DEBUG
	CString test;
#endif
	for(DWORD i=0;icount;i++){
		p->szBuff[i]=new char[MAX_PATH];
		if(::RegEnumKey(hKey,i,p->szBuff[i],MAX_PATH)!=0)
			return p->count;
#ifdef _DEBUG
		test.Format("循环子项:%s,Count:%d,Index:%d\n",p->szBuff[i],p->count,i);
		OutputDebugString(test);
#endif
	}
	RegCloseKey(hKey);
	return p->count;
}

bool regedit::GetSubKeys(HKEY hKey,LPDWORD a){//获取子项数量
	if(RegQueryInfoKey(hKey,NULL,NULL,NULL,a,NULL,NULL,NULL,NULL,NULL,NULL,NULL)==ERROR_SUCCESS)
		return true;
	else
		return false;
}
bool regedit::GetValues(HKEY hKey,LPDWORD a){ //获取键名数量
	if(RegQueryInfoKey(hKey,NULL,NULL,NULL,NULL,NULL,NULL,a,NULL,NULL,NULL,NULL)==ERROR_SUCCESS)
		return true;
	else
		return false;
}
bool regedit::GetRegDword(char* lpSubKey,char* lpKeyName,DWORD* lpValue){
	HKEY hKey = this->open(lpSubKey);
	if(!hKey || !lpValue)
		return false;
	DWORD b=MAX_PATH,type=REG_DWORD;
	if(RegQueryValueEx(hKey,lpKeyName,NULL,&type,(LPBYTE)lpValue,&b)!=ERROR_SUCCESS)
		return false;
	#ifdef _DEBUG
	CString test;
	test.Format("获取DWORD:%d\n",*lpValue);
	OutputDebugString(test);
#endif
	RegCloseKey(hKey);
	return true;
}
bool regedit::GetRegStr(char* lpSubKey,char* lpKeyName,char* lpSubStr){
	HKEY hKey = this->open(lpSubKey);
	if(!hKey)
		return false;
	DWORD b=MAX_PATH,type=REG_SZ;
	BYTE tmp[MAX_PATH];
	if(RegQueryValueEx(hKey,lpKeyName,NULL,&type,tmp,&b)!=ERROR_SUCCESS)
		return false;
	lpSubStr = new char[b];
	if(RegQueryValueEx(hKey,lpKeyName,NULL,&type,(LPBYTE)lpSubStr,&b)!=ERROR_SUCCESS)
		return false;
#ifdef _DEBUG
	CString test;
	test.Format("获取长字串符(%d):%s\n",b,lpSubStr);
	OutputDebugString(test);
#endif
	RegCloseKey(hKey);
	return true;

}
bool regedit::GetRegUnicodeStr(char* lpSubKey,char* lpKeyName,char* lpSubStr){
	HKEY hKey = this->open(lpSubKey);
	if(!hKey)
		return false;
	DWORD b=MAX_PATH,type=REG_MULTI_SZ;
	BYTE tmp[MAX_PATH];
	if(RegQueryValueEx(hKey,lpKeyName,NULL,&type,tmp,&b)!=ERROR_SUCCESS)
		return false;
	lpSubStr = new char[b];
	if(RegQueryValueEx(hKey,lpKeyName,NULL,&type,(LPBYTE)lpSubStr,&b)!=ERROR_SUCCESS)
		return false;
#ifdef _DEBUG
	CString test;
	test.Format("获取长字串符(%d):%s\n",b,lpSubStr);
	OutputDebugString(test);
#endif
	RegCloseKey(hKey);
	return true;

}
bool regedit::GetRegUnicodeStrEx(char* lpSubKey,char* lpKeyName,char* lpSubStr){
	HKEY hKey = this->open(lpSubKey);
	if(!hKey)
		return false;
	DWORD b=MAX_PATH,type=REG_EXPAND_SZ;
	BYTE tmp[MAX_PATH];
	if(RegQueryValueEx(hKey,lpKeyName,NULL,&type,tmp,&b)!=ERROR_SUCCESS)
		return false;
	lpSubStr = new char[b];
	if(RegQueryValueEx(hKey,lpKeyName,NULL,&type,(LPBYTE)lpSubStr,&b)!=ERROR_SUCCESS)
		return false;
#ifdef _DEBUG
	CString test;
	test.Format("获取扩充字串符(%d):%s\n",b,lpSubStr);
	OutputDebugString(test);
#endif
	RegCloseKey(hKey);
	return true;

}
bool regedit::GetRegBinary(char* lpSubKey,char* lpKeyName,char* lpSubStr){
	HKEY hKey = this->open(lpSubKey);
	if(!hKey)
		return false;
	DWORD b=MAX_PATH,type=REG_BINARY;
	BYTE tmp[MAX_PATH];
	if(RegQueryValueEx(hKey,lpKeyName,NULL,&type,tmp,&b)!=ERROR_SUCCESS)
		return false;
	lpSubStr = new char[b];
	if(RegQueryValueEx(hKey,lpKeyName,NULL,&type,(LPBYTE)lpSubStr,&b)!=ERROR_SUCCESS)
		return false;

	#ifdef _DEBUG
	CString test;
	test.Format("获取二进制(%d):%x\n",b,lpSubStr);
	OutputDebugString(test);
#endif
	RegCloseKey(hKey);
	return true;
}
int regedit::GetValueType(char* lpSubKey,char* lpValue){
	HKEY hKey = this->open(lpSubKey);
	if(!hKey)
		return -1;
	regedit_enum_value p;
	GetValues(hKey,&p.count);
	char a[MAX_PATH];
	BYTE b[MAX_PATH];
	DWORD type;
	DWORD nameSize = MAX_PATH;
	DWORD valueSize = MAX_PATH;
	for(DWORD i=0;iopen(lpSubKey);
	if(!hKey)
		return false;
	bool a = ::RegDeleteValue(hKey,lpValue);
	RegCloseKey(hKey);
	return a;

}
bool regedit::DelRegKey(char* lpSubKey){
	

	int count = strlen(lpSubKey);
	bool c=false;
	if(lpSubKey[count-1] == '\\'){
		c=true;
		count--;
	}

		
	
	char* a = new char[count];
	char* b = new char[count];
	memset(a,'\0',count);
	memset(b,'\0',count);
	
	int len=0;
	for(int i=0,l=0;iRegEnumKey(lpSubKey,&p);
		
	h.Format("总数:%d",Count);
	OutputDebugString(h);
	

	
	for(int i=0;iDelRegKey(h.GetBuffer());
	}
	
	





#ifdef _DEBUG
	CString test;
	test.Format("获取:%s|||%s",a,b);
	OutputDebugString(test);
#endif
	HKEY hKey = this->open(a);
	if(!hKey)
		return false;
	delete a;
	::RegDeleteKey(hKey,b);
	RegCloseKey(hKey);
	

	return true;
}
bool regedit::RegIsValue(char* lpSubKey,char* lpKeyName){
	HKEY hKey = this->open(lpSubKey);
	if(!hKey)
		return false;
	BYTE a[MAX_PATH];
	DWORD as = MAX_PATH;
	if(::RegQueryValueEx(hKey,lpKeyName,NULL,NULL,a,&as)!=ERROR_SUCCESS)
		return false;
	RegCloseKey(hKey);
	return true;

}
bool regedit::RegBreak(char* lpSubKey){
	HKEY hKey = this->open(lpSubKey);
	if(!hKey)
		return false;
	if(RegFlushKey(hKey)!=ERROR_SUCCESS){
		RegCloseKey(hKey);
		return false;
	}
	else{
		RegCloseKey(hKey);
		return true;
	}
}
bool regedit::SetRegDword(char* lpSubKey,char* lpValue,DWORD value){
	HKEY hKey = this->open(lpSubKey);
	if(!hKey)
		return false;

	if(RegSetValueEx(hKey,lpValue,NULL,REG_DWORD,(LPBYTE)&value,sizeof(DWORD))!=0)
		return false;
	return true;
}
bool regedit::SetRegUnicodeStr(char* lpSubKey,char* lpValue,char* value){
	HKEY hKey = this->open(lpSubKey);
	if(!hKey)
		return false;

	if(RegSetValueEx(hKey,lpValue,NULL,REG_MULTI_SZ,(LPBYTE)value,strlen(value))!=0)
		return false;
	return true;
}
bool regedit::SetRegUnicodeStrEx(char* lpSubKey,char* lpValue,char* value){
	HKEY hKey = this->open(lpSubKey);
	if(!hKey)
		return false;

	if(RegSetValueEx(hKey,lpValue,NULL,REG_EXPAND_SZ,(LPBYTE)value,strlen(value))!=0)
		return false;
	return true;
}
bool regedit::SetRegStr(char* lpSubKey,char* lpValue,char* value){
	HKEY hKey = this->open(lpSubKey);
	if(!hKey)
		return false;

	if(RegSetValueEx(hKey,lpValue,NULL,REG_SZ,(LPBYTE)value,strlen(value))!=0)
		return false;
	return true;
}
bool regedit::SetRegBinary(char* lpSubKey,char* lpValue,char* value){
	HKEY hKey = this->open(lpSubKey);
	if(!hKey)
		return false;

	if(RegSetValueEx(hKey,lpValue,NULL,REG_BINARY,(LPBYTE)value,strlen(value))!=0)
		return false;
	return true;
}
?

Image

0 条评论

发表评论

你需要登录后才可进行发表