Solved

Enumerate through an hkey's StringValue and Name.

Posted on 2000-03-27
4
362 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now