📄 registry.cpp
字号:
//---------------------------------------------------------------------------
// Copyright (C) 1998, Interscope Ltd. All rights reserved.
// Reproduction or distribution of this program, or any portion of it,
// is permitted only if this header is kept as it is.
// For more information, contact:
//
// Interscope Ltd., 5 Culturii St., 5th Floor, 4800 Baia Mare, RO
// Phone/Fax: +40-62-215023
// E-mail: office@interscope.ro
//
// $Author: Levente Farkas $
// $Date: 5/12/98 11:50p $
// $Modtime: 4/27/98 6:50a $
// $Revision: 41 $
// $Archive: /Interscope/Thebe/InstallMaster/Registry.cpp $
// $Workfile: Registry.cpp $
//-----------------------------------------------------------------------
#ifdef __STDAFX__
#include "StdAfx.H"
#endif
#include "Portable.H"
#include "AssertX.H"
#include "Registry.Hpp"
//--- Debugee --------------------------------------------------------------
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#ifdef __MFC__
#define new DEBUG_NEW
#endif // __MFC__
#endif // _DEBUG
//--- Miscellaneous -----------------------------------------------------
const int MAX_REG_MACHINE_NAME_LEN = 50;
//-----------------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Create a new registry key object, butdo not attach it 2 any key yet
//-----------------------------------------------------------------------
CRegistry::CRegistry():
m_hKey(NULL),
m_bStatus(FALSE)
{
}
//-----------------------------------------------------------------------
// Pre :
// Post : Set the status 2 TRUE if opened OK
// Globals :
// I/O :
// Task : Open a registry key
//-----------------------------------------------------------------------
CRegistry::CRegistry(HKEY hKey, // A previously open key or a section
LPCTSTR lpcszSubKey, // Path relative 2 hKey
REGSAM dwDesiredSecurityAccessMask, // Desired open/create access
BOOL bAllowCreate, // Create new, if does not exist
DWORD dwOptions, // New key options
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // New key security
LPDWORD lpdwDisposition): // Cretion disposition (created or opened)
m_hKey(NULL),
m_bStatus(FALSE)
{
Open(hKey,lpcszSubKey,dwDesiredSecurityAccessMask,bAllowCreate,dwOptions,lpSecurityAttributes,lpdwDisposition);
}
//-----------------------------------------------------------------------
// Pre :
// Post : Set the status 2 TRUE if opened OK
// Globals :
// I/O :
// Task : Open a registry key
//-----------------------------------------------------------------------
BOOL CRegistry::Open(HKEY hKey, // A previously open key or a section
LPCTSTR lpcszSubKey, // Path relative 2 hKey
REGSAM dwDesiredSecurityAccessMask, // Desired open/create access
BOOL bAllowCreate, // Create new, if does not exist
DWORD dwOptions, // New key options
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // New key security
LPDWORD lpdwDisposition) // Creation disposition (created or opened)
{
// If already attached 2 a key, close it now
Close();
#ifdef _DEBUG
// Store the key's parent and the key's path
m_hKeyParent =hKey;
STRNCPY(m_hKeyPath,lpcszSubKey,sizeof(m_hKeyPath)/sizeof(TCHAR));
#endif
// Attempt 2 open specified key
m_nErrorCode =RegOpenKeyEx(hKey,lpcszSubKey,0,dwDesiredSecurityAccessMask,&m_hKey);
if(m_nErrorCode == ERROR_SUCCESS)
{
m_bStatus=TRUE;
if(lpdwDisposition)
*lpdwDisposition =REG_OPENED_EXISTING_KEY;
}
if(!m_bStatus && bAllowCreate)
{
// Could not open key, probably inexistent
// Attempt 2 create it
DWORD operation;
m_nErrorCode =RegCreateKeyEx(hKey,lpcszSubKey,0,NULL,dwOptions,dwDesiredSecurityAccessMask,lpSecurityAttributes,&m_hKey,&operation);
if(m_nErrorCode == ERROR_SUCCESS)
{
m_bStatus=TRUE;
if(lpdwDisposition)
*lpdwDisposition =operation;
}
}
return m_bStatus;
}
//---------------------------------------------------------------------------
// Pre :
// Post : Returns size in bytes, -1 on error
// Globals :
// I/O :
// Task : Get the size of the specified value
//---------------------------------------------------------------------------
DWORD CRegistry::GetValueSize(LPCTSTR name)
{
if(!m_bStatus)
return (DWORD)-1;
DWORD value_size;
if(!GetValue(name,NULL,value_size))
return (DWORD)-1;
return value_size;
}
//-----------------------------------------------------------------------
// Pre : data_size must be initialized before this call 2 the size of
// the buffer where you expect data 2 be returned
// Post : Return TRUE on success
// Globals :
// I/O :
// Task : Extract a value from current key
//-----------------------------------------------------------------------
BOOL CRegistry::GetValue(LPCTSTR name, BYTE *data, DWORD &data_size, DWORD *type)
{
if(!m_bStatus)
return FALSE;
m_nErrorCode=RegQueryValueEx(m_hKey,name,NULL,type,data,&data_size);
return m_nErrorCode==ERROR_SUCCESS;
}
//-----------------------------------------------------------------------
// Pre : buffsize must be initialized before this call 2 the size of
// the buffer where you expect data 2 be returned
// Post : Return TRUE on success
// Globals :
// I/O :
// Task : Extract default value from current key
//-----------------------------------------------------------------------
BOOL CRegistry::GetDefaultValue(LPTSTR def_value_buff, DWORD &buffsize)
{
if(!m_bStatus)
return FALSE;
m_nErrorCode=RegQueryValueEx(m_hKey,NULL,0,NULL,(BYTE *)def_value_buff,&buffsize);
return m_nErrorCode==ERROR_SUCCESS;
}
//-----------------------------------------------------------------------
// Pre :
// Post : Return TRUE on success
// Globals :
// I/O :
// Task : Set a value of the current key
//-----------------------------------------------------------------------
BOOL CRegistry::SetValue(LPCTSTR name, const BYTE *data, DWORD data_size, DWORD type)
{
if(!m_bStatus)
return FALSE;
m_nErrorCode=RegSetValueEx(m_hKey,name,0,type,data,data_size);
return m_nErrorCode==ERROR_SUCCESS;
}
//-----------------------------------------------------------------------
// Pre :
// Post : Return TRUE on success
// Globals :
// I/O :
// Task : Set default value of current key
//-----------------------------------------------------------------------
BOOL CRegistry::SetDefaultValue(LPCTSTR def_value)
{
if(!m_bStatus)
return FALSE;
m_nErrorCode=RegSetValueEx(m_hKey,NULL,0,REG_SZ,(BYTE *)def_value,lstrlen(def_value)+1);
return m_nErrorCode==ERROR_SUCCESS;
}
//-----------------------------------------------------------------------
// Pre :
// Post : Return TRUE on success
// Globals :
// I/O :
// Task : Deletete specified value of current key
//-----------------------------------------------------------------------
BOOL CRegistry::DeleteValue(LPCTSTR name)
{
if(!m_bStatus)
return FALSE;
m_nErrorCode=RegDeleteValue(m_hKey,name);
return m_nErrorCode==ERROR_SUCCESS;
}
//-----------------------------------------------------------------------
// Pre :
// Post : Return TRUE on success
// Globals :
// I/O :
// Task : Enumerate subkeys of the current key
// You can use this by calling CountSubKeys 2 get the number of
// subkeys, then calling this member with 0-based indexes of subkey
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -