VC++遞迴刪除註冊表子鍵

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.