?
Solved

Working sample to enum all the values in a specified directory

Posted on 2003-03-26
8
Medium Priority
?
380 Views
Last Modified: 2010-04-01
Hi,

I need to call a function which enums all the values in a reg key (RegEnumValue) and fills an array of LPCSTR's or something similar.

thanks,


gegege
0
Comment
Question by:gegege
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 8210329
The following will add all string entries to a list, along with the names of the entries:

#include <list>
#include <string>
#using namespace std;

struct KeyEntry
{
    string strName;
    string strValue;
};

BOOL EnumKeyStringEntries   ( LPCSTR        pszKey, list<KeyEntry>& lstEntries)
{

    HKEY        hKey;
    long        lnRes;

    char        acName  [   MAX_PATH];
    DWORD       dwName  =   MAX_PATH;

    char        acValue [   MAX_PATH];
    DWORD       dwValue =   MAX_PATH;

    DWORD       dwType  =   REG_SZ;

    int         nIdx    =   0;

    if  (   ERROR_SUCCESS   !=  RegOpenKeyEx    (   HKEY_LOCAL_MACHINE,
                                                    pszKey,
                                                    0,
                                                    KEY_READ,
                                                    &hKey
                                                )
        )   return  (   FALSE);


    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;
                }  

            if  (   REG_SZ  != dwType)  contine; // only pocess strings

            KeyEntry ke;

            ke.strName  = acName;
            ke.strValue = acValue;

            lstEntries.push_back
           
            nIdx++;
        }

    RegCloseKey (   hKey);

    return TRUE;
}
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 8211079
Hi gegege,

in addition to jkr's sample I'd suggest to use RegQueryInfoKey first ...
with this you can find the number of values and the
maximum length for value names and values.

ZOPPO
0
 
LVL 1

Author Comment

by:gegege
ID: 8217251
Hi,

I put this in a cpp file on it's own (in an mfc app) and I get the error

fatal error C1010: unexpected end of file while looking for precompiled header directive

when i compile it.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 31

Expert Comment

by:Zoppo
ID: 8217451
Maybe you forgot a
#include <stdafx.h>
as first statement in the .cpp file
This leads to trouble due to precompiled headers coz
the compiler doesn't parse anything before he found
the header used for precompiled headers.
0
 
LVL 86

Expert Comment

by:jkr
ID: 8217684
>>Maybe you forgot a
>>#include <stdafx.h>

Yup, that's most likely to be the reason...
0
 
LVL 1

Author Comment

by:gegege
ID: 8238695
that got passed that error, but now I get:

 warning C4551: function call missing argument list

on this line:

          lstEntries.push_back


thanks for your help so far, gegege
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 8238715
Sorry, that should have read

        lstEntries.push_back ( ke);
 
0
 
LVL 1

Author Comment

by:gegege
ID: 8239905
yep that works, cheers.

there was one thing i needed to change:

 if  (   REG_SZ  != dwType) // continue; // only pocess strings
             {
                    break;
             }

otherwsise it gets caught in a loop when it found a value that wasn't a string.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

801 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