VC++递归删除注册表子键

VC6中没有提供删除带有子键的注册表键,而最新的.net则提供了这个函数。不过很多程序还是用VC6写的。

我就发个自己弄出来的代码:
 
测试绝对能用!


/***
*DeleteSubKeyTree(HKEY hKey, LPCTSTR lpSubKey)
* 功能: 递归删除注册表子键
* 入口:
*      hKey :  一个HEKY类型的参数,比如HKEY_CLASSES_ROOT
*      lpSubKey: 比如".exe"
* 出口: 成功删除,返回ERROR_SUCCESS,失败则返回一个非0值。
*
* 实例:        LPCTSTR lpSubKey= "shile";
        HKEY hKey = HKEY_CLASSES_ROOT;
        long ret;
        ret = DeleteSubKeyTree(hKey, lpSubKey);
        if (ret == ERROR_SUCCESS)
        {
                SetDlgItemText(IDC_STATIC, "删除子键HKEY_CLASSES_ROOT//shile成功");
        }
*    
**************************************************/

DeleteSubKeyTree(HKEY hKey, LPCTSTR lpSubKey)
{

        LONG lResult;
        HKEY hSubKey;
        DWORD   dwIndex, cbName;
        char   szSubKey[512];
        FILETIME   ft;
        lResult   =   RegOpenKeyEx(hKey,   lpSubKey,   0,   KEY_ALL_ACCESS,&hSubKey);
        if (lResult != ERROR_SUCCESS)
        {
                RegCloseKey(hSubKey);
                return lResult;
        }
        dwIndex = 0;
        cbName = sizeof(szSubKey)/sizeof(szSubKey[0]);
        while (ERROR_SUCCESS == (lResult = RegEnumKeyEx(hSubKey, dwIndex, szSubKey, &cbName, NULL, NULL, NULL, &ft)))
        {
                DeleteSubKeyTree(hSubKey, szSubKey);
        }
        RegCloseKey(hSubKey);
        lResult = RegDeleteKey(hKey, lpSubKey);
        return lResult;
}

文章出处:http://www.diybl.com/course/3_program/c++/cppxl/20081119/152041.html

这个代码有问题,以下的可以用:

原文链接:http://blog.csdn.net/sding/archive/2008/12/14/3514562.aspx

///////////////////////////////////////////////////////////////////////////
// DeleRegTree.cpp : Defines the entry point for the console application.
// Author: sDing 2008-12-14                         
////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;

DWORD DeleteTree(HKEY RootKey, const char *pSubKey)
{
    
    HKEY hKey;
    DWORD nRet;
    DWORD NameCnt,NameMaxLen;
    DWORD KeyCnt,KeyMaxLen,MaxDateLen;
    static char sFormat[256] = "";
    strcat(sFormat, "----");

    static int dwDeep = -1;
    dwDeep++;

    nRet=RegOpenKeyEx(RootKey,pSubKey,0,KEY_ALL_ACCESS,&hKey);
    if(nRet!=ERROR_SUCCESS)
    {
        cout<<"can't open the regedit";
        return 0;
    }

    nRet = RegQueryInfoKey(hKey,NULL,NULL,NULL,&KeyCnt,&KeyMaxLen,NULL,&NameCnt,
                            &NameMaxLen,&MaxDateLen,NULL,NULL);
    if(nRet == ERROR_SUCCESS)
    {
        for(int dwIndex = KeyCnt - 1; dwIndex >= 0; dwIndex--)  //枚举键值
        {
            char sKeyName[256] = "";
            RegEnumKey(hKey, dwIndex, sKeyName, sizeof(sKeyName));

            HKEY hKeySub;
            DWORD KeyCntSub;
            char pSubKeyTemp[256] = "";
            strcpy(pSubKeyTemp, pSubKey);
            strcat(pSubKeyTemp, "//");
            strcat(pSubKeyTemp, sKeyName);
            
            nRet = RegOpenKeyEx(RootKey,pSubKeyTemp,0,KEY_ALL_ACCESS,&hKeySub);
            if(nRet == ERROR_SUCCESS)
            {
                nRet = RegQueryInfoKey(hKeySub,NULL,NULL,NULL,&KeyCntSub,&KeyMaxLen,NULL,&NameCnt,
                                        &NameMaxLen,&MaxDateLen,NULL,NULL);
                if(nRet == ERROR_SUCCESS)
                {
                    if (KeyCntSub != 0)
                    {
                        DeleteTree(RootKey, pSubKeyTemp);
                    }
                    RegCloseKey(hKeySub);
                }
            }

//          cout << sFormat << sKeyName << endl;

            RegDeleteKey(RootKey ,pSubKeyTemp);
        }

        RegCloseKey(hKey);
    }

//  sFormat[strlen(sFormat) - 4] = 0;

    if (dwDeep == 0)
    {
        RegDeleteKey(RootKey ,pSubKey);
    }

    return 0;
}

int main(int argc, char* argv[])
{
    DeleteTree(HKEY_LOCAL_MACHINE, "SYSTEM//CurrentControlSet//Control//SafeBoot//Minimal");
    
    system("pause");
    return 0;
}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/woyaowenzi/archive/2009/02/03/3861049.aspx

阅读更多
上一篇初识exe程序反汇编小感
下一篇用微信小程序开店之一——如何申请个人开发号
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭