1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Looping through the windows registry using RegEnumKeyEx till the leaves

Discussion in 'C' started by rag84dec, Nov 15, 2009.

  1. rag84dec

    rag84dec New Member

    Joined:
    Jul 17, 2007
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    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. 
    
        }
    }
    
     

Share This Page