简体   繁体   中英

Help with enum values in registry c++

 DWORD type = REG_NONE;
 int i = 0;
 size = sizeof(ValueName);
 size2 = sizeof(ValueData);
 BOOL bContinue = TRUE;

 do
 {
  lRet = RegEnumValue(Hkey , i , ValueName , &size , 0 , &type , ValueData , &size2);
  switch(lRet)
  {
  case ERROR_SUCCESS:
   print_values(ValueName , type , ValueData , size2);
   i++;
   size = sizeof(ValueName);
   size2 = sizeof(ValueData);
   break;
  case ERROR_MORE_DATA:
   size2 = sizeof(ValueData);
   if(NULL != ValueData) delete [] ValueData;
   ValueData = new BYTE[size2];
   break;
  case ERROR_NO_MORE_ITEMS:
   bContinue = false;
   break;
  default:
   cout << "Unexpected error: " << GetLastError() << endl;
   bContinue = false;
   break;
  }
 }while(bContinue);

it always goes to ERROR_NO_MORE_DATA ,why is that ? :-/

EDIT:

sorry,i ment : ERROR_MORE_DATA:) i changed something,still doesnt work

class Registry {
public:
    Registry();
    ~Registry();
    bool open_key_ex(HKEY hkey, const char * key, HKEY & hkey_out);
    void querry_info_key(HKEY);
    void enum_key(HKEY , DWORD);
    void enum_value(HKEY);
    void print_values(LPCSTR , DWORD , LPBYTE , DWORD);
    void run();
private:
    HKEY hkey;
    long lRet;
    FILETIME filetime;
    DWORD sub_keys;
    TCHAR* ValueName;
    DWORD size;
    LPBYTE ValueData;
    DWORD size2;
    HKEY a;
    DWORD MaxValueLen;
    DWORD MaxDataLen;

};

Registry::Registry()
{
    ValueName = new TCHAR[MAX_PATH];
    ValueData = NULL;
    MaxValueLen = MAX_PATH + 1;
    MaxDataLen = 0;
}

Registry::~Registry()
{
    delete [] ValueName;
}

void Registry::enum_value(HKEY Hkey)
{
    DWORD type = REG_NONE;
    int i = 0;
    size2 = MaxDataLen;
    BOOL bContinue = TRUE;

    do
    {
        lRet = RegEnumValue(Hkey , i , ValueName , &size , 0 , &type , ValueData , &size2);
        size = MaxValueLen;
        switch(lRet)
        {
        case ERROR_SUCCESS:
            print_values(ValueName , type , ValueData , size2);
            size2 = MaxDataLen;
            i++;
            break;
        case ERROR_MORE_DATA:
            MaxDataLen = size2;
            if(NULL != ValueData) delete [] ValueData;
            ValueData = new BYTE[MaxDataLen];
            break;
        case ERROR_NO_MORE_ITEMS:
            bContinue = false;
            break;
        default:
            cout << "Unexpected error: " << GetLastError() << endl;
            bContinue = false;
            break;
        }
    }while(bContinue);
}

I suspect it's related to this:

size2 = sizeof(ValueData);

Although I don't see a declaration for ValueData , it's apparent that it is a pointer, not an array. Therefore, sizeof will give you the size of a pointer rather than the size of the buffer it points to. You'll need to keep track of the size yourself. The same might be true of ValueName , but I can't tell.

From http://msdn.microsoft.com/en-us/library/ms724865%28VS.85%29.aspx :

If the buffer specified by lpData is not large enough to hold the data, the function returns ERROR_MORE_DATA and stores the required buffer size in the variable pointed to by lpcbData. In this case, the contents of lpData are undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM