我的搜索注册表功能有问题。我的目标是输入密钥名称并遍历注册表树,直到找到具有该名称的密钥为止。我的功能可以遍历整棵树(深度优先),但是问题出在搜索上-我只能在“第一级”上成功搜索键。但是,如果我尝试搜索例如HKEY_CURRENT_USER \ AppEvents \ Schemes,则“ Schemes”键将被跳过。对于搜索,我使用found标志,其想法是在将其设置为false时继续搜索。但是,即使(!found){//功能代码},也不会(!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_ROOT\n2 - HKEY_CURRENT_USER\n3 - HKEY_LOCAL_MACHINE\n4 - HKEY_USERS\n5 - 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);
}
}
//}
//}
}
答案 0 :(得分:0)
此问题的解决方案只是使bool发现全局变量。另外,engf-010注释对于查看所有键很有用。