?
Solved

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

Posted on 2004-08-18
8
Medium Priority
?
998 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 1000 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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
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

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!

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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

771 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