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

📄 registryhelper.cpp

📁 PW芯片方案Flash ROM烧写程序
💻 CPP
字号:
//---------------------------------------------------------------------------
// Pixelworks Inc. Company Confidential Strictly Private
//
// $Archive: /SwTools/FlashUpgrader/RegistryHelper.cpp $
// $Revision: 1.1.1.1 $
// $Author: KevinM $
// $Date: 2003/09/29 18:19:04 $
//
// --------------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// --------------------------------------------------------------------------
// Copyright 1997 (c) Pixelworks Inc.
//
// Pixelworks owns the sole copyright to this software. Under international 
// copyright laws you (1) may not make a copy of this software except for 
// the purposes of maintaining a single archive copy, (2) may not derive
// works herefrom, (3) may not distribute this work to others. These rights 
// are provided for information clarification, other restrictions of rights 
// may apply as well.
//
// This is an unpublished work.
// --------------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>>>>>> WARRANTEE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// --------------------------------------------------------------------------
// Pixelworks Inc. MAKES NO WARRANTY OF ANY KIND WITH REGARD TO THE USE OF
// THIS SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
// PURPOSE.
// --------------------------------------------------------------------------

#include "stdafx.h"
#include "RegistryHelper.h"

//----------------------------------------------------------------------------
// Definitions used in this file
//----------------------------------------------------------------------------
#define MAX_LONG_IN_ASCII   (256)

//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
void gCloseKey(HKEY hOpenKey)
{
    try
    {
        ::RegCloseKey(hOpenKey);
        hOpenKey = 0;
    }
    catch(CException *pExc)
    {
        TRACE("RegCloseKey CException thrown\n");
        pExc->Delete();
    }
    catch(...)
    {
        TRACE("RegCloseKey unknown exception\n");
    }
}

//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL gSetValueInRegistry(HKEY hKey, LPCSTR pstrKey, LPCSTR pstrValueName,
                         LPCSTR pstrData)
{
    HKEY hOpenKey;
    DWORD dwDispValue;
    if (ERROR_SUCCESS != RegCreateKeyEx(
            hKey,	        // handle of an open key or predefined reserved key
            pstrKey,        // address of subkey name
            0,      	    // reserved
            0,	            // address of class string
            0,	            // special options flag
            KEY_WRITE,	    // desired security access
            NULL,	        // address of key security structure 
            &hOpenKey,	    // address of buffer for opened handle
            &dwDispValue))  // address of disposition value buffer
    {
        TRACE2("RegCreateKeyEx failed. Key= %s, Error= %d\n", pstrKey,
               ::GetLastError());
        return FALSE;
    }

    BOOL bStatus = TRUE;
    if (ERROR_SUCCESS != RegSetValueEx(
            hOpenKey,	    // handle of key to set value for  
            pstrValueName,  // address of value to set
            0,  	        // reserved
            REG_SZ, 	    // flag for value type
            (const BYTE*)pstrData,	// address of value data
            strlen(pstrData) + 1)) 	// size of value data
    {
        TRACE3("RegSetValueEx failed. Key= %s, Value= %s, Error= %d\n",
                pstrKey, pstrValueName, ::GetLastError());
        bStatus = FALSE;
    }

    gCloseKey(hOpenKey);

    return bStatus;
}

//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL gGetValueFromRegistry(HKEY hKey, LPCSTR pstrKey, LPCSTR pstrValueName,
                           CString *pcstrData)
{
    //------------------------------------------------------------------------
    // Make sure passed in pointers are not NULL.
    //------------------------------------------------------------------------
    if (NULL == pcstrData)
    {
        TRACE("<gGetValueFromRegistry> NULL pointer\n");
        return FALSE;
    }

    pcstrData->Empty();

    BYTE chrVal[MAX_LONG_IN_ASCII];
    DWORD dwMaxSize = sizeof(chrVal);
    DWORD dwValueType;

    HKEY hOpenKey;
    if (ERROR_SUCCESS != RegOpenKeyEx(
            hKey,                       // handle of open key or predefined reserved key
            pstrKey,                    // address of name of subkey to open
            0,                  	    // reserved
            KEY_READ,            	    // security access mask 
            &hOpenKey))                 // address of handle of key
    {
        TRACE2("RegOpenKeyEx failed. Key= %s, Error= %d\n", pstrKey,
               ::GetLastError());
        return FALSE;
    }

    BOOL bStatus = TRUE;
    if (ERROR_SUCCESS != RegQueryValueEx(
        hOpenKey,
        pstrValueName,	// address of name of value to query
        NULL,       	// reserved
        &dwValueType,  	// address of buffer for value type
        &chrVal[0],   	// address of data buffer
        &dwMaxSize))  	// address of data buffer size
    {
        TRACE3("RegQueryValueEx failed. Key= %s, Value= %s, Error= %d\n",
                pstrKey, pstrValueName, ::GetLastError());
        bStatus = FALSE;
    }

    gCloseKey(hOpenKey);

    if (bStatus)
    {
        LPSTR pStr = pcstrData->GetBuffer(dwMaxSize);
        strcpy(pStr, (char*)&chrVal[0]);
        pcstrData->ReleaseBuffer();
    }

    return bStatus;
}

//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL gGetValueFromRegistry(HKEY hOpenKey, LPCSTR pstrValueName, CString *pcstrData)
{
    pcstrData->Empty();
    
	BYTE chrVal[MAX_LONG_IN_ASCII];
    DWORD dwMaxSize = sizeof(chrVal);
    DWORD dwValueType;

    if (ERROR_SUCCESS != RegQueryValueEx(
        hOpenKey,
        pstrValueName,	// address of name of value to query
        NULL,       	// reserved
        &dwValueType,  	// address of buffer for value type
        &chrVal[0],   	// address of data buffer
        &dwMaxSize))  	// address of data buffer size
    {
        TRACE2("RegQueryValueEx failed. Value= %s, Error= %d\n", pstrValueName,
			   ::GetLastError());
		return FALSE;
    }

    LPSTR pStr = pcstrData->GetBuffer(dwMaxSize);
    strcpy(pStr, (char*)&chrVal[0]);
    pcstrData->ReleaseBuffer();

	return TRUE;
}

//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL gEnumKeysFromRegistry(HKEY hKey, HKEY *phKey, LPCSTR pstrKey,
                           DWORD *dwIndex, CString *pcstrValueName)
{
    //------------------------------------------------------------------------
    // Make sure passed in pointers are not NULL.
    //------------------------------------------------------------------------
    if (NULL == phKey || NULL == dwIndex || NULL == pcstrValueName)
    {
        TRACE("<gEnumKeysFromRegistry> NULL pointer\n");
        return FALSE;
    }

    if (0 == *dwIndex)
    {
        if (ERROR_SUCCESS != RegOpenKeyEx(
                hKey,	    // handle of open key or predefined reserved key
                pstrKey,	// address of name of subkey to open 
                0,      	// reserved 
                KEY_ENUMERATE_SUB_KEYS,	// security access mask 
                phKey)) 	// address of handle of open key
        {
                TRACE2("RegOpenKeyEx failed. Key= %s, Error= %d\n", pstrKey,
                       ::GetLastError());
            return FALSE;
        }
    }

    char chrVal[MAX_LONG_IN_ASCII];
    DWORD dwMaxSize = sizeof(chrVal);

    BOOL bStatus = TRUE;
    if (ERROR_SUCCESS != RegEnumKeyEx(
                *phKey,	        // handle of key to enumerate
                *dwIndex,       // index of subkey to enumerate
                &chrVal[0],     // address of buffer for subkey name
                &dwMaxSize,	    // address for size of subkey buffer
                0,              // reserved
                NULL,          	// address of buffer for class string
                NULL,           // address for size of class buffer
                NULL))          // address for time key last written to
    {
        bStatus = FALSE;
    }

    if (bStatus)
    {
        LPSTR pStr = pcstrValueName->GetBuffer(dwMaxSize);
        strcpy(pStr, &chrVal[0]);
        pcstrValueName->ReleaseBuffer();
        *dwIndex++;
    }
    else
    {
        gCloseKey(*phKey);
        *phKey = NULL;
    }

    return bStatus;
}

//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL gEnumValuesFromRegistry(HKEY hKey, LPCSTR pstrKey, DWORD *dwIndex,
                             CString *pcstrValue)
{
    //------------------------------------------------------------------------
    // Make sure passed in pointers are not NULL.
    //------------------------------------------------------------------------
    if (NULL == dwIndex || NULL == pcstrValue)
    {
        TRACE("<gEnumValuesFromRegistry> NULL pointer\n");
        return FALSE;
    }

    HKEY hOpenKey;
	if (0 == *dwIndex)
	{
        if (ERROR_SUCCESS != RegOpenKeyEx(
                hKey,	            // handle of open key or predefined reserved key
                pstrKey,	        // address of name of subkey to open 
                0,              	// reserved 
                KEY_ENUMERATE_SUB_KEYS,	// security access mask 
                &hOpenKey))         // address of handle of open key
        {
            TRACE2("RegOpenKeyEx failed. Key= %s, Error= %d\n", pstrKey,
                   ::GetLastError());
            return FALSE;
        }
    }

    char chrVal[MAX_LONG_IN_ASCII];
    DWORD dwMaxSize = sizeof(chrVal);

    BOOL bStatus = TRUE;
    if (ERROR_SUCCESS != RegEnumValue(
                hOpenKey,  	// handle of key to query
                *dwIndex,	// index of value to query
                &chrVal[0],	// address of buffer for value string
                &dwMaxSize,	// address for size of value buffer
                0,          // reserved
                REG_NONE,	// address of buffer for type code
                NULL,   	// address of buffer for value data
                NULL))      // address for size of data buffer
    {
        bStatus = FALSE;
    }

    if (bStatus)
    {
        LPSTR pStr = pcstrValue->GetBuffer(dwMaxSize);
        strcpy(pStr, &chrVal[0]);
        pcstrValue->ReleaseBuffer();
        *dwIndex++;
    }
    else
    {
        gCloseKey(hOpenKey);
    }

    return bStatus;
}

//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL gRemoveKeyFromRegistry(HKEY hKey, LPCSTR pstrKey)
{
    if (ERROR_SUCCESS != RegDeleteKey(hKey, pstrKey))
    {
        TRACE("RegDeleteKey failed. Key= %s, Error= %d\n", pstrKey,
              ::GetLastError());
        return FALSE;
    }

    return TRUE;
}

⌨️ 快捷键说明

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