CRegKeyEx GetValuesCount返回无效的数字

时间:2015-07-28 21:22:52

标签: c++ windows registry registrykey

我在C ++ / VS2013 / Win7x64中有这个代码,应用程序是用64位编译的

CString strKey=_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\SharedDLLs");
CRegKeyEx   reg;

// Count entries
if (reg.Open(HKEY_LOCAL_MACHINE, strKey, KEY_ALL_ACCESS) != ERROR_SUCCESS)
        return;

if (reg.QueryInfo() != ERROR_SUCCESS) return;
dwCountEntries = reg.GetValuesCount(); 

这是CRegKeyEx类

ifndef __REGKEYEX_H__INCLUDED__
#define __REGKEYEX_H__INCLUDED__

#include <atlbase.h>

//
// Enhanced version of ATL's CRegKey class
// After you open/attach the key, you should call QueryInfo in order
// to initialize some members which this class use.
//
class CRegKeyEx : public CRegKey
{
public:

    // Retreives information about the key
    LONG  QueryInfo();

    // Number of subkeys
    DWORD GetSubKeysCount() { return m_cSubKeys; }
    // Number of values
    DWORD GetValuesCount()  { return m_cValues;  }

    // Enumerate values of arbitrary type
    // allocate space in ppBuff (you have to free() it)
    LONG EnumValue(DWORD dwIndex, CString& strName, 
                   LPBYTE* ppBuff, DWORD* pBuffSize, DWORD* pType);
    // Enum value as String
    // *pType will be REG_SZ or REG_EXPAND_SZ
    LONG EnumValue(DWORD dwIndex, CString& strName, CString& strValue, DWORD* pType);

    // EnumKey with CString output
    LONG EnumKey(DWORD dwIndex, CString& strName);


protected:
    DWORD m_cSubKeys;
    DWORD m_cMaxSubKeyLen;

    DWORD m_cValues;
    DWORD m_cMaxValueNameLen;
    DWORD m_cMaxValueLen; // this is in bytes (can be any kind of data)
};


// ---- implementation ----

// Retreives information about the key
inline LONG CRegKeyEx::QueryInfo()
{
    LONG ret;
    m_cSubKeys = 0;
    m_cMaxSubKeyLen = 0;
    m_cValues = 0;
    m_cMaxValueNameLen = 0;
    m_cMaxValueLen = 0;
    ret = ::RegQueryInfoKey(m_hKey, 
                NULL, NULL, NULL,
                &m_cSubKeys, &m_cMaxSubKeyLen,
                NULL,
                &m_cValues, &m_cMaxValueNameLen, &m_cMaxValueLen,
                NULL, NULL);

    // include the terminating NULL character(s)
    m_cMaxSubKeyLen++;
    m_cMaxValueNameLen++;

    return ret;
}


// Enumerate values of arbitrary type
// allocate space in ppBuff (you have to free() it)
inline LONG CRegKeyEx::EnumValue(DWORD dwIndex, 
                                 CString& strName, 
                                 LPBYTE* ppBuff, 
                                 DWORD* pBuffSize,
                                 DWORD* pType)
{
    TCHAR  *pName;
    DWORD   cName;
    LPBYTE  pBuff = NULL;
    DWORD   dwBuffSize = 0;
    DWORD   dwType;
    LONG    ret;

    // Allocate space for buffers
    pName = (TCHAR*)malloc(m_cMaxValueNameLen * sizeof(TCHAR));
    cName = m_cMaxValueNameLen;
    if (ppBuff != NULL) // data may not be required
    {
        pBuff = (LPBYTE)malloc(m_cMaxValueLen);
        dwBuffSize = m_cMaxValueLen;
    }

    // Retreive name and data
    ret = ::RegEnumValue(m_hKey, dwIndex, 
                pName, &cName,
                NULL, &dwType,
                pBuff, pBuff ? &dwBuffSize : NULL);

    if (ret != ERROR_SUCCESS)
    {
        free(pName);
        free(pBuff);
        return ret;
    }

    // Resize the buffers
    pName = (TCHAR*)realloc(pName, (cName + 1) * sizeof(TCHAR));
    if (pBuff)
        pBuff = (LPBYTE)realloc(pBuff, dwBuffSize);

    if (ppBuff)
        *ppBuff = pBuff;
    if (pBuffSize)
        *pBuffSize = dwBuffSize;
    if (pType)
        *pType = dwType;

    strName = pName;
    free(pName);

    return ERROR_SUCCESS;
}

// Enum value as String
// *pType will be REG_SZ or REG_EXPAND_SZ
inline LONG CRegKeyEx::EnumValue(DWORD dwIndex, 
                                 CString& strName, 
                                 CString& strValue, 
                                 DWORD* pType)
{
    DWORD   dwType;
    LPBYTE  pBuff;
    LONG    ret;

    ret = EnumValue(dwIndex, strName, &pBuff, NULL, &dwType);
    if (ret != ERROR_SUCCESS)
        return ret;

    if (pType)
        *pType = dwType;

    if (dwType == REG_SZ || dwType == REG_EXPAND_SZ)
    {
        strValue = (TCHAR*)pBuff;
        free(pBuff);
    }
    else
    {
        free(pBuff);
        return ERROR_INVALID_DATATYPE;
    }

    return ERROR_SUCCESS;
}

// EnumKey with CString output
inline LONG CRegKeyEx::EnumKey(DWORD dwIndex, CString& strName)
{
    TCHAR  *pName;
    DWORD   dwLen;
    LONG    ret;

    // Allocate space for buffers
    dwLen = m_cMaxSubKeyLen;
    pName = (TCHAR*)malloc(dwLen * sizeof(TCHAR));
    memset(pName, 0, dwLen * sizeof(TCHAR));

    ret = CRegKey::EnumKey(dwIndex, pName, &dwLen, NULL);
    strName = pName;
    free(pName);

    return ret;
}

#endif __REGKEYEX_H__INCLUDED__

注册表项有1725个值,GetValuesCount只返回762!

0 个答案:

没有答案