Solved

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

Posted on 2004-08-18
8
985 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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