简体   繁体   English

帮助注册表C ++中的枚举值

[英]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 ? 它总是转到ERROR_NO_MORE_DATA,为什么? :-/ :-/

EDIT: 编辑:

sorry,i ment : ERROR_MORE_DATA:) i changed something,still doesnt work 抱歉,ment:ERROR_MORE_DATA :)我更改了一些内容,仍然无法正常工作

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. 尽管我看不到ValueData的声明,但很显然它是一个指针,而不是数组。 Therefore, sizeof will give you the size of a pointer rather than the size of the buffer it points to. 因此, sizeof将为您提供指针的大小,而不是指针所指向的缓冲区的大小。 You'll need to keep track of the size yourself. 您需要自己跟踪尺寸。 The same might be true of ValueName , but I can't tell. ValueName可能也是如此,但我无法确定。

From http://msdn.microsoft.com/en-us/library/ms724865%28VS.85%29.aspx : http://msdn.microsoft.com/zh-cn/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. 如果lpData指定的缓冲区不足以容纳数据,则该函数返回ERROR_MORE_DATA并将所需的缓冲区大小存储在lpcbData指向的变量中。 In this case, the contents of lpData are undefined. 在这种情况下,lpData的内容是不确定的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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