Thread: Looping through the windows registry using RegEnumKeyEx till the leaves

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    Looping through the windows registry using RegEnumKeyEx till the leaves

    Hi all,

    I am trying to write code to loop through the windows registry till i find the leaf , meaning the
    key value pair exists . Each time i get the key , i want to know whether it is parent of some key-value pair.
    If yes , loop through its children till the point where a key comes which has no child. I tried writing the program on my own
    using RegEnumKeyEx , The key structure is given below.

    If you type "regedit", you get the following template...

    Code:
    	System
    	   Select
    		Current - 2
    		Default - "Key"
    		Known	- "Key2"
    		LastError - "Key"
    	   Select1
    		Current - 2
    		Default - "Key"
    		Known	- "Key2"
    		LastError - "Key"
    	   Select2
    		Current - 2
    		Default - "Key"
    		Known	- "Key2"
    		LastError - "Key"
    In the above tree , i want to loop through subkeys of "System" like Select , Select1 and Select2
    and loop through the key value pair as given above.I have written the code as below for retrieving
    subkeys.

    Code:
    void QueryKey(HKEY hKey) 
    { 
        TCHAR    achKey[500];   // 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);
    		if(cSubKeys!=0)
            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);
    
                }
    		//Here write a recursive function to loop through the subkeys
            }
        
    	} 
    	else
    	{
    		printf("This is a child\n");
    	}
     
        // Enumerate the key values. 
    
        }
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What problem are you having?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows 2000/XP, network and registry
    By kes103 in forum Windows Programming
    Replies: 3
    Last Post: 05-25-2002, 08:27 AM
  2. Windows Registry
    By Nor in forum Windows Programming
    Replies: 1
    Last Post: 02-15-2002, 03:19 AM
  3. where windows registry store ?
    By Adisonz in forum C Programming
    Replies: 7
    Last Post: 02-05-2002, 10:41 PM
  4. How to use Cprogramming to assign windows registry value ?
    By Adisonz in forum Windows Programming
    Replies: 6
    Last Post: 01-31-2002, 06:49 PM
  5. Editing Windows Registry in C?
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 01-11-2002, 09:42 PM