在 c++ 中递归搜索注册表



我的搜索注册表功能有问题。我的目标是输入键名并遍历注册表树,直到找到具有此名称的键。我的函数可以遍历整棵树(深度优先(,但问题在于搜索 - 我只能在"第一级"成功搜索键。但是,如果我尝试搜索,例如,HKEY_CURRENT_USER\AppEvents\Schemes,"Schemes"键将被跳过。对于搜索,我使用找到的标志,想法是在设置为 false 时继续搜索。但是,如果(!found({//function code },或者(!found(似乎对我有用。我错过了什么以及如何解决?

我的代码是:

#define BUFF_SIZE 400
void searchKeys(HKEY, string, TCHAR*, bool);
int main()
{
    HKEY rootKey= HKEY_CLASSES_ROOT;
    int keyMenu;
    TCHAR searchedName[BUFF_SIZE];
    bool found = false;

    cout << "Select root key: " << endl;
    cout << "1 - HKEY_CLASSES_ROOTn2 - HKEY_CURRENT_USERn3 - HKEY_LOCAL_MACHINEn4 - HKEY_USERSn5 - HKEY_CURRENT_CONFIG"<<endl;
    cin >> keyMenu;
    switch (keyMenu) {
    case 1: rootKey = HKEY_CLASSES_ROOT;
        break;
    case 2: rootKey = HKEY_CURRENT_USER;
        break;
    case 3: rootKey = HKEY_LOCAL_MACHINE;
        break;
    case 4: rootKey = HKEY_USERS;
        break;
    case 5: rootKey = HKEY_CURRENT_CONFIG;
        break;
    default: 
        cout << "This root key does not exist" << endl;
        system("PAUSE");
        return 0;
    }

    cout << "Enter key name to search: " << endl;
    cin >> searchedName;
    cout << "n";
    string subKeyPath = "";
    searchKeys(rootKey, subKeyPath, searchedName, found);
    system("PAUSE");
    return 0;
}
void searchKeys(HKEY rootKey, string subKeyPath, TCHAR* searchedName, bool found) {
    HKEY subKey;
    DWORD subKeyCount, buffSize;
    char subKeyBuff[BUFF_SIZE];
    char result[BUFF_SIZE];
    TCHAR sbNameBuf[BUFF_SIZE];
    const char * subKeyName;
    subKeyName = subKeyPath.c_str();
    copy(subKeyPath.begin(), subKeyPath.end(), sbNameBuf);
    //if (!found) {
    //while (!found) {
        DWORD output = RegOpenKeyEx(rootKey, subKeyName, NULL, KEY_ALL_ACCESS, &subKey); //open specified root catalogue
        if (output != ERROR_SUCCESS) return;
        RegQueryInfoKey(subKey, NULL, NULL, NULL, &subKeyCount, NULL, NULL, NULL, NULL, NULL, NULL, NULL); //get info about root key
        if (!subKeyCount) return;
        for (DWORD i = 0; i < subKeyCount && !found; i++) {
            buffSize = sizeof(subKeyBuff);
            RegEnumKeyEx(subKey, i, subKeyBuff, &buffSize, NULL, NULL, NULL, NULL);
            string keyName = subKeyBuff;
            if (strcmp(subKeyBuff, searchedName) == 0) {
                found = true;
            }
            else {
                cout << subKeyBuff << endl;
            }
            keyName = subKeyPath + subKeyBuff + "\";
            RegOpenKeyEx(rootKey, subKeyName, NULL, KEY_ALL_ACCESS, &subKey);
            RegQueryInfoKey(subKey, NULL, NULL, NULL, &subKeyCount, NULL, NULL, NULL, NULL, NULL, NULL, NULL); //get subKeyCount
            if (subKeyCount) {
                searchKeys(rootKey, keyName, searchedName, found);
            }
        }
    //}
    //}
}

这个问题的解决方案只是让布尔值找到全局变量。此外,engf-010 注释对于查看所有键很有用。

最新更新