Solved

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

Posted on 2004-08-18
8
996 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
[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
  • 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
Industry Leaders: 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!

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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…
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

695 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