Solved

Enumerate through an hkey's StringValue and Name.

Posted on 2000-03-27
4
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sumDigits challenge 9 180
while loop over for loop 7 119
Problem to event 3 123
Bartender label printing - switch on and off graphics 3 95
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

710 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