Solved

Enumerate through an hkey's StringValue and Name.

Posted on 2000-03-27
4
364 Views
Last Modified: 2013-11-20
Knowing that i already have a key at
HKEY_LOCAL_MACHINE
   -> SOFTWARE
         -> MYSOFTWARENAME
                  ->MYSETTINGS
                           -Setting1 = "1234567"
                           -Setting2 = "aaaaaaa"


how do i enumurate thorugh MYSETTINGS to get all the available setting information. Since I do not know how many Settings they are in MySettings and I do not know what the Key and also the value is?

it could be Setting1, it could be SettingAAA etc.


Some code samples would help, ive tried playing around with RegEnumKey RegEnumValue and RegQueryalue and its associated functions but I can't seem to get it to work... it wont even compile right becoz i dun really know how to pass in some of its function parameters... im still pretty new in VC++ so workable code samples would really really help...

ive looked thorugh code guru and most of the samples are for extracting values where u already know the HKEY to look for... and in my case i do not..

so lotsa help needed... regards

lamer,
0
Comment
Question by:lamer
  • 3
4 Comments
 
LVL 6

Accepted Solution

by:
snoegler earned 100 total points
ID: 2661779
Maybe this little code helps you.
If not, feel free to reject :)

// This little piece of code is a Win32-Console-App
// Usage:
//   dumpregkeys HKEY_LOCAL_MACHINE\\Software\\Windows

#include "stdafx.h"

struct {
      char      *szHive;
      HKEY      hkeyHive;
}      hive_info[]=
{
      {"HKEY_LOCAL_MACHINE",HKEY_LOCAL_MACHINE},
      {"HKEY_CLASSES_ROOT",HKEY_CLASSES_ROOT},
      {"HKEY_CURRENT_USER",HKEY_CURRENT_USER},
      {"HKEY_USERS",HKEY_USERS},
      {"HKLM",HKEY_LOCAL_MACHINE},
      {"HKCR",HKEY_CLASSES_ROOT},
      {"HKCU",HKEY_CURRENT_USER},
      {"HKU",HKEY_USERS},
      {NULL,NULL}
};

bool      parseArgs(int argc, char* argv[], HKEY& hkeySearch)
{
      if(argc<=1) {
            puts("Usage:");
            puts("dumpregkeys [reg-path]\n");
            puts("Example:\ndumpregkeys HKLM\\Enum\\BIOS");
            return false;
      }

      // Parse arguments
      char      *pszSlash=strchr(argv[1],'\\');
      char      *pszHive;

      if(pszSlash==NULL) {
            puts("Error - Please give a full registry path.");
            return false;
      }

      // Extract the first part of the registry path,
      // for example HKEY_LOCAL_MACHINE,
      // and search for the appropriate HKEY -> hkeyMainHive
      HKEY      hkeyMainHive=NULL;
      int            nLen=(pszSlash-argv[1]);
      pszHive      =new char[nLen+1];
      memcpy(pszHive,argv[1],nLen);
      pszHive[nLen]='\0';

      int            i;
      for(i=0;hive_info[i].szHive!=NULL;i++) {
            if(stricmp(hive_info[i].szHive,pszHive)==0) {
                  hkeyMainHive=hive_info[i].hkeyHive;
                  break;
            }
      }
      delete [] pszHive;

      if( hkeyMainHive==NULL ||
            RegOpenKey(hkeyMainHive,pszSlash+1,&hkeySearch)!=ERROR_SUCCESS)
      {
            puts("Error - Please give a valid registry path.");
            return false;
      }

      return true;
}

// This here is the important part
bool dump_hive(HKEY hkeySearch, char* szCurrentPath)
{
      int            nHive;
      int            nLeaf;

      // 1st: Enumerate sub-keys
      for(nHive=0;;nHive++) {
            char      szEntryName[512];
            LONG      res;

            res            =RegEnumKey(hkeySearch,nHive,szEntryName,512);
            if(res!=ERROR_SUCCESS)
                  break;

            printf("HIVE: %s\n",szEntryName);
      }

      // 2nd: Enumerate leaves
      for(nLeaf=0;;nLeaf++) {
            char      szEntryName[512];
            DWORD      szBuffer=512;
            DWORD      dwType;                  // One of REG_BINARY/REG_SZ/...
            LONG      res;

            res            =RegEnumValue(hkeySearch,nLeaf,
                                            szEntryName,&szBuffer,
                                            NULL,
                                            &dwType,
                                            NULL,NULL);
            if(res!=ERROR_SUCCESS)
                  break;

            printf("LEAF: %s\n",szEntryName);
      }

      return true;
}

int main(int argc, char* argv[])
{
      HKEY      hkeySearch;

      // Parse arguments / validity check
      if(!parseArgs(argc,argv,hkeySearch))
            return -1;

      // Give feedback
      printf("Browsing registry path %s\n\n",
               argv[1]);

      // Do the actual enumeration
      dump_hive(hkeySearch,argv[1]);

      // Close the key
      RegCloseKey(hkeySearch);

      return 0;
}

A little explanation:

RegEnumKey(
  HKEY hKey,
  DWORD dwIndex,
  LPTSTR lpName,
  DWORD cbName)

Give the origin of the enumeration in 'hKey', then iterate from zero upwards in 'dwIndex' (the iteration ends with RegEnumKey returning !=ERROR_SUCCESS).
lpName receives the name of the hive, and cbName tells RegEnumKey how many bytes(!) lpName can accept.

RegEnumValue(
  HKEY hKey,
  DWORD dwINdex,
  LPTSTR lpName,
  LPDWORD lpcbValueName,
  LPDWORD reserved, // ==NULL
  LPDWORD lpType,
  LPDWORD lpData,
  LPDWORD lpcbData
}

This is pretty similar to RegEnumKey, but:
lpcbValueName must point to a DWORD containing how many bytes 'lpName' can accept, and receives after the function call the number of bytes which actually have been copied to 'lpName'. Make sure to initialize it right.
'reserved' must be NULL,
'lpType' receives the type of the value
(for example REG_SZ, REG_BINARY, ...),
'lpData' can be NULL (then 'lpcbData' must be NULL also),
or it can point to a pre-allocated buffer which will receive the contents of the value. In this case, 'lpcbData' must contain the number of bytes you allocated for 'lpData', and will contain the number of bytes which have been copied afterwards.

snoegler
0
 

Author Comment

by:lamer
ID: 2662526
What u did missed something..
I still do now know how to get the Value of the the Leafs..

by using RegQueryValueEx I managed to return the default value for the 1st leaf.

But not for my other leafs. Although i can enumrate through the list of leafs and return its name.

Are u telling me that the only option is to ReOpen the Registry with a new Key which is the leaf as the subkey and call RegQueryKeyEx until all the leafs are enumerated?

thanx
0
 

Author Comment

by:lamer
ID: 2662527
What u did missed something..
I still do now know how to get the Value of the the Leafs..

by using RegQueryValueEx I managed to return the default value for the 1st leaf.

But not for my other leafs. Although i can enumrate through the list of leafs and return its name.

Are u telling me that the only option is to ReOpen the Registry with a new Key which is the leaf as the subkey and call RegQueryKeyEx until all the leafs are enumerated?

thanx
0
 

Author Comment

by:lamer
ID: 2662624
Nvermind.. solved it...
apparently u need to call RegQueryValEx
twice!!!

1) to get the size and the type
2) to get the Value..

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question