norains的专栏

只专注于WINCE开发

用户操作
[即时聊天] [发私信] [加为好友]
norainsID:norains
142879次访问,排名598,好友0人,关注者53人。
代码其实是一种乐趣
norains的文章
原创 189 篇
翻译 0 篇
转载 10 篇
评论 274 篇
norains的公告
联系方式请看置顶文章
最近评论
dfdf:讨厌MFC!我觉得MFC就是太乱了!看似无用的代码不要不行,MD微软啥都给我们做完了,原理性的东西我们却永远没法搞懂了!
ironox:有个地方 我觉得很别扭,不知道怎么办好

比如说 CReg reg(HKEY_CURRENT_USER,TEXT("ControlPanel\Volume"));
ControlPanel\Volume 有可能不存在呀,这个该怎么处理哦?对象虽然创建了,出错了也没提示
szterry:呵呵,果然工作狂技术狂,同感,一样的感觉……不过我才刚毕业一年……搞IT就是玩……
jinlking:这个botton的实现只是在主窗口画了一块区域,对于事件的处理还要放在主窗口的窗口处理函数之中,在对应的消息处理上调用CheckTap来判断是否是此“按钮”,问一下,这种方法与把按钮封装在子窗口中有什么区别,二者使用那个更好?
KUODY:博主真是好人
文章分类
收藏
    相册
    动漫
    文章图片
    程序交流
    xumercury的BLOG
    狗友们的博客
    清蒸石斑鱼
    美女如刀锋
    茁茁的BLOG
    魅力老姐的窝
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 CReg更新至V1.1.0收藏

    新一篇: WinCE实时获取电源状态变化 | 旧一篇: 自己比较喜欢的CCommon类

    //========================================================================
    //TITLE:
    //    CReg更新至V1.1.0
    //AUTHOR:
    //    norains
    //DATE:
    //    wednesday  60-June-2007
    //Environment:
    //        EVC4.0 + Standard SDK
    //========================================================================

        v1.0.0版本及具体用法:http://blog.csdn.net/norains/archive/2007/04/08/1556296.aspx

        相对V1.0.0版本的改进:
        1.删除难以实现并且容易出问题的两个函数:    LPCTSTR GetValueSZ(LPCTSTR szName),    LPBYTE GetValueBinary(LPCTSTR szName);
        2.修改如下函数的声明:EnumValue,GetValueSZ
        3.完善部分函数的说明
        4.增加如下函数:GetValueType,CheckKeyExist
        5.修正一些已知的bug


    ///////////////////////////////////////////////////////////////////////
    // Reg.h: interface for the CReg class.
    //
    //Version:
    //    1.1.0
    //Date:
    //    2007.05.28
    //////////////////////////////////////////////////////////////////////

    #ifndef REG_H
    #define REG_H 

    //---------------------------------------------------------------------
    enum ValueType
    {
        VAL_ERROR,
        VAL_DWORD,
        VAL_MULTISZ,
        VAL_SZ,
        VAL_BINARY
    };
    //---------------------------------------------------------------------

    class CReg  
    {
    public:
        BOOL CheckKeyExist(HKEY hkRoot,LPCTSTR pszKey);
        ValueType GetValueType(LPCTSTR szName);
        BOOL DeleteKey(LPCTSTR szName);
        BOOL DeleteValue(LPCTSTR szName);
        BOOL SetMultiSZ(LPCTSTR szName, LPCTSTR lpszValue, DWORD dwLen);
        BOOL SetBinary(LPCTSTR szName, LPBYTE lpbValue, DWORD dwLen);
        BOOL SetDW(LPCTSTR szName, DWORD dwValue);
        BOOL SetSZ(LPCTSTR szName, LPCTSTR szValue);
        BOOL SetSZ(LPCTSTR szName, LPCTSTR szValue, DWORD dwLen);
        DWORD GetValueDW(LPCTSTR szName, DWORD dwDefault
    =0);
        DWORD GetValueBinary(LPCTSTR szName, LPBYTE lpbValue, DWORD dwLen);
        DWORD GetValueSZ(LPCTSTR szName, LPTSTR szValue, DWORD dwLen);
        BOOL EnumValue(LPTSTR pszName, DWORD dwLenName, LPBYTE lpValue, DWORD dwLenValue);
        BOOL EnumKey(LPTSTR psz, DWORD dwLen);
        BOOL IsOK();
        
    operator HKEY();
        
    void Reset();
        CReg(HKEY hkRoot, LPCTSTR pszKey);
        BOOL Open(HKEY hkRoot, LPCTSTR pszKey, REGSAM sam
    =KEY_READ);
        BOOL Create(HKEY hkRoot, LPCTSTR pszKey);
        CReg();
        
    virtual ~CReg();

    private:
        HKEY    m_hKey;
        
    int        m_Index;
        LPBYTE    m_lpbValue; 
    // last value read, if any
    };

    #endif //#ifndef REG_H


    //////////////////////////////////////////////////////////////////////
    // Reg.cpp: implementation of the CReg class.
    //
    //////////////////////////////////////////////////////////////////////

    #include 
    "stdafx.h"
    #include 
    "Reg.h"


    //=======================================================================
    //Macro define
    #define MyFree(p)            { if(p) LocalFree(p); }
    //=======================================================================
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////


    //-------------------------------------------------------------------
    //Description:
    //    Construction and open the key
    //-------------------------------------------------------------------
    CReg::CReg()
    {
        m_hKey 
    = NULL;
        m_Index 
    = 0;
        m_lpbValue 
    = NULL;
    }

    //-------------------------------------------------------------------
    //Description:
    //    Construction and open the key
    //
    //Parameters:
    //    hkRoot: [in] The root key
    //    pszKey: [in] The key to open
    //-------------------------------------------------------------------
    CReg::CReg(HKEY hkRoot, LPCTSTR pszKey)
    {
        m_hKey 
    = NULL;
        m_Index 
    = 0;
        m_lpbValue 
    = NULL;
        Open(hkRoot, pszKey);
    }

    //-------------------------------------------------------------------
    //Description:
    //    Destruction
    //-------------------------------------------------------------------
    CReg::~CReg()
    {
        
    if(m_hKey)
        {
            RegCloseKey(m_hKey);
        }
        MyFree(m_lpbValue);
    }


    //-------------------------------------------------------------------
    //Description:
    //    This function creates the specified key. 
    //If the key already exists in the registry, the function opens it.
    //After succeed in calling the fuction,should call the Reset() to release the resource.
    //
    //Parameters:
    //    hkRoot: [in] The root key
    //    pszKey: [in] The key to create
    //-------------------------------------------------------------------
    BOOL CReg::Create(HKEY hkRoot, LPCTSTR pszKey)
    {
        DWORD dwDisp;
        
    return ERROR_SUCCESS == RegCreateKeyEx(hkRoot, pszKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hKey, &dwDisp);
    }


    //-------------------------------------------------------------------
    //Description:
    //    This function opens the specified key.
    //After succeed in calling the fuction,should call the Reset() to release the resource.
    //
    //Parameters:
    //    hkRoot: [in] The root key
    //    pszKey: [in] The key to open
    //    sam: [in] Not supported; set to 0
    //-------------------------------------------------------------------
    BOOL CReg::Open(HKEY hkRoot, LPCTSTR pszKey, REGSAM sam)
    {
        
    return ERROR_SUCCESS == RegOpenKeyEx(hkRoot, pszKey, 0, sam, &m_hKey);
    }

    //-------------------------------------------------------------------
    //Description:
    //    Reset the value and release the resource.
    //-------------------------------------------------------------------
    void CReg::Reset()
    {
        
    if(m_hKey)
        {
            RegCloseKey(m_hKey); 
        }
        MyFree(m_lpbValue);
        m_hKey 
    = NULL;
        m_Index 
    = 0;
        m_lpbValue 
    = NULL;
    }

    //-------------------------------------------------------------------
    //Description:
    //    Operator overload
    //-------------------------------------------------------------------
    CReg::operator HKEY()
    {
        
    return m_hKey;
    }

    //-------------------------------------------------------------------
    //Description:
    //    Check whether is ready for next operate
    //
    //Return value:
    //    TRUE : Ready to do next operate
    //    FALSE: Not ready.
    //-------------------------------------------------------------------
    BOOL CReg::IsOK()
    {
        
    return m_hKey != NULL;
    }


    //-------------------------------------------------------------------
    //Description:
    //    This function enumerates subkeys of the specified open registry key.
    //
    //Parameters:
    //    psz: [out] Pointer to a buffer that receives the name of the subkey, including the terminating null character. 
    //        The function copies only the name of the subkey, not the full key hierarchy, to the buffer.
    //    dwLen: [in]  Specifies the length of the buffer pointed to by the psz parameter.
    //-------------------------------------------------------------------
    BOOL CReg::EnumKey(LPTSTR psz, DWORD dwLen)
    {
        
    if(!m_hKey)
        {
            
    return FALSE;
        }
        dwLen 
    *= sizeof(TCHAR);
        
    return ERROR_SUCCESS == RegEnumKeyEx(m_hKey, m_Index++, psz, &dwLen, NULL, NULL, NULL, NULL);
    }


    //-------------------------------------------------------------------
    //Description:
    //    This function enumerates the values for the specified open registry key.
    //
    //Parameters:
    //    pszName: [out] Pointer to a buffer that receives the name of the value, including the terminating null character. 
    //    dwLenName: [in] Specifies the length of the buffer pointed to by the pszName parameter. 
    //    lpValue: [out] Pointer to a buffer that receives the data for the value entry. This parameter can be NULL if the data is not required. 
    //    dwLenValue: [in] Specifies the length of the buffer pointed to by the lpValue parameter. 
    //-------------------------------------------------------------------
    BOOL CReg::EnumValue(LPTSTR pszName, DWORD dwLenName, LPBYTE lpValue, DWORD dwLenValue)
    {
        DWORD dwType;

        
    if(!m_hKey) 
        {
            
    return FALSE;
        }
        
        dwLenValue 
    *= sizeof(BYTE); // convert length in number to bytes
        
        
    return ERROR_SUCCESS == RegEnumValue(m_hKey, m_Index++, pszName, &dwLenName, NULL, &dwType, (LPBYTE)lpValue, &dwLenValue);
    }


    //--------------------------------------------------------------------------------------------------------------
    //Description:
    //    Get the string value
    //
    //Parameters:
    //    szName: [in] Pointer to a string containing the name of the value to query
    //    szValue: [out] Pointer to a buffer that receives the value's data. 
    //                If it's null, it will return the size of buffer need.
    //    dwLen: [in] Specifies the length of the buffer pointed to by the szValue parameter. 
    //                If szValue is null, the parameter would ignore.
    //
    //Return value:
    //    The number of the characters have been get.
    //0 means failed. Others means succeed, and the value include the null character
    //----------------------------------------------------------------------------------------------------------------------
    DWORD CReg::GetValueSZ(LPCTSTR szName, LPTSTR szValue, DWORD dwLen)
    {
        
    if(!m_hKey)
        {
            
    return FALSE;
        }
        
        dwLen 
    *= sizeof(TCHAR); // convert length in chars to bytes

        DWORD dwType ;
        
    if(ERROR_SUCCESS == RegQueryValueEx(m_hKey, szName, NULL, &dwType, (LPBYTE)szValue, &dwLen))
        {
            
    if(REG_SZ != dwType)
            {
                dwLen 
    = 0;
                
    return FALSE;
            }
            
    else
            {
                dwLen 
    /= sizeof(TCHAR);
            }
            
        }
        
    else
        {
            dwLen 
    = 0;
        }

        
    return dwLen;
    }

    //-------------------------------------------------------------------
    //Description:
    //    Get the binary value
    //
    //Parameters:
    //    szName: [in] Pointer to a string containing the name of the value to query
    //    lpbValue: [out] Pointer to a buffer that receives the value's data.
    //                If it's null, it will return the size of buffer need.
    //    dwLen: [in] Specifies the length of the buffer pointed to by the lpbValue parameter. 
    //                If szValue is null, the parameter would ignore.
    //
    //Return value:
    //    The number of the binary have been get.
    //0 means failed. Others means succeed, and the value include the null character
    //-------------------------------------------------------------------
    DWORD CReg::GetValueBinary(LPCTSTR szName, LPBYTE lpbValue, DWORD dwLen)
    {
        
    if(!m_hKey) 
        {
            
    return FALSE;
        }


        DWORD dwType ;
        
    if(ERROR_SUCCESS == RegQueryValueEx(m_hKey, szName, NULL, &dwType, lpbValue, &dwLen))
        {
            
    if(dwType != REG_BINARY)
            {
                dwLen 
    = 0;
            }
        }
        
    else
        {
            dwLen 
    = 0;
        }

        
    return dwLen;

    }



    //-------------------------------------------------------------------
    //Description:
    //    Get the DWORD value
    //
    //Parameters:
    //    szName:[in] The value of registry
    //    dwDefault:[in] The default value return when failed in getting the DWORD value.
    //
    //Return value:
    //    If the return value just be same as the dwDefault, it may mean failed.
    //-------------------------------------------------------------------
    DWORD CReg::GetValueDW(LPCTSTR szName, DWORD dwDefault)
    {
        
    if(!m_hKey) 
        {
            
    return FALSE;
        }
        DWORD dwValue 
    = dwDefault;
        DWORD dwLen 
    = sizeof(DWORD);
        DWORD dwType;
        
    if(ERROR_SUCCESS == RegQueryValueEx(m_hKey, szName, NULL, &dwType, (LPBYTE)&dwValue, &dwLen))
        {
            
    if(dwType != REG_DWORD)
            {
                dwValue 
    = dwDefault;
            }
        }
        
    else
        {
            dwValue 
    = dwDefault;
        }

        
    return dwValue;
    }


    //-------------------------------------------------------------------
    //Description:
    //    Set the string value
    //
    //Parameters:
    //    szName: [in] Pointer to a string containing the name of the value to set
    //    szValue: [in] The buffer include the data to be set
    //    dwLen: [in] Specifies the length of the buffer pointed to by the szValue parameter.  
    //-------------------------------------------------------------------
    BOOL CReg::SetSZ(LPCTSTR szName, LPCTSTR szValue, DWORD dwLen)
    {
        
    //Prefix
        if(!m_hKey) 
        {
            
    return FALSE;
        }
        
        
    return ERROR_SUCCESS == RegSetValueEx(m_hKey, szName, 0, REG_SZ, (LPBYTE)szValue, sizeof(TCHAR)*dwLen);
    }


    //-------------------------------------------------------------------
    //Description:
    //    Set the string value
    //
    //Parameters:
    //    szName: [in] Pointer to a string containing the name of the value to set
    //    szValue: [in] The string buffer include null character to be set 
    //-------------------------------------------------------------------
    BOOL CReg::SetSZ(LPCTSTR szName, LPCTSTR szValue)
    {
        
    return SetSZ(szName, szValue, 1 + _tcslen(szValue));
    }


    //-------------------------------------------------------------------
    //Description:
    //    Get the DWORD value
    //
    //Parameters:
    //    szName: [in] Pointer to a string containing the name of the value to set
    //    dwValue: [in] The value to be set
    //-------------------------------------------------------------------
    BOOL CReg::SetDW(LPCTSTR szName, DWORD dwValue)
    {
        
    //Prefix
        if(!m_hKey) 
        {
            
    return FALSE;
        }
        
        
    return ERROR_SUCCESS==RegSetValueEx(m_hKey, szName, 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(DWORD));
    }


    //-------------------------------------------------------------------
    //Description:
    //    Get the binary value
    //
    //Parameters:
    //    szName: [in] Pointer to a string containing the name of the value to set
    //    lpbValue: [in] The buffer include the data to be set
    //    dwLen: [in] Specifies the length of the buffer pointed to by the lpbValue parameter.
    //-------------------------------------------------------------------
    BOOL CReg::SetBinary(LPCTSTR szName, LPBYTE lpbValue, DWORD dwLen)
    {
        
    //Prefix
        if(!m_hKey) 
        {
            
    return FALSE;
        }

        
    return ERROR_SUCCESS == RegSetValueEx(m_hKey, szName, 0, REG_BINARY, lpbValue, sizeof(BYTE) * dwLen);
    }


    //-------------------------------------------------------------------
    //Description:
    //    Set the Multi value
    //
    //Parameters:
    //    szName: [in] Pointer to a string containing the name of the value to set
    //    lpszValue: [in] The buffer include the data to be set
    //    dwLen: [in] Specifies the length of the buffer pointed to by the lpszValue parameter.
    //-------------------------------------------------------------------
    BOOL CReg::SetMultiSZ(LPCTSTR szName, LPCTSTR lpszValue, DWORD dwLen)
    {
        
    return ERROR_SUCCESS == RegSetValueEx(m_hKey, szName, 0, REG_MULTI_SZ, (LPBYTE)lpszValue, sizeof(TCHAR)*dwLen);
    }


    //-------------------------------------------------------------------
    //Description:
    //    This function removes a named value from the specified registry key.
    //
    //Parameters:
    //    szName:[in] Pointer to a null-terminated string that names the value to remove.
    //-------------------------------------------------------------------
    BOOL CReg::DeleteValue(LPCTSTR szName)
    {
        
    //Prefix
        if(!m_hKey) 
        {
            
    return FALSE;
        }
        
    //
        return ERROR_SUCCESS == RegDeleteValue(m_hKey, szName);
    }



    //-------------------------------------------------------------------
    //Description:
    //    This function recursively deletes all subkeys of a named subkey of a specified registry key.
    //
    //Parameters:
    //    szName:[in] Pointer to a null-terminated string specifying the name of the key to delete. This parameter cannot be NULL. 
    //-------------------------------------------------------------------
    BOOL CReg::DeleteKey(LPCTSTR szName)
    {
        
    if(!m_hKey) 
        {
            
    return FALSE;
        }
        
        
    return ERROR_SUCCESS == RegDeleteKey(m_hKey, szName);
    }


    //-------------------------------------------------------------------
    //Description:
    //    Get the value type
    //
    //Parameters:
    //    szName: [in] Pointer to a null-terminated string that names the value
    // 
    //------------------------------------------------------------------------
    ValueType CReg::GetValueType(LPCTSTR szName)
    {
        
    if(m_hKey == NULL)
        {
            
    return VAL_ERROR;
        }

        DWORD dwType ;
        
    if(ERROR_SUCCESS != RegQueryValueEx(m_hKey, szName, NULL, &dwType, 00))
        {
            
    return VAL_ERROR;
        }


        
    switch(dwType)
        {
            
    case REG_BINARY:
                
    return VAL_BINARY;

            
    case REG_DWORD:
            
    case REG_DWORD_BIG_ENDIAN:
                
    return VAL_DWORD;

            
    case REG_EXPAND_SZ:
            
    case REG_SZ:
                
    return VAL_SZ;

            
    case REG_MULTI_SZ:
                
    return VAL_MULTISZ;

            
    default:
                
    return VAL_ERROR;    // There are other types, but not supported by CReg
        }
    }


    //-------------------------------------------------------------------
    //Description:
    //    Check the key exist.
    //
    //Parameters:
    //    pszKey: [in] Pointer to a null-terminated string that names key
    // 
    //------------------------------------------------------------------------
    BOOL CReg::CheckKeyExist(HKEY hkRoot,LPCTSTR pszKey)
    {
        HKEY hKey 
    = 0;
        
    if(ERROR_SUCCESS == RegOpenKeyEx(hkRoot, pszKey, 00&hKey))
        {
            RegCloseKey(hKey);
            
    return TRUE;
        }
        
    else
        {
            
    return FALSE;
        }


        
    }

    发表于 @ 2007年06月20日 21:05:00|评论(loading...)|编辑

    新一篇: WinCE实时获取电源状态变化 | 旧一篇: 自己比较喜欢的CCommon类

    评论

    #ironox 发表于2008-11-27 19:02:29  IP: 61.145.186.*
    有个地方 我觉得很别扭,不知道怎么办好

    比如说 CReg reg(HKEY_CURRENT_USER,TEXT("ControlPanel\Volume"));
    ControlPanel\Volume 有可能不存在呀,这个该怎么处理哦?对象虽然创建了,出错了也没提示
    2008-11-27 19:37:39作者回复
    没办法,因为构造函数是没有返回值的。你可以调用IsOK函数看看是否正确打开了。
    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © norains