Solved

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

Posted on 2004-08-18
8
986 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Importing  SSL Certificate into Firefox using GPO 28 202
xyBalance chalenge 58 88
Creating Labels and Frequency list style reports in SAS code 9 92
haveThree challenge 22 112
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…
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.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

15 Experts available now in Live!

Get 1:1 Help Now