Registry wrapping class

from Study/API 2007/12/11 21:24 view 23125
1. Registry.h
/** \file Registry.h
**    \date  2007/12/11
**    \author kimjunju@hotmail.com
**/

#pragma once

/**    \class CRegistry
    \brief 레지스트리를 관리하는 클래스
*/


/** \ingroup  UTIL */

class CRegistry
{
public:
    CRegistry(void);
    virtual ~CRegistry(void);
public:
    BOOL Open(HKEY rootKey, LPCTSTR subKey);
    BOOL Close(void);

    BOOL CreateKey(HKEY rootKey, LPCTSTR subKey);
    BOOL DeleteKey(HKEY rootKey, LPCTSTR subKey);

    BOOL SetValue(LPCTSTR valueName, LPCTSTR value);
    BOOL SetValue(LPCTSTR valueName, DWORD value);
    BOOL SetValueForMultiSz(LPCTSTR valueName, LPCTSTR value, DWORD byteLength);

    BOOL GetValue(LPCTSTR valueName, LPCTSTR value, LPDWORD bufferLength);
    BOOL GetValue(LPCTSTR valueName, LPDWORD value);

    BOOL DeleteValue(LPCTSTR valueName);
private:
    HKEY    mRootKey;
    BOOL    mIsOpened;
};

2. Registry.cpp
#include "stdafx.h"
#include "Registry.h"

CRegistry::CRegistry(void)
{
    mRootKey    = NULL;
    mIsOpened    = FALSE;
}

CRegistry::~CRegistry(void)
{
}

BOOL CRegistry::Open(HKEY rootKey, LPCTSTR subKey)
{
    if (!subKey)
        return FALSE;

    if (mIsOpened)
        return FALSE;

    if (RegOpenKey(rootKey, subKey, &mRootKey) != ERROR_SUCCESS)
        return FALSE;

    mIsOpened = TRUE;

    return TRUE;
}

BOOL CRegistry::Close(void)
{
    if (RegCloseKey(mRootKey) != ERROR_SUCCESS)
        return FALSE;

    mIsOpened = FALSE;

    return TRUE;
}

BOOL CRegistry::CreateKey(HKEY rootKey, LPCTSTR subKey)
{
    if (!subKey)
        return FALSE;
   
    if (mIsOpened)
        return FALSE;

    if (RegCreateKey(rootKey, subKey, &mRootKey) != ERROR_SUCCESS)
        return FALSE;

    mIsOpened = TRUE;

    return TRUE;
}

BOOL CRegistry::DeleteKey(HKEY rootKey, LPCTSTR subKey)
{
    if (!subKey)
        return FALSE;

    if (RegDeleteKey(rootKey, subKey) != ERROR_SUCCESS)
        return FALSE;

    return TRUE;
}

BOOL CRegistry::SetValue(LPCTSTR valueName, LPCTSTR value)
{
    if (!valueName || !value)
        return FALSE;

    if (!mIsOpened)
        return FALSE;

    if (RegSetValueEx(mRootKey, valueName, 0, REG_SZ, (BYTE*) value, (DWORD) _tcslen(value) * sizeof(TCHAR))
        != ERROR_SUCCESS)
        return FALSE;

    return TRUE;
}

BOOL CRegistry::SetValueForMultiSz(LPCTSTR valueName, LPCTSTR value, DWORD byteLength)
{
    if (!valueName || !value)
        return FALSE;

    if (!mIsOpened)
        return FALSE;

    if (RegSetValueEx(mRootKey, valueName, 0, REG_MULTI_SZ, (BYTE*) value, byteLength)
        != ERROR_SUCCESS)
        return FALSE;

    return TRUE;
}

BOOL CRegistry::SetValue(LPCTSTR valueName, DWORD value)
{
    if (!valueName)
        return FALSE;

    if (!mIsOpened)
        return FALSE;

    if (RegSetValueEx(mRootKey, valueName, 0, REG_DWORD, (BYTE*) &value, sizeof(DWORD))
        != ERROR_SUCCESS)
        return FALSE;

    return TRUE;
}

BOOL CRegistry::GetValue(LPCTSTR valueName, LPCTSTR value, LPDWORD bufferLength)
{
    DWORD ValueType = 0;

    if (!valueName || !value || !bufferLength)
        return FALSE;

    if (!mIsOpened)
        return FALSE;

    if (RegQueryValueEx(mRootKey, valueName, 0, &ValueType, (BYTE*) value, bufferLength)
        != ERROR_SUCCESS)
        return FALSE;

    return TRUE;
}

BOOL CRegistry::GetValue(LPCTSTR valueName, LPDWORD value)
{
    DWORD    ValueType        = 0;
    DWORD    BufferLength    = sizeof(DWORD);

    if (!valueName || !value)
        return FALSE;

    if (!mIsOpened)
        return FALSE;

    if (RegQueryValueEx(mRootKey, valueName, 0, &ValueType, (BYTE*) value, &BufferLength)
        != ERROR_SUCCESS)
        return FALSE;

    return TRUE;
}

BOOL CRegistry::DeleteValue(LPCTSTR valueName)
{
    if (!valueName)
        return FALSE;

    if (!mIsOpened)
        return FALSE;

    if (RegDeleteValue(mRootKey, valueName) != ERROR_SUCCESS)
        return FALSE;

    return TRUE;
}

Trackback Address :: 이 글에는 트랙백을 보낼 수 없습니다