Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

Working sample to enum all the values in a specified directory

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
gegege
Asked:
gegege
  • 3
  • 3
  • 2
1 Solution
 
jkrCommented:
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
 
ZoppoCommented:
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
 
gegegeAuthor Commented:
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.

 
ZoppoCommented:
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
 
jkrCommented:
>>Maybe you forgot a
>>#include <stdafx.h>

Yup, that's most likely to be the reason...
0
 
gegegeAuthor Commented:
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
 
jkrCommented:
Sorry, that should have read

        lstEntries.push_back ( ke);
 
0
 
gegegeAuthor Commented:
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

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.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now