Windows/MFC

CExRegKey

aucd29 2013. 10. 2. 18:16
#pragma once

#include <atlbase.h>
#include "../Lib_BaseWin32/Log.h"

class CExRegKey : public CRegKey
{
public:
    // pszKeyPath : Value Name을 제외한 경로
    BOOL ExSetDefaultStringValue(LPCTSTR pszKeyPath, LPCTSTR pszValue)
    {
        if(!ExOpen(pszKeyPath))
            return FALSE;

        if(CRegKey::SetStringValue(NULL, pszValue) != ERROR_SUCCESS)
        {
            LOG_ERROR2(0, ::GetLastError());
            return FALSE;
        }

        return TRUE;
    }

    // pszValuePath의 경로에 포함된 Value Name이 없다면 새로 생성한다.
    BOOL ExSetDWORDValue(LPCTSTR pszValuePath, DWORD dwValue)
    {
        CString strKeyPath, strValueName;
        SplitValueName(pszValuePath, strKeyPath, strValueName);

        if(!ExOpen(strKeyPath))
            return FALSE;

        if(CRegKey::SetDWORDValue(strValueName, dwValue) != ERROR_SUCCESS)
        {
            LOG_ERROR2(0, ::GetLastError());
            return FALSE;
        }

        return TRUE;
    }

    BOOL ExSetQWORDValue(LPCTSTR pszValuePath, ULONGLONG qwValue)
    {
        CString strKeyPath, strValueName;
        SplitValueName(pszValuePath, strKeyPath, strValueName);

        if(!ExOpen(strKeyPath))
            return FALSE;

        if(CRegKey::SetQWORDValue(strValueName, qwValue) != ERROR_SUCCESS)
        {
            LOG_ERROR2(0, ::GetLastError());
            return FALSE;
        }

        return TRUE;
    }

    BOOL ExSetGUIDValue(LPCTSTR pszValuePath, GUID &guidValue)
    {
        CString strKeyPath, strValueName;
        SplitValueName(pszValuePath, strKeyPath, strValueName);

        if(!ExOpen(strKeyPath))
            return FALSE;

        if(CRegKey::SetGUIDValue(strValueName, guidValue) != ERROR_SUCCESS)
        {
            LOG_ERROR2(0, ::GetLastError());
            return FALSE;
        }

        return TRUE;
    }

    BOOL ExSetBinaryValue(LPCTSTR pszValuePath, const void *pValue, ULONG nBytes)
    {
        CString strKeyPath, strValueName;
        SplitValueName(pszValuePath, strKeyPath, strValueName);        

        if(!ExOpen(strKeyPath))
            return FALSE;

        if(CRegKey::SetBinaryValue(strValueName, pValue, nBytes) != ERROR_SUCCESS)
        {
            LOG_ERROR2(0, ::GetLastError());
            return FALSE;
        }

        return TRUE;
    }

    BOOL ExSetStringValue(LPCTSTR pszValuePath, LPCTSTR pszValue, DWORD dwType = REG_SZ)
    {
        CString strKeyPath, strValueName;
        SplitValueName(pszValuePath, strKeyPath, strValueName);

        if(!ExOpen(strKeyPath))
            return FALSE;

        if(CRegKey::SetStringValue(strValueName, pszValue, dwType) != ERROR_SUCCESS)
        {
            LOG_ERROR2(0, ::GetLastError());
            return FALSE;
        }

        return TRUE;
    }

    BOOL ExSetExpandStringValue(LPCTSTR pszValuePath, LPCTSTR pszValue)
    {
        return ExSetStringValue(pszValuePath, pszValue, REG_EXPAND_SZ);
    }

    // pszValue : NULL 2개로 끝이나는 문자열의 연속된 값
    BOOL ExSetMultiStringValue(LPCTSTR pszValuePath, LPCTSTR pszValue)
    {
        CString strKeyPath, strValueName;
        SplitValueName(pszValuePath, strKeyPath, strValueName);

        if(!ExOpen(strKeyPath))
            return FALSE;

        if(CRegKey::SetMultiStringValue(strValueName, pszValue) != ERROR_SUCCESS)
        {
            LOG_ERROR2(0, ::GetLastError());
            return FALSE;
        }

        return TRUE;
    }

    // pszKeyPth : Value Name을 제외한 경로
    // 예 : "HKEY_CURRENT_USER\\Environment"
    BOOL ExGetDefaultStringValue(LPCTSTR pszKeyPath, CString &strValue)
    {
        if(!ExOpen(pszKeyPath))
            return FALSE;

        return ExQueryStringValue(NULL, strValue);
    }

    BOOL ExGetDWORDValue(LPCTSTR pszValuePath, DWORD &dwValue)
    {
        CString strKeyPath, strValueName;
        SplitValueName(pszValuePath, strKeyPath, strValueName);

        if(!ExOpen(strKeyPath))
            return FALSE;

        if(QueryDWORDValue(strValueName, dwValue) != ERROR_SUCCESS)
            return FALSE;

        return TRUE;
    }

    BOOL ExGetQWORDValue(LPCTSTR pszValuePath, ULONGLONG &qwValue)
    {
        CString strKeyPath, strValueName;
        SplitValueName(pszValuePath, strKeyPath, strValueName);

        if(!ExOpen(strKeyPath))
            return FALSE;

        if(QueryQWORDValue(strValueName, qwValue) != ERROR_SUCCESS)
            return FALSE;

        return TRUE;
    }

    BOOL ExGetGUIDValue(LPCTSTR pszValuePath, GUID &guidValue)
    {
        CString strKeyPath, strValueName;
        SplitValueName(pszValuePath, strKeyPath, strValueName);

        if(!ExOpen(strKeyPath))
            return FALSE;

        if(QueryGUIDValue(strValueName, guidValue) != ERROR_SUCCESS)
            return FALSE;

        return TRUE;
    }

    BOOL ExGetBinaryValue(LPCTSTR pszValuePath, CAutoVectorPtr<BYTE> *pBuf, ULONG* pnBytes)
    {
        CString strKeyPath, strValueName;
        SplitValueName(pszValuePath, strKeyPath, strValueName);        

        if(!ExOpen(strKeyPath))
            return FALSE;

        return ExQueryBinaryValue(strValueName, pBuf, pnBytes);
    }

    // pszValuePath : Value Name을 포함한 경로
    // 예 : "HKEY_CURRENT_USER\\Environment\\TEMP"
    BOOL ExGetStringValue(LPCTSTR pszValuePath, CString &strValue)
    {
        CString strKeyPath, strValueName;
        SplitValueName(pszValuePath, strKeyPath, strValueName);

        if(!ExOpen(strKeyPath))
            return FALSE;

        return ExQueryStringValue(strValueName, strValue);
    }

    // strValue : NULL 2개로 끝이나는 문자열의 연속된 값
    BOOL ExGetMultiStringValue(LPCTSTR pszValuePath, CString &strValue)
    {
        CString strKeyPath, strValueName;
        SplitValueName(pszValuePath, strKeyPath, strValueName);

        if(!ExOpen(strKeyPath))
            return FALSE;

        return ExQueryMultiStringValue(strValueName, strValue);
    }
    
    // pszKeyPath : Value Name을 제외한 경로
    BOOL ExOpen(LPCTSTR pszKeyPath)
    {
        CString strKeyPath(pszKeyPath);
        CString strKeyRoot;

        int nOffset = 0;
        strKeyRoot = strKeyPath.Tokenize(_T("\\"), nOffset);
        strKeyPath.Delete(0, strKeyRoot.GetLength()+1);

        return ExOpen(strKeyRoot, strKeyPath);
    }

    BOOL ExOpen(LPCTSTR pszRootName, LPCTSTR pszKeyName)
    {
        HKEY hRootKey = GetRootKey(pszRootName);
        if(hRootKey == NULL)
            return FALSE;

        if(CRegKey::Open(hRootKey, pszKeyName) != ERROR_SUCCESS)
        {
            LOG_ERROR2(0, ::GetLastError());
            return FALSE;
        }

        return TRUE;
    }

    //
    BOOL ExQueryBinaryValue(LPCTSTR pszValueName, CAutoVectorPtr<BYTE> *pBuf, ULONG* pnBytes)
    {
        ULONG nBytes = 0;
        if(CRegKey::QueryBinaryValue(pszValueName, NULL, &nBytes) != ERROR_SUCCESS)
        {
            LOG_ERROR2(0, ::GetLastError());
            return FALSE;
        }

        if(!pBuf->Allocate(nBytes))
            return FALSE;
        if(CRegKey::QueryBinaryValue(pszValueName, *pBuf, pnBytes) != ERROR_SUCCESS)
        {
            LOG_ERROR2(0, ::GetLastError());
            return FALSE;
        }

        return TRUE;
    }

    BOOL ExQueryStringValue(LPCTSTR pszValueName, CString &strValue)
    {
        ULONG nChars = 0;
        if(CRegKey::QueryStringValue(pszValueName, NULL, &nChars) != ERROR_SUCCESS)
        {
            LOG_ERROR2(0, ::GetLastError());
            return FALSE;
        }

        LPTSTR psz = strValue.GetBuffer(nChars+1);
        if(CRegKey::QueryStringValue(pszValueName, psz, &nChars) != ERROR_SUCCESS)
        {
            strValue.ReleaseBuffer();

            LOG_ERROR2(0, ::GetLastError());
            return FALSE;
        }

        strValue.ReleaseBuffer();
        return TRUE;
    }

    BOOL ExQueryMultiStringValue(LPCTSTR pszValueName, CString &strValue)
    {
        ULONG nChars = 0;
        if(CRegKey::QueryMultiStringValue(pszValueName, NULL, &nChars) != ERROR_SUCCESS)
        {
            LOG_ERROR2(0, ::GetLastError());
            return FALSE;
        }

        LPTSTR psz = strValue.GetBuffer(nChars+1);
        if(CRegKey::QueryMultiStringValue(pszValueName, psz, &nChars) != ERROR_SUCCESS)
        {
            strValue.ReleaseBuffer();

            LOG_ERROR2(0, ::GetLastError());
            return FALSE;
        }

        strValue.ReleaseBuffer();
        return TRUE;
    }


private:
    void SplitValueName(CString strPath, CString &strKeyPath, CString &strValueName)
    {
        int nIndex = strPath.ReverseFind(_T('\\'));
        int nOffset = nIndex;
        strValueName = strPath.Tokenize(_T("\\"), nOffset);
        strPath.Delete(nIndex, strValueName.GetLength()+1);

        strKeyPath = strPath;
    }

    HKEY GetRootKey(LPCTSTR pszRoot)
    {
        HKEY hRootKey = NULL;

        if(_tcsicmp(pszRoot, _T("HKEY_CLASSES_ROOT")) == 0 || _tcsicmp(pszRoot, _T("HKCR")) == 0)
            hRootKey = HKEY_CLASSES_ROOT;
        else if(_tcsicmp(pszRoot, _T("HKEY_CURRENT_USER")) == 0 || _tcsicmp(pszRoot, _T("HKCU")) == 0)
            hRootKey = HKEY_CURRENT_USER;
        else if(_tcsicmp(pszRoot, _T("HKEY_LOCAL_MACHINE")) == 0 || _tcsicmp(pszRoot, _T("HKLM")) == 0)
            hRootKey = HKEY_LOCAL_MACHINE;
        else if(_tcsicmp(pszRoot, _T("HKEY_USERS")) == 0 || _tcsicmp(pszRoot, _T("HKU")) == 0)
            hRootKey = HKEY_USERS;
        else if(_tcsicmp(pszRoot, _T("HKEY_PERFORMANCE_DATA")) == 0)
            hRootKey = HKEY_PERFORMANCE_DATA;
        else if(_tcsicmp(pszRoot, _T("HKEY_PERFORMANCE_TEXT")) == 0)
            hRootKey = HKEY_PERFORMANCE_TEXT;
        else if(_tcsicmp(pszRoot, _T("HKEY_PERFORMANCE_NLSTEXT")) == 0)
            hRootKey = HKEY_PERFORMANCE_NLSTEXT;
        else if(_tcsicmp(pszRoot, _T("HKEY_CURRENT_CONFIG")) == 0 || _tcsicmp(pszRoot, _T("HKCC")) == 0)
            hRootKey = HKEY_CURRENT_CONFIG;
        else if(_tcsicmp(pszRoot, _T("HKEY_DYN_DATA")) == 0)
            hRootKey = HKEY_DYN_DATA;

        return hRootKey;
    }

private:
    HKEY m_hSubKey;
};