| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 915 人关注过本帖
标题:【请教】如何列举注册表分支?
只看楼主 加入收藏
zaroty
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2008-3-28
收藏
 问题点数:0 回复次数:2 
【请教】如何列举注册表分支?
很高兴您能注意到这个帖子,希望您能帮助我解决个这个问题,在这里小弟先谢谢了。

问题如下:

现在我想写一个注册表操作相关的程序,在写的过程中,遇到了一些问题,

我想实现的目标是:将 HKEY_LOCAL_MACHINE 这个项下的所有的分支给列举出来,比如

HKEY_LOCAL_MACHINE 的分支:

HARDWARE
SAM
SECURITY
SOFTWARE
SYSTEM

为了实现这一目的,我去查阅了MSDN中注册表操作相关的函数,其中与列举内容相关的函数有:

RegEnumKeyEx
The RegEnumKeyEx function enumerates subkeys of the specified open registry key. The function retrieves information about one subkey each time it is called. Unlike the RegEnumKey function, RegEnumKeyEx retrieves the class name of the subkey and the time it was last modified.

RegEnumValue
The RegEnumValue function enumerates the values for the specified open registry key. The function copies one indexed value name and data block for the key each time it is called.

这两个函数,一个是列举子键,一个是列举键值,没有列举分支的功能,在MSDN中也没有找到相应的列举分支的函数。

在提问之前,我分析了微软自带的regedit.exe中所使用的函数,里面也没有发现什么特殊的内容。

现在请教大家,如何列举注册表的分支,应该用哪个函数?或者是用哪种方法呢?
搜索更多相关主题的帖子: function 注册表 分支 The RegEnumKeyEx 
2008-08-07 14:02
zaroty
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2008-3-28
收藏
得分:0 
没有人回个话么。。。???大哥大姐们、、

http://hi.baidu.com/zaroty  偶滴博客
2008-08-07 16:31
zaroty
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2008-3-28
收藏
得分:0 
程序代码:
// QueryKey - Enumerates the subkeys of key and its associated values.
//     hKey - Key whose subkeys and values are to be enumerated.

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383

 
void QueryKey(HKEY hKey) 
{ 
    TCHAR    achKey[MAX_KEY_LENGTH];   // buffer for subkey name
    DWORD    cbName;                   // size of name string 
    TCHAR    achClass[MAX_PATH] = TEXT("");  // buffer for class name 
    DWORD    cchClassName = MAX_PATH;  // size of class string 
    DWORD    cSubKeys=0;               // number of subkeys 
    DWORD    cbMaxSubKey;              // longest subkey size 
    DWORD    cchMaxClass;              // longest class string 
    DWORD    cValues=0;              // number of values for key 
    DWORD    cchMaxValue;          // longest value name 
    DWORD    cbMaxValueData;       // longest value data 
    DWORD    cbSecurityDescriptor; // size of security descriptor 
    FILETIME ftLastWriteTime;      // last write time 

 
    DWORD i, retCode; 

 
    TCHAR  achValue[MAX_VALUE_NAME]; 
    DWORD cchValue = MAX_VALUE_NAME; 

 
    // Get the class name and the value count. 
    retCode = RegQueryInfoKey(
        hKey,                    // key handle 
        achClass,                // buffer for class name 
        &cchClassName,           // size of class string 
        NULL,                    // reserved 
        &cSubKeys,               // number of subkeys 
        &cbMaxSubKey,            // longest subkey size 
        &cchMaxClass,            // longest class string 
        &cValues,                // number of values for this key 
        &cchMaxValue,            // longest value name 
        &cbMaxValueData,         // longest value data 
        &cbSecurityDescriptor,   // security descriptor 
        &ftLastWriteTime);       // last write time 

 
    // Enumerate the subkeys, until RegEnumKeyEx fails.
    
    if (cSubKeys)
    {
        printf( "\nNumber of subkeys: %d\n", cSubKeys);

        for (i=0; i<cSubKeys; i++) 
        { 
            cbName = MAX_KEY_LENGTH;
            retCode = RegEnumKeyEx(hKey, i,
                     achKey, 
                     &cbName, 
                     NULL, 
                     NULL, 
                     NULL, 
                     &ftLastWriteTime); 
            if (retCode == ERROR_SUCCESS) 
            {
                _tprintf(TEXT("(%d) %s\n"), i+1, achKey);
            }
        }
    } 

 
    // Enumerate the key values. 

    if (cValues) 
    {
        printf( "\nNumber of values: %d\n", cValues);

        for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++) 
        { 
            cchValue = MAX_VALUE_NAME; 
            achValue[0] = '\0'; 
            retCode = RegEnumValue(hKey, i, 
                achValue, 
                &cchValue, 
                NULL, 
                NULL,
                NULL,
                NULL);

 
            if (retCode == ERROR_SUCCESS ) 
            { 
                _tprintf(TEXT("(%d) %s\n"), i+1, achValue); 
            } 
        }
    }
}

void _tmain(void)
{
   HKEY hTestKey;

   if( RegOpenKeyEx( HKEY_CURRENT_USER,
        TEXT("SOFTWARE\\Microsoft\\Developer"),
        0,
        KEY_READ,
        &hTestKey) == ERROR_SUCCESS
      )
   {
      QueryKey(hTestKey);
   }

   RegCloseKey(hTestKey);
}

http://hi.baidu.com/zaroty  偶滴博客
2008-08-07 16:55
快速回复:【请教】如何列举注册表分支?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015505 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved