c++注册表操作

BOOL fIsBrowser(std::string& paraStrBrowser)
{// 判断是否是浏览器

	if (std::string::npos != paraStrBrowser.find("Opera"))
	{
		paraStrBrowser = "Opera" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("Internet Explorer"))
	{
		paraStrBrowser = "IE" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("Firefox"))
	{
		paraStrBrowser = "Firefox" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("Safari"))
	{
		paraStrBrowser = "Safari" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("Tencent Traveler"))
	{
		paraStrBrowser = "腾讯TT" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("搜狗高速浏览器"))
	{
		paraStrBrowser = "Sogo" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("傲游"))
	{
		paraStrBrowser = "傲游" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("360极速浏览器"))
	{
		paraStrBrowser = "360" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("Chrome"))
	{
		paraStrBrowser = "Chrome" ;
		return TRUE ;
	}

	paraStrBrowser.erase() ;
	return FALSE ;
}

std::string strGetSubKeyValue(HKEY paraHKey, std::string& paraStrRegPath)
{
	HKEY hKey;
	long lRet = RegOpenKeyEx(paraHKey, paraStrRegPath.c_str(), 0, KEY_READ,&hKey);
	if (0 != lRet)
	{
		MessageBox(NULL, "从注册表中获取浏览器名称失败", "Error", MB_OK) ;
		exit(1) ;
	}

	int		dwIndex = 0 ; 
	char	szValueName[MAX_PATH] = {0} ; 
	DWORD	dwValueVameLen = MAX_PATH ; 
	DWORD	dwType; 
	union
	{
		BYTE data[1024]; 
		DWORD idata; 
	}lpdata; 
	DWORD dwDataLen = sizeof(lpdata); 
	DWORD Type ;
	memset(&lpdata, 0, sizeof(lpdata)); 
	while(RegEnumValue(hKey, dwIndex, (LPTSTR)szValueName, &dwValueVameLen, 0, &Type, lpdata.data, &dwDataLen) != ERROR_NO_MORE_ITEMS) 
	{ 
		if (!strcmp(szValueName, "DisplayName"))
		{// 相等看其Value是否是浏览器

			std::string strTemp((char*)lpdata.data) ;
			if (fIsBrowser(strTemp))
			{// 如果是的话,那么在函数内部做处理,然后返回
				return strTemp ;
			}
			else
			{
				return "" ;
			}
		}

		dwIndex++; 
		dwValueVameLen  =   sizeof(szValueName); 
		dwDataLen		=   sizeof(lpdata); 
		memset(&lpdata, 0, sizeof(lpdata)); 
	}

	RegCloseKey(hKey) ;

	return "" ;
}

std::string& strGetS_1_5_21Path(std::string& paraStrPath)
{
	const int MAX_KEY_LENGTH = 255 ;
	const int MAX_VALUE_NAME = 16383 ;

	HKEY hKey ;
	if (0 != RegOpenKeyEx(HKEY_USERS, NULL, 0, KEY_READ, &hKey))
	{
		MessageBox(NULL, "从注册表获取浏览器记录失败", "Error", MB_OK) ;
		exit(1) ;
	}

	DWORD dwCntSubKeys = 0 ;
	if(RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwCntSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
	{
		MessageBox(NULL, "从注册表获取浏览器记录失败", "Error", MB_OK) ;
		exit(1) ;
	}

	char			szKey[MAX_KEY_LENGTH] = {0} ;
	DWORD			dwKeyLength = MAX_KEY_LENGTH ;
	for (int i = 0; i < dwCntSubKeys; ++i)
	{
		memset(szKey, 0 ,MAX_KEY_LENGTH) ;
		dwKeyLength = MAX_KEY_LENGTH ;
		//DWORD n1 = RegEnumKeyEx(hKey, i, szKey, &dwKeyLength, NULL, NULL, NULL, NULL) ;
		if (RegEnumKeyEx(hKey, i, szKey, &dwKeyLength, NULL, NULL, NULL, NULL))
		{
			MessageBox(NULL, "从注册表获取浏览器记录失败", "Error", MB_OK) ;
			exit(1) ;
		}
		else
		{
			// 制造SubKey的Reg路径
			paraStrPath = szKey ;
			if (std::string::npos != paraStrPath.find("S-1-5-21-") && std::string::npos == paraStrPath.find("_Classes"))
			{
				paraStrPath += "\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" ;
				return paraStrPath ;
			}
		}
	}

	return paraStrPath ;
}

vector<pair<std::string, float>>& vecGetBrowser(HKEY hKeyRoot, HKEY& hKey, const std::string& strRegPath, vector<pair<std::string, float>>& retVecBrowser)
{
	//vector<pair<std::string, float>> retVecBrowser ;

	const int MAX_KEY_LENGTH = 255 ;
	const int MAX_VALUE_NAME = 16383 ;

	//std::string strRegPath("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\") ;

	// 读取注册表前期工作
	//HKEY hKey ;
	if (0 != RegOpenKeyEx(hKeyRoot, strRegPath.c_str(), 0, KEY_READ, &hKey))
	{
		MessageBox(NULL, "从注册表获取浏览器记录失败", "Error", MB_OK) ;
		exit(1) ;
	}

	// 开始遍历指定注册Key的Subkey
	DWORD dwCntSubKeys = 0 ;
	if(RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwCntSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
	{
		MessageBox(NULL, "从注册表获取浏览器记录失败", "Error", MB_OK) ;
		exit(1) ;
	}

	if (!dwCntSubKeys)
	{
		return retVecBrowser ;
	}

	char			szKey[MAX_KEY_LENGTH] = {0} ;
	DWORD			dwKeyLength = MAX_KEY_LENGTH ;
	std::string		strTmp("") ;
	std::string		strRegPathTmp("") ;
	for (int i = 0; i < dwCntSubKeys; ++i)
	{
		memset(szKey, 0 ,MAX_KEY_LENGTH) ;
		dwKeyLength = MAX_KEY_LENGTH ;
		//DWORD n1 = RegEnumKeyEx(hKey, i, szKey, &dwKeyLength, NULL, NULL, NULL, NULL) ;
		if (RegEnumKeyEx(hKey, i, szKey, &dwKeyLength, NULL, NULL, NULL, NULL))
		{
			MessageBox(NULL, "从注册表获取浏览器记录失败", "Error", MB_OK) ;
			exit(1) ;
		}
		else
		{
			// 制造SubKey的Reg路径
			strRegPathTmp = strRegPath ;
			strRegPathTmp += szKey ;
			strRegPathTmp += "\\" ;

			// 获取SubKey的Value,指定要浏览器名称
			strTmp = strGetSubKeyValue(hKeyRoot, strRegPathTmp) ;
			if (!strTmp.empty())
			{	
				pair<std::string, float> pairTemp ;
				pairTemp.first	= strTmp ;
				pairTemp.second = 0 ;

				// 首先判断该浏览器是否在,不在才加进去
				if (retVecBrowser.end() == find(retVecBrowser.begin(), retVecBrowser.end(), pairTemp))
				{
					retVecBrowser.push_back(pairTemp) ;
				}
			}
		}
	}

	return retVecBrowser ;
}

void vCaculateAverage(vector<pair<std::string, float>>& retVecBrowser)
{
	for (int i = 0; i < retVecBrowser.size(); ++i)
	{
		retVecBrowser[i].second = float(1) / retVecBrowser.size() ;
	}
}
使用
// 获取浏览器种类名称
std::string strPath("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\") ;
vecGetBrowser(HKEY_LOCAL_MACHINE, hKey, strPath, pBrowser) ;
std::string strS_1_5_21Path("") ;
vecGetBrowser(HKEY_USERS, hKey, strGetS_1_5_21Path(strS_1_5_21Path), pBrowser) ;
vCaculateAverage(pBrowser) ;



遍历注册表下面的所有键值
// 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
 
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; 
 
    TCHAR  achValue[MAX_VALUE_NAME]; 
    DWORD cchValue = MAX_VALUE_NAME; 
 
    // 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++) 
        { 
            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);
            }
        }
    } 
 
    // Enumerate the key values. 
 
    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 ) 
            { 
                _tprintf(TEXT("(%d) %s\n"), i+1, achValue); 
            } 
        }
    }
}
 
void __cdecl _tmain(void)
{
   HKEY hTestKey;
 
   if( RegOpenKeyEx( HKEY_CURRENT_USER,
        TEXT("SOFTWARE\\Microsoft"),
        0,
        KEY_READ,
        &hTestKey) == ERROR_SUCCESS
      )
   {
      QueryKey(hTestKey);
   }
   
   RegCloseKey(hTestKey);
}

另外一个例子

// reg.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include "windows.h"
#include "malloc.h"
 
int main(int argc, char* argv[])
{   
    DWORD dwIndex=0,NameSize,NameCnt,NameMaxLen,Type;
    DWORD KeySize,KeyCnt,KeyMaxLen,DateSize,MaxDateLen; 
    HKEY hKey;
    char *szKeyName;
    char *szValueName;
    LPBYTE szValueDate;
    //打开关闭注册表---------------------------------------------------------------
    LPCTSTR SubKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,SubKey,0,KEY_ALL_ACCESS,&hKey)!=
        ERROR_SUCCESS)
    {
        printf("RegOpenKeyEx错误");
        return 0;
    }
    //获取子键信息---------------------------------------------------------------
    if(RegQueryInfoKey(hKey,NULL,NULL,NULL,&KeyCnt,&KeyMaxLen,NULL,&NameCnt,&NameMaxLen,&MaxDateLen,NULL,NULL)!=ERROR_SUCCESS)
    {
        printf("RegQueryInfoKey错误");
        ::RegCloseKey(hKey);
        return 0;
    }
    //枚举子键信息---------------------------------------------------------------
    for(dwIndex=0;dwIndex<KeyCnt;dwIndex++)        //枚举子键
    {
        KeySize=KeyMaxLen+1;            //因为RegQueryInfoKey得到的长度不包括0结束字符,所以应加1
        szKeyName=(char*)malloc(KeySize);
        RegEnumKeyEx(hKey,dwIndex,szKeyName,&KeySize,NULL,NULL,NULL,NULL);//枚举子键
        printf("%s\n",szKeyName);
    }
    //枚举键值信息---------------------------------------------------------------
    for(dwIndex=0;dwIndex<NameCnt;dwIndex++)    //枚举键值
    {
        DateSize=MaxDateLen+1;
        NameSize=NameMaxLen+1;
        szValueName=(char *)malloc(NameSize);
        szValueDate=(LPBYTE)malloc(DateSize);
        RegEnumValue(hKey,dwIndex,szValueName,&NameSize,NULL,&Type,szValueDate,&DateSize);//读取键值
         
        if(Type==REG_SZ)
        {
            ///*判断键值项类型并做其它操作......*/
            printf("%s\n",szValueName);
        }
        if(Type==REG_DWORD)
        {
             
        }       
    }
    RegCloseKey(hKey);
    //创建删除子键---------------------------------------------------------------
    if (ERROR_SUCCESS!=RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\MyKey",&hKey))
    {
        printf("创建子键失败!\n");
        return 0;
    }
    else
    {
        printf("创建子键成功!\n");
    }
    if(ERROR_SUCCESS==RegDeleteKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\MyKey"))
    {
        printf("删除子键成功!\n");
    }
    else
    {
        printf("删除子键失败!\n");
        RegCloseKey(hKey);
        return 0;
    }
    RegCloseKey(hKey);
    //创建删除键值---------------------------------------------------------------
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,SubKey,0,KEY_ALL_ACCESS,&hKey)!=ERROR_SUCCESS) 
    {
        printf("创建HKEY失败!\n");
        return 0;
    }
    char *szValueName1="QQ";
    char *szValueDate1="This is QQ";
    UINT cbLen=strlen(szValueDate1);
    char *szValueName2="TT";
    UINT tmp=16;
    UINT *szValueDate2=&tmp;
    if(RegSetValueEx(hKey,szValueName1,0,REG_SZ,(const unsigned char *)szValueDate1,cbLen)==ERROR_SUCCESS)
    {
        printf("创建REG_SZ键值成功!\n");
    }
    else
    {
        printf("创建REG_SZ键值失败!\n");
        return 0;
    }
    if(RegSetValueEx(hKey,szValueName2,0,REG_DWORD,(const unsigned char *)szValueDate2,4)==ERROR_SUCCESS)
    {
        printf("创建REG_DWORD键值成功!\n");
    }
    else
    {
        printf("创建REG_DWORD键值失败!\n");
        RegCloseKey(hKey);
        return 0;
    }
    RegCloseKey(hKey);
    return 0;
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值