如何读取给定密钥的所有Windows注册表子键? (在visual c ++中) [英] How to read all Windows Registry SubKeys for a given Key? (in visual c++)

查看:133
本文介绍了如何读取给定密钥的所有Windows注册表子键? (在visual c ++中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何读取给定Key的所有Windows注册表子键? (在visual c ++中)


How to read all Windows Registry SubKeys for a given Key? (in visual c++)

#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;              // 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 __cdecl _tmain(void)
{
   HKEY hTestKey;

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


推荐答案

嗯,你的解决方案可能会结束以递归方式调用QueryKey。因此,在枚举子键的循环中,您可以将其修改为类似

Well, your solution could end up calling QueryKey recursively. So in your loop enumerating the sub keys, you could modify it to something like

for (i=0; i<cSubKeys; i++) 
{
    cbName = MAX_KEY_LENGTH;
    retCode = RegEnumKeyEx(hKey, i,
        achKey, 
        &cbName, 
        NULL, 
        NULL, 
        NULL, 
        &ftLastWriteTime); 
     if (retCode == ERROR_SUCCESS) 
     {
        HKEY subKey;
        retCode = RegOpenKeyEx(hKey, 
            achKey,
            0, KEY_READ,
            &subKey);
        //error checking skipped for illustrative purposes
        QueryKey(subKey);
     }
}

如您所见,它打开子键的句柄,然后在其上调用QueryKey。

As you can see, it opens a handle to the sub key, then calls QueryKey on it.

当你递归调用时要小心的是有一种方法可以停止。为此,它会在用完子键时自然停止,这就是为什么你没有看到任何显式检查来停止递归的原因。但是如果你自己离开并使用
递归,请记住必须有一种方法来阻止它。

The thing to be careful with when you are calling recursively is that there is a way to stop. For this, it will stop naturally when it runs out of child keys, so that is why you don't see any explicit check to stop the recursion. But if you go away and use recursion on your own, remember that there has to be a way to stop it.

这方面的一个例子是

unsigned int factorial(unsigned int value)
{
    if(value <= 1)
    {
        return value * factorial(value - 1);
    }
    return 1;
}

这是计算阶乘的递归方法。如果你把它称为阶乘(5),那么它将返回120.但重要的是检查值的值。如果不存在,它将继续运行直至崩溃。

This is a recursive way of calculating a factorial. If you call it as factorial(5) then it will return 120. But the important part is the check for value's value. If this wasn't there it would keep going until it crashed.


这篇关于如何读取给定密钥的所有Windows注册表子键? (在visual c ++中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆