找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3622|回复: 2

【C】删除含有子KEY的KEY(WIN32API)

[复制链接]

55

主题

275

回帖

9352

积分

用户组: 管理员

UID
77
精华
16
威望
237 点
宅币
8217 个
贡献
251 次
宅之契约
0 份
在线时间
254 小时
注册时间
2014-2-22
发表于 2014-4-7 17:15:06 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
  1. DWORD RegDeleteKeys(HKEY RootKey, const char *pSubKey, bool InitVar)
  2. {
  3.     HKEY hKey;
  4.     DWORD nRet,NameCnt,NameMaxLen,KeyCnt,KeyMaxLen,MaxDateLen;
  5.     static int dwDeep = -1;
  6.     if(InitVar)
  7.         dwDeep = -1;
  8.     else
  9.         dwDeep++;
  10.     nRet=RegOpenKeyEx(RootKey,pSubKey,0,KEY_ALL_ACCESS,&hKey);
  11.     if(nRet!=ERROR_SUCCESS)
  12.     {
  13.         return 0;
  14.     }
  15.     nRet = RegQueryInfoKey(hKey,NULL,NULL,NULL,&KeyCnt,&KeyMaxLen,NULL,&NameCnt,&NameMaxLen,&MaxDateLen,NULL,NULL);
  16.     if(nRet == ERROR_SUCCESS)
  17.     {
  18.         for(int dwIndex = KeyCnt - 1; dwIndex >= 0; dwIndex--)  //枚举键值
  19.         {
  20.             char sKeyName[256] = {0}, pSubKeyTemp[256] = {0};
  21.             RegEnumKey(hKey, dwIndex, sKeyName, sizeof(sKeyName));
  22.             HKEY hKeySub;
  23.             DWORD KeyCntSub;
  24.             strcpy(pSubKeyTemp, pSubKey);
  25.             strcat(pSubKeyTemp, "\");
  26.             strcat(pSubKeyTemp, sKeyName);
  27.             nRet = RegOpenKeyEx(RootKey,pSubKeyTemp,0,KEY_ALL_ACCESS,&hKeySub);
  28.             if(nRet == ERROR_SUCCESS)
  29.             {
  30.                 nRet = RegQueryInfoKey(hKeySub,NULL,NULL,NULL,&KeyCntSub,&KeyMaxLen,NULL,&NameCnt,&NameMaxLen,&MaxDateLen,NULL,NULL);
  31.                 if(nRet == ERROR_SUCCESS)
  32.                 {
  33.                     if (KeyCntSub != 0)
  34.                     {
  35.                         RegDeleteKeys(RootKey, pSubKeyTemp,0);
  36.                     }
  37.                     RegCloseKey(hKeySub);
  38.                 }
  39.             }
  40.             RegDeleteKey(RootKey ,pSubKeyTemp);
  41.         }
  42.         RegCloseKey(hKey);
  43.     }
  44.     RegDeleteKey(RootKey ,pSubKey);
  45.     return 0;
  46. }
复制代码
测试:
  1. RegDeleteKeys(HKEY_LOCAL_MACHINE,"SYSTEM\\ControlSet001\\services\\WIN64AST",1);
  2. RegDeleteKeys(HKEY_LOCAL_MACHINE,"SYSTEM\\ControlSet002\\services\\WIN64AST",1);
复制代码

本帖被以下淘专辑推荐:

回复

使用道具 举报

1111

主题

1651

回帖

7万

积分

用户组: 管理员

一只技术宅

UID
1
精华
244
威望
743 点
宅币
24235 个
贡献
46222 次
宅之契约
0 份
在线时间
2296 小时
注册时间
2014-1-26
发表于 2019-7-30 18:56:00 | 显示全部楼层
这是递归方式实现的。

不过,在删到一半的时候,如果有别的进程打开了注册表往你要删除的注册表项里创建了新的项,会出错吗?
回复 赞! 靠!

使用道具 举报

9

主题

176

回帖

1万

积分

用户组: 真·技术宅

UID
4293
精华
6
威望
441 点
宅币
8670 个
贡献
850 次
宅之契约
0 份
在线时间
338 小时
注册时间
2018-9-19
发表于 2019-8-28 09:46:08 | 显示全部楼层
Vista之后有个新API函数 RegDeleteTree 就自带删除带子级的Key。Win2000和XP还有个 SHDeleteKey 函数也能实现类似功能,只不过这个需要单独链接 shlwapi 库。
回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-20 05:11 , Processed in 0.038815 second(s), 36 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表