首页
社区
课程
招聘
[原创][菜鸟奉献]Delphi使用WindowsAPI枚举递归枚举注册表
发表于: 2009-1-13 23:04 7080

[原创][菜鸟奉献]Delphi使用WindowsAPI枚举递归枚举注册表

2009-1-13 23:04
7080
首先声明:本人是DELPHI初学者。
最近需要一个枚举注册表的函数,而且还是需要DELPHI来写。在网络搜索很久,都没找到。
也许是我的搜索关键字不对。
这下我只能利用VC的代码转换为DELPHI 。
测试已经通过,很稳定

使用方式:如果要枚举HKEY_CURRENT_USER
测试代码:
Reg_RecursionEnum(HKEY_CURRENT_USER, '','',
                   mpr_Form_RegFindInvalidFilePath.ST_ScanRegPath) ;

{************************************************************************/
/*函数名:	Reg_RecursionEnum(hkey_param_Root:HKEY;
                            const str_param_SubKey:string;
                            const str_param_FullKeyPath:string;
                            var SText_param_ScanRegPath:TStaticText):Boolean;

/*功能:	  递归注册表

/*参数:   1> 注册表根项
          2> 子键项名
          3> 递归时所有子键项名的总和[全注册表路径]
          4> 在界面上显示的路径[跟GUI交互]

/*作者:   ZTS

/*返回值:	True  递归成功
          False 递归失败

/************************************************************************}
function Reg_RecursionEnum(hkey_param_Root:HKEY;
                           const str_param_SubKey:string;
                           const str_param_FullKeyPath:string;
                           var SText_param_ScanRegPath:TStaticText):Boolean;
var
  int_numSubKey:Integer       ;
  int_numSubKeyVal:Integer    ;
  dword_sizeSubKey:DWORD      ;
  dword_sizeSubKeyVal:DWORD   ;
  int_SubKeyIndex:Integer     ;
  int_SubKeyValIndex:Integer  ;

  str_QueryVal:string      ;
  dword_QueryValSize:DWORD ;
  dworl_QueryValType:DWORD ;



  char_SubKey:array[0..G_INT_MAX_KEY_LENGTH-1] of Char ;
  char_SubKeyVal:array[0..G_INT_MAX_KEY_LENGTH-1] of Char ;

begin
  Result := True ;

  int_numSubKey := 0 ;
  int_numSubKeyVal := 0 ;

  if RegOpenKeyEx(hkey_param_Root, PChar(str_param_SubKey), 0,
                  KEY_ALL_ACCESS,
                  hkey_param_Root) = ERROR_SUCCESS then
  begin

    // 打印: 子键项名的总和[全注册表路径]
    // SText_param_ScanRegPath.Caption := str_param_FullKeyPath ;

    //*首先查得当前键下的子键项数以及子键项值数目*//
    RegQueryInfoKey(hkey_param_Root, nil, nil, nil,
                    @int_numSubKey, nil, nil, @int_numSubKeyVal, nil,
                    nil, nil, nil) ;
    if int_numSubKey <> 0 then
    begin
      for int_SubKeyIndex := 0 to int_numSubKey-1 do
      begin
        {*子键项值枚举查询*}
        if int_numSubKeyVal <> 0 then
        begin
          for int_SubKeyValIndex := 0 to int_numSubKeyVal-1 do
          begin
            ZeroMemory(@char_SubKeyVal, G_INT_MAX_KEY_LENGTH);
            dword_sizeSubKey := G_INT_MAX_KEY_LENGTH ;

            RegEnumValue(hkey_param_Root, int_SubKeyValIndex, char_SubKeyVal, dword_sizeSubKey,
                         nil, nil, nil, nil) ;

           // 打印: 子键项值名称
           // SText_param_ScanRegPath.Caption := string(char_SubKeyVal) ;

           // 查询类型[REG_SZ, REG_EXPAND_SZ]
           dword_QueryValSize := 0 ;
           dworl_QueryValType := 0 ;
           if RegQueryValueEx(hkey_param_Root, char_SubKeyVal, nil,
                              @dworl_QueryValType, nil, @dword_QueryValSize) = ERROR_SUCCESS then
           begin
             if dworl_QueryValType in [REG_SZ, REG_EXPAND_SZ] then
             begin
                 SetLength(str_QueryVal, dword_QueryValSize) ;
                 if RegQueryValueEx(hkey_param_Root, char_SubKeyVal, nil,
                                    @dworl_QueryValType, PByte(str_QueryVal), @dword_QueryValSize) = ERROR_SUCCESS then
                 begin
                    SetLength(str_QueryVal, StrLen(PChar(str_QueryVal))) ;

                    // 打印: 子键项值内容
                    // SText_param_ScanRegPath.Caption := str_QueryVal ;

                 end;
                 
             end;

           end;

          end;

        end;

        {*子键项枚举查询*}
        ZeroMemory(@char_SubKey, G_INT_MAX_KEY_LENGTH);
        dword_sizeSubKey := G_INT_MAX_KEY_LENGTH ;

        // 开始枚举子键项,枚举出一个子键项则递归调用本函数体
        RegEnumKeyEx(hkey_param_Root, int_SubKeyIndex, char_SubKey, dword_sizeSubKey,
                    nil, nil, nil, nil) ;

        // 进入下一次递归[子键项]
        Reg_RecursionEnum(hkey_param_Root, string(char_SubKey), str_param_FullKeyPath+string(char_SubKey)+'\',
                          SText_param_ScanRegPath) ;

      end;

    end;

    RegCloseKey(hkey_param_Root) ;
    
  end;
  
end; // End Reg_RecursionEnum()

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 8
支持
分享
最新回复 (6)
雪    币: 347
活跃值: (25)
能力值: ( LV9,RANK:420 )
在线值:
发帖
回帖
粉丝
2
沙发是我的

看你写的Delphi感觉像C啊
2009-1-13 23:08
0
雪    币: 6051
活跃值: (1441)
能力值: ( LV15,RANK:1473 )
在线值:
发帖
回帖
粉丝
3
学习一下!省得自己研究了
2009-1-13 23:08
0
雪    币: 301
活跃值: (300)
能力值: ( LV9,RANK:290 )
在线值:
发帖
回帖
粉丝
4
收藏学习之,谢谢楼主的分享
2009-1-13 23:15
0
雪    币: 454
活跃值: (1673)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
5
猪兄有新目标啦?
2009-1-14 00:45
0
雪    币: 204
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
不错,学习下~
严重支持
2009-1-14 01:14
0
雪    币: 63
活跃值: (17)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
7
顶猪兄
2009-1-14 11:32
0
游客
登录 | 注册 方可回帖
返回
//