C++对注册表删除操作

 
最近需要开发一个小工具,用于清除注册表中的一些注册信息,这里的技术点有以下几点:
1) 查询注册表指定项信息;根据给定的注册表项名称,查询项的信息,如包含子项的数量。
2) 循环查询子项;由于注册表中的子项名称不知道,只能根据指定的项下面,循环取得子项,然后匹配子项中的特定的键和键值,从而确定子项。
3) 查询子项中的键和键值。
4) 删除子项。
具体的代码如下:
 首先取得项的信息:
    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
 
    
   
    if (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)
            {
                            std::string strachKey= achKey;
                            std::string strsubKey=strRegKey +"//"+ strachKey;
                           
                HKEY hSubKey;
                            if(ERROR_SUCCESS== RegOpenKeyA( rootKey,   //打开子项
strsubKey.c_str(),
&hSubKey)
)
                            {
                                   DWORD dwValueType;
                                   DWORD dwValueLen;
                                   LPTSTR lpValue = NULL;
                                   LPCSTR szValueName = strValueName.c_str();
                    DWORD dwSize;
                             if(ERROR_SUCCESS== RegQueryValueEx( hSubKey, 
szValueName,0,
&dwValueType,
NULL,&dwValueLen)
) //查询子项中键值长度
信息
                                   {
                     
                                      lpValue = new TCHAR[dwValueLen];
                                          if( lpValue )
                                          {
                                                 dwSize = dwValueLen;
                                                 ZeroMemory( lpValue,dwValueLen );   
if( ERROR_SUCCESS==RegQueryValueEx( hSubKey,szValueName,0,&dwValueType,(LPBYTE)lpValue,&dwSize) )//查询子键的键值
                                                 {
                                                        LPCSTR value =strValue.c_str();
                                                        if(!_tcscmp(lpValue,value )) //进行键值比对,满足删除
                                                        {
//此处需要使用SHDeleteKey函数,因为子项下可能还有子项
                                                               DWORD dwError= SHDeleteKey( rootKey,
strsubKey.c_str()
);//删除子项
                                                               if( ERROR_SUCCESS == dwError )
                                                               {
                                                                      _tprintf(TEXT("Delete %s %s Key Success!/n"), lpValue,strtype.c_str());
                                                               }
                                                               else
                                                               {
                                                                      _tprintf(TEXT("Delete %s %s Key Failed! 0x%08x /n"), lpValue,strtype.c_str(),dwError);
                                                               }
                                                        }
                                                        
                                                 }
                                                 ZeroMemory( lpValue,dwValueLen );
                                          }
                                   }
                                RegCloseKey(hSubKey);
                            }
               //_tprintf(TEXT("(%d) %s/n"), i+1, achKey);
            }
        }
}
在此函数中分别调用了RegQueryInfoKey,RegEnumKeyEx,RegOpenKeyA,RegQueryValueEx,SHDeleteKey和RegCloseKey 函数,具体使用 可以参看MSDN Online
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值