hi all. I am trying to enumerate all of the subkeys of a key and then all of the subkeys of each subkey and so on....i dont know if u get my point.For example : enumerate HKEY_CURRENT_USER: AppEvents,Console,Control Panel.....and then enumerate all of subkeys of AppEvents,COnsole....an then every subkey of AppEvents,and so on: here is my code:

void Registry::enum_key(HKEY Hkey , DWORD level)
{
	int i = 0;
	while(RegEnumKeyEx(Hkey , i , name , &size , 0 , 0 , 0 , &filetime)  != ERROR_NO_MORE_ITEMS)
	{
		cout << i << ":" << name << endl;
		i++;
		size = sizeof(name);
		HKEY subhkey;
		if(open_key(hkey , name))
		{
			enum_key(subhkey , level + 1);
			RegCloseKey(subhkey);
		}
	}
}

but what he does is enumerate twice the subkeys of the HKEY_CURRENT_USER.

Line #12, subhkey would be used uninitialized at that point, even if your class declares a member: HKEY subhkey .

Then, are you sure you posted the actual code, I think, open_key(hkey should rather read open_key(hKey ?

>> but what he does is enumerate twice the subkeys of the HKEY_CURRENT_USER
Perhaps post more (actual) code, showing how enum_key() gets called.

HKEY Registry::open_key(HKEY Key , char* subkey)
{
    int ret = RegOpenKeyEx(Key , subkey , 0 ,  KEY_READ  , &hkey);
    if(ret == ERROR_SUCCESS)
    {
        return hkey;
    }

}

void Registry::querry_info_key()
{
    int ret = RegQueryInfoKey(hkey , 0 , 0 , 0 , &sub_keys , 0 , 0 , 0 , 0 , 0 ,  0 , &filetime);
    if(ret == ERROR_SUCCESS)
    {
        if(sub_keys)
        {
            cout << "There are " << sub_keys << " keys" << endl;
        }
    }
}

void Registry::enum_key(HKEY Hkey , DWORD level)
{
    int i = 0;
    while(RegEnumKeyEx(Hkey , i , name , &size , 0 , 0 , 0 , &filetime)  != ERROR_NO_MORE_ITEMS)
    {
        cout << i << ":" << name << endl;
        i++;
        size = sizeof(name);
        HKEY subhkey;
        if(open_key(hkey , name))
        {
            enum_key(subhkey , level + 1);
            RegCloseKey(subhkey);
        }
    }
}

void Registry::run()
{
    HKEY keys[] = { HKEY_CLASSES_ROOT , HKEY_CURRENT_USER , HKEY_LOCAL_MACHINE , HKEY_USERS , HKEY_CURRENT_CONFIG };

        open_key(keys[1] , 0);
        querry_info_key();
        enum_key(hkey , 0);
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Please use code tags.

HKEY subhkey;
// You are using the object's handle here,
// you should use the passed-in hKey instead
if(open_key(hkey , name))
{
  // subhkey is used uninitialized here
  enum_key(subhkey , level + 1);

open_key() is lacking wrt. returning a value as supposed - you must return a value in all cases.

You might add the following

bool open_key_ex(HKEY hkey, const char * key, HKEY & hkey_out)
{
    return RegOpenKeyEx(hkey, key, 0, KEY_READ, &hkey_out)
    == ERROR_SUCCESS ;
}

And then ..

void Registry::enum_key(HKEY Hkey , DWORD level)
{
  int i = 0;
  while(RegEnumKeyEx(Hkey , i , name , &size , 0 , 0 , 0 , &filetime) != ERROR_NO_MORE_ITEMS)
  {
    cout << i << ":" << name << endl;
    i++;
    size = sizeof(name);
    HKEY subhkey;

    // Use the Hkey as the parent key ..
    if(open_key_ex(Hkey , name, subhkey))
    //if(open_key(hkey , name))
    {
      enum_key(subhkey , level + 1);
      RegCloseKey(subhkey);
    }
  }
}

And be sure to initialize size = sizeof(name); .

i did what u said but on line 12 when i call open_key_ex() i get an:

undefined reference to `Registry::open_key_ex(HKEY__*, char const*, HKEY__*&)'

Hmm, you should have the following kind of construct ..

class Registry
{
  ...
  bool open_key_ex(HKEY hkey, const char * key, HKEY & hkey_out);
  ...
};

bool Registry::open_key_ex(HKEY hkey, const char * key, HKEY & hkey_out)
{
    return RegOpenKeyEx(hkey, key, 0, KEY_READ, &hkey_out)
    == ERROR_SUCCESS ;
}
This article has been dead for over six months. Start a new discussion instead.