⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 registrystore.cpp

📁 一个WinCE6。0下的IP phone的源代码
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "RegistryStore.hpp"
#include <auto_xxx.hxx>         //for auto_bstr, auto_hfile, auto_hfine etc

const HKEY      RegistryStore_t::sc_RootKey = HKEY_LOCAL_MACHINE; 
const WCHAR*    RegistryStore_t::sc_SubKeys[] = 
{
    L"Security\\phone\\VoIP\\Provisioning0", 
    L"Security\\phone\\VoIP\\Provisioning1", 
}; 


RegistryStore_t::RegistryStore_t(
    const WCHAR*    c_pName
    )
{
    if ((c_pName == NULL) ||
        (c_pName[0] == NULL))
    {
        ASSERT(FALSE); 
    }

    if (!m_Name.assign(c_pName))
    {
        ASSERT(FALSE); 
    }

}

RegistryStore_t::~RegistryStore_t(
    void
    )
{
    ; 
}


HRESULT
RegistryStore_t::SetValue(
    SettingCategory_e   OperationCategory,     
    const BYTE*         c_pValue, 
    DWORD               ValueSize
    )
{
    if (c_pValue == NULL || 
        ValueSize <= 0)
    {
        return E_INVALIDARG; 
    }

    HKEY SubKey; 
    long Result = RegCreateKeyEx(
                    sc_RootKey, 
                    sc_SubKeys[OperationCategory], 
                    0, NULL, 
                    REG_OPTION_NON_VOLATILE, 
                    0, NULL, 
                    &SubKey, 
                    NULL
                    ); 
    if (Result != ERROR_SUCCESS || 
        SubKey == NULL)
    {
        return E_FAIL; 
    }

    Result = RegSetValueEx(
                SubKey, 
                m_Name, 
                0,
                REG_NONE, 
                c_pValue, 
                ValueSize
                ); 
    
    RegCloseKey(SubKey); 

    return (Result == ERROR_SUCCESS) ? S_OK : E_FAIL; 
}

HRESULT
RegistryStore_t::GetValue(
    SettingCategory_e   OperationCategory,     
    BYTE**              ppValue, 
    DWORD*              pValueSize
    )
{
    if (ppValue == NULL || 
        pValueSize == NULL)
    {
        return E_INVALIDARG; 
    }

    *ppValue    = NULL; 
    *pValueSize = 0;
    
    DWORD           RegSize; 
    ce::auto_hkey   SubKey; 

    long Result = RegOpenKeyEx(
                    sc_RootKey, 
                    sc_SubKeys[OperationCategory], 
                    0, 0, 
                    &SubKey
                    ); 
    if (Result == ERROR_FILE_NOT_FOUND)
    {
        return S_OK; 
    }
    
    if (Result != ERROR_SUCCESS)
    {
        return E_FAIL; 
    }

    Result = RegQueryValueEx(
                    SubKey, 
                    m_Name, 
                    NULL, NULL, NULL, 
                    &RegSize
                    ); 
    if (Result == ERROR_FILE_NOT_FOUND)
    {
        return S_OK; 
    }

    if (Result != ERROR_SUCCESS)
    {
        return E_FAIL; 
    }
                    
    BYTE*   pRegValue = reinterpret_cast<BYTE*>(LocalAlloc(LMEM_ZEROINIT, RegSize)); 
    if (pRegValue == NULL)
    {
        return E_OUTOFMEMORY; 
    }

    Result = RegQueryValueEx(
                SubKey, 
                m_Name, 
                NULL,NULL, 
                pRegValue, 
                &RegSize
                ); 
    if (Result != ERROR_SUCCESS)
    {
        if (pRegValue != NULL)   
        {
            LocalFree(pRegValue); 
        }
        
        return E_FAIL; 
    }

    *ppValue    = pRegValue; 
    *pValueSize = RegSize; 

    return S_OK; 
                        
}
        
    
HRESULT
RegistryStore_t::Delete(
    SettingCategory_e   OperationCategory, 
    bool*               pNotExist
    )
{
    if (pNotExist != NULL)
    {
        *pNotExist = false; 
    }
    
    HKEY SubKey; 
    long Result = RegCreateKeyEx(
                    sc_RootKey, 
                    sc_SubKeys[OperationCategory], 
                    0, NULL, 
                    REG_OPTION_NON_VOLATILE, 
                    0, NULL, 
                    &SubKey, 
                    NULL
                    ); 
    if (Result != ERROR_SUCCESS || 
        SubKey == NULL)
    {
        return E_FAIL; 
    }

    Result = RegDeleteValue(
                SubKey, 
                m_Name 
                ); 
    
    RegCloseKey(SubKey); 

    if (Result == ERROR_FILE_NOT_FOUND && pNotExist != NULL)
    {
        *pNotExist = true; 
    }

    return (Result == ERROR_SUCCESS || Result == ERROR_FILE_NOT_FOUND) ? S_OK : E_FAIL; 
        
}


HRESULT
RegistryStore_t::CheckOldValueExists(
    SettingCategory_e   CheckCategory, 
    bool*               pNotExist
    )
{
    if (pNotExist == NULL)
    {
        return E_INVALIDARG; 
    }
    
    ce::auto_hkey   SubKey; 
    long    Result = RegOpenKeyEx(
                        sc_RootKey, 
                        sc_SubKeys[CheckCategory], 
                        0, 0, 
                        &SubKey
                        ); 
    if (Result != ERROR_SUCCESS)
    {
        *pNotExist = true; 
        return S_OK; 
    }
    
    Result = RegQueryValueEx(
                SubKey, 
                m_Name, 
                NULL, NULL, NULL, NULL
                ); 

    *pNotExist = (Result == ERROR_FILE_NOT_FOUND); 

    return S_OK; 
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -