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