Solved

how to read all REG_SZ values and their data from a registry key (RegOpenKeyEx/ect?)

Posted on 2004-08-18
8
993 Views
Last Modified: 2013-11-20
I'm looking for an example of how to use RegOpenKeyEx,/ect to open the registry key HKLM\SYSTEM\CurrentControlSet\Control\hivelist and read / display all the values and their data.

I've been able to open the key but not read through all of the values to find what I am looking for...


Thanks!
0
Comment
Question by:jploettner
  • 2
  • 2
  • 2
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 11835661
You'd use 'RegEnumValue()' - see e.g. the sample at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/enumerating_registry_subkeys.asp ("Enumerating Registry Subkeys"). E.g.

    char        acName  [   MAX_PATH];
    DWORD       dwName  =   MAX_PATH;

    char        acValue [   MAX_PATH];
    DWORD       dwValue =   MAX_PATH;

    DWORD       dwType  =   REG_SZ;

    int         nIdx    =   0;

    for (   ;;)
        {
            lnRes   =   RegEnumValue    (   hKey,
                                            nIdx,
                                            acName,
                                            &dwName,
                                            0,
                                            &dwType,
                                            ( LPBYTE) acValue,
                                            &dwValue
                                        );

            if  (   ERROR_NO_MORE_ITEMS ==  lnRes)
                {  
                    break;
                }

            if  (   ERROR_SUCCESS   !=  lnRes   &&  ERROR_MORE_DATA !=  lnRes)
                {
                    break;
                }
        }

0
 

Author Comment

by:jploettner
ID: 11836738
I have it sort of working...  There are 12 values under that registry key on my system but it seems i only get first few then then it repeats the last for the remaining values till end (ie: does 4 ok then 8 same as last 4)..

Can you please try this on your system and see what I may be doing wrong?  

Thanks!

      HKEY hKey;
    int      nIdx = 0;
    long lnRes;
      char acName[MAX_PATH];
    char acValue[MAX_PATH];
    DWORD dwName = MAX_PATH + 1;
    DWORD dwValue = MAX_PATH + 1;
    DWORD dwType = REG_SZ;
      CString strSomething;
      
      if (ERROR_SUCCESS == RegOpenKeyEx( HKEY_LOCAL_MACHINE,
            "SYSTEM\\CurrentControlSet\\Control\\hivelist",
            0, KEY_ALL_ACCESS, &hKey))
      {            
            lnRes = RegEnumValue(hKey, nIdx++, acName,
                         &dwName, 0, &dwType, (LPBYTE)acValue,
                 &dwValue);

            while (lnRes != ERROR_NO_MORE_ITEMS)
            {
                  strSomething.Format("name: %s value: %s", acName, acValue);
                  AfxMessageBox(strSomething);

                  lnRes = RegEnumValue(hKey, nIdx++, acName,
                               &dwName, 0, &dwType, (LPBYTE)acValue,
                               &dwValue);

                  LPVOID lpMsgBuf;
                  DWORD dw = GetLastError();

                  if (dw != ERROR_SUCCESS)
                  {
                        FormatMessage(
                              FORMAT_MESSAGE_ALLOCATE_BUFFER |
                              FORMAT_MESSAGE_FROM_SYSTEM,
                              NULL,
                              dw,
                              MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                              (LPTSTR) &lpMsgBuf,
                              0, NULL );

                        strSomething.Format("%s", lpMsgBuf);
                        MessageBox(strSomething);
                  }

                  LocalFree(lpMsgBuf);
        }
      }
      else
            AfxMessageBox("RegOpenKeyEx() failed");
0
 
LVL 5

Expert Comment

by:Nass89
ID: 11838602
Hi,
I tested your codes under Win98. It read all keys, but with a problem. The string acName after each call became one character shorter.
for example the first call is ok:
acName="New Value #1"
after second call is "New Value #" and after next is "New Value " but the values are OK.

I added the following lines to your codes and it iread all keys and strings correctly.

...
...
          lnRes = RegEnumValue(hKey, nIdx++, acName,
                     &dwName, 0, &dwType, (LPBYTE)acValue,
                 &dwValue);

          while (lnRes != ERROR_NO_MORE_ITEMS)
          {
//These are added here
                     dwName = MAX_PATH + 1;
                     dwValue = MAX_PATH + 1;
               strSomething.Format("name: %s value: %s", acName, acValue);
               AfxMessageBox(strSomething);

...
...

Hope it helps.

Good Luck!
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 86

Expert Comment

by:jkr
ID: 11840941
Sorry, but wasn't it me who answered your Q? *puzzled*
0
 

Author Comment

by:jploettner
ID: 11841020
yea i clicked on the wrong one, I'll see how to fix that :)
0
 
LVL 5

Expert Comment

by:Nass89
ID: 11862363
Hi,
I tested your codes and corrected it to operate properly...!!!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.

840 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