본문 바로가기
IT-개발,DB

[개발/VC] 레지스트리 키 열기, 닫기, 목록 가져오기, 키 값 가져오기

by SB리치퍼슨 2012. 9. 20.

[개발/VC] 레지스트리 키 열기, 닫기, 목록 가져오기, 키 값 가져오기 


레지스트리 키 열기, 닫기
키 목록 가져오기,
키에 해당하는 값 가져오기(GetText, GetDword 외 필요하시면 추가하시면 됩니다.)
키하위의 값목록 가져오기.

이와 같은 동작을 하는 함수만 아래 나열.
 
typedef struct REGVALUE_T
{
    enum
    {
        TYPE_NONE,
        TYPE_DWORD,
        TYPE_SZ
    }type;
    string key;
    union
    {
        DWORD val_dword;
        char *val_sz;
    }value;
}RegValue, *PRegValue;
 
typedef list RegValueList;
 
 
HKEY RegCtrl::OpenKey(string keyName)
{
    HKEY hKey = NULL;
    string retval_;
    LONG lretval_;
    string rValueName_;
 
    lretval_ = RegOpenKeyEx( HKEY_LOCAL_MACHINE, keyName.c_str(), 0, KEY_ALL_ACCESS, &hKey );
    if(lretval_ != ERROR_SUCCESS)
    {
        return NULL;
    }
 
    return hKey;
}
 
void RegCtrl::CloseKey(HKEY hKey)
{
    // TODO: Add your specialized code here.
    LONG lretval_ = RegCloseKey(hKey);
    if(lretval_ != ERROR_SUCCESS)
    {
    }
}
 
 list RegCtrl::GetKeyList(HKEY hKey)
{
    list retval_;
    CHAR     achKey[MAX_KEY_LENGTH];    // buffer for subkey name
    DWORD   cbName;                     // size of name string 
    CHAR     achClass[MAX_PATH] = "";   // buffer for class name 
    DWORD   cchClassName = MAX_PATH;    // size of class string 
    DWORD   cSubKeys=0;                 // number of subkeys 
    DWORD   cbMaxSubKey;                // longest subkey size 
    DWORD   cchMaxClass;                // longest class string 
    DWORD   cValues;                    // number of values for key 
    DWORD   cchMaxValue;                // longest value name 
    DWORD   cbMaxValueData;             // longest value data 
    DWORD   cbSecurityDescriptor;       // size of security descriptor 
    FILETIME ftLastWriteTime;           // last write time 
 
    DWORD i, lretval_; 
 
    DWORD cchValue = MAX_VALUE_NAME; 
 
    // Get the class name and the value count. 
    lretval_ = RegQueryInfoKey(
        hKey,   // key handle 
        achClass,               // buffer for class name 
        &cchClassName,          // size of class string 
        NULL,                   // reserved 
        &cSubKeys,              // number of subkeys 
        &cbMaxSubKey,           // longest subkey size 
        &cchMaxClass,           // longest class string 
        &cValues,               // number of values for this key 
        &cchMaxValue,           // longest value name 
        &cbMaxValueData,        // longest value data 
        &cbSecurityDescriptor,  // security descriptor 
        &ftLastWriteTime);      // last write time 
 
    // Enumerate the subkeys, until RegEnumKeyEx fails.
    
    if (cSubKeys)
    {
//      printf( "Number of subkeys: %d\n", cSubKeys);
        for (i=0; i
        { 
            cbName = MAX_KEY_LENGTH;
            lretval_ = RegEnumKeyEx(hKey,
                                    i,
                                    achKey, 
                                    &cbName, 
                                    NULL, 
                                    NULL, 
                                    NULL, 
                                    &ftLastWriteTime); 
            if (lretval_ == ERROR_SUCCESS) 
            {
                retval_.push_back( achKey);
            }
        }
    } 
    return retval_;
}
 
string RegCtrl::GetText(HKEY hKey, string name)
{
    string retval_;
    DWORD size_ = 0;
    LONG lretval_;
 
    retval_.resize(256);
    size_ = retval_.size();
 
    lretval_ = RegQueryValueEx( hKey, name.c_str(), NULL, NULL, (LPBYTE)((LPCTSTR)retval_.c_str()), &size_);
    if(lretval_ == ERROR_MORE_DATA)
    {
        retval_.resize(retval_.size() + size_);
        size_ = retval_.size();
        lretval_ = RegQueryValueEx(hKey, name.c_str(), 0, NULL, (LPBYTE)((LPCTSTR)retval_.c_str()), &size_);
    }
    else if(lretval_ != ERROR_SUCCESS)
    {
        retval_.resize(0);
    }
 
    return retval_;
}
 
DWORD RegCtrl::GetDword(HKEY hKey, string name)
{
    DWORD retval_ = 0;
    DWORD size_ = 0;
    LONG lretval_;
 
    size_ = sizeof(retval_);
 
    lretval_ = RegQueryValueEx( hKey, name.c_str(), NULL, NULL, (LPBYTE)&retval_, &size_);
    if(lretval_ != ERROR_SUCCESS)
    {
        return 0;
    }
    
    return retval_;
}
 
RegValueList RegCtrl::GetValueList(HKEY hKey)
{
    RegValueList retval_;
//  CHAR    achKey[MAX_KEY_LENGTH]; // buffer for subkey name
//  DWORD   cbName;                     // size of name string 
    CHAR    achClass[MAX_PATH] = "";    // buffer for class name 
    DWORD   cchClassName = MAX_PATH;    // size of class string 
    DWORD   cSubKeys=0;                 // number of subkeys 
    DWORD   cbMaxSubKey;                // longest subkey size 
    DWORD   cchMaxClass;                // longest class string 
    DWORD   cValues;                    // number of values for key 
    DWORD   cchMaxValue;                // longest value name 
    DWORD   cbMaxValueData;             // longest value data 
    DWORD   cbSecurityDescriptor;       // size of security descriptor 
    DWORD   cType;                      // type of value
    FILETIME ftLastWriteTime;           // last write time 
 
    DWORD i, lretval_; 
 
    DWORD cchValue = MAX_VALUE_NAME; 
 
    // Get the class name and the value count. 
    lretval_ = RegQueryInfoKey(
        hKey,   // key handle 
        achClass,               // buffer for class name 
        &cchClassName,          // size of class string 
        NULL,                   // reserved 
        &cSubKeys,              // number of subkeys 
        &cbMaxSubKey,           // longest subkey size 
        &cchMaxClass,           // longest class string 
        &cValues,               // number of values for this key 
        &cchMaxValue,           // longest value name 
        &cbMaxValueData,        // longest value data 
        &cbSecurityDescriptor,  // security descriptor 
        &ftLastWriteTime);      // last write time 
 
    // Enumerate the key values. 
    CHAR  achValue[MAX_VALUE_NAME]; 
    if (cValues) 
    {
//      printf( "\nNumber of values: %d\n", cValues);
        for (i=0, lretval_=ERROR_SUCCESS; i
        { 
            cchValue = MAX_VALUE_NAME; 
            achValue[0] = '\0'; 
            lretval_ = RegEnumValue(hKey, i, 
                achValue, 
                &cchValue, 
                NULL, 
                &cType,
                NULL,
                NULL);
 
            if (lretval_ == ERROR_SUCCESS ) 
            {
                RegValue value;
                value.key = achValue;
                if(cType == REG_DWORD)
                {
                    value.type = RegValue::TYPE_DWORD;
                    value.value.val_dword = YMRegCtrl::GetDword(hKey, value.key);
                }
                else if(cType == REG_SZ)
                {
                    value.type = RegValue::TYPE_SZ;
                    string tmp_ = YMRegCtrl::GetText(hKey, value.key);
                    value.value.val_sz = new char[tmp_.length()+2];
                    strcpy(value.value.val_sz, tmp_.c_str());
                }
                else
                {
                    value.type = RegValue::TYPE_NONE;
                }
                retval_.insert(retval_.end(), value);
//              printf("(%d) %s\n", i+1, achValue); 
            } 
        }
    }
 
    return retval_;
}

반응형

댓글