📄 windowsregistry.cpp
字号:
/*===========================================================================
(c) Copyright 1999, Emmanuel KARTMANN, all rights reserved
===========================================================================
File : FWindowsRegistry.cpp
$Header: $
Author : Emmanuel KARTMANN
Creation : Tuesday 9/21/99 2:49:45 PM
Remake :
------------------------------- Description -------------------------------
Implementation of the CWindowsRegistry class.
------------------------------ Modifications ------------------------------
$Log: $
===========================================================================
*/
#include "WindowsRegistry.h"
// Maximum size for the write buffer (recommended in the WIN32 API: bigger
// data shouldn't be stored in the registry).
const size_t CWindowsRegistry::MaximumWriteSize = 2048;
// Array of strings and their associated base handles
CWindowsRegistry::BaseNameEntry CWindowsRegistry::m_pBaseNameMap[] = {
{ "Unknown", NULL },
{ "HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE },
{ "HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT },
{ "HKEY_USERS", HKEY_USERS },
{ "HKEY_CURRENT_USER", HKEY_CURRENT_USER },
{ "HKEY_PERFORMANCE_DATA", HKEY_PERFORMANCE_DATA },
{ "HKEY_CURRENT_CONFIG", HKEY_CURRENT_CONFIG },
{ "HKEY_DYN_DATA", HKEY_DYN_DATA },
{ NULL, NULL }
};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWindowsRegistry::CWindowsRegistry()
: m_pKey(NULL),
m_pBaseKey(NULL),
m_pSubKeyName(NULL),
m_nSecurityAccessMask(0),
m_nErrorCode(ERROR_SUCCESS)
{
}
CWindowsRegistry::CWindowsRegistry(LPCTSTR pSubKeyName, HKEY pBaseKey, REGSAM nSecurityAccessMask)
: m_pKey(NULL),
m_pBaseKey(NULL),
m_pSubKeyName(NULL),
m_nSecurityAccessMask(0),
m_nErrorCode(ERROR_SUCCESS)
{
m_nErrorCode=OpenKey(pSubKeyName, pBaseKey, nSecurityAccessMask);
}
CWindowsRegistry::~CWindowsRegistry()
{
CloseKey();
}
LONG CWindowsRegistry::OpenKey(LPCTSTR pSubKeyName, HKEY pBaseKey, REGSAM nSecurityAccessMask)
{
m_nErrorCode=ERROR_SUCCESS;
if (IsOpen() && pBaseKey != (HKEY)NULL) {
CloseKey();
}
if (*pSubKeyName == '\\') {
// It's an absolute path: get base name
while (*pSubKeyName == '\\') {
pSubKeyName++; // skip the backslash(es)
}
for (int i=0; m_pBaseNameMap[i].String!=NULL; i++) {
if (!strncmp(m_pBaseNameMap[i].String, pSubKeyName, strlen(m_pBaseNameMap[i].String)))
{
if (m_pKey!=(HKEY)NULL) {
CloseKey();
}
pBaseKey = m_pBaseNameMap[i].Handle;
pSubKeyName += strlen(m_pBaseNameMap[i].String)+1;
break;
}
}
}
DWORD ulOptions=0;
m_pBaseKey=pBaseKey;
m_pSubKeyName=pSubKeyName;
m_nSecurityAccessMask=nSecurityAccessMask;
m_nErrorCode=RegOpenKeyEx(m_pBaseKey, // handle to base key
m_pSubKeyName, // name of subkey to open
ulOptions, // reserved
m_nSecurityAccessMask, // security access mask
&m_pKey // handle of opened key
);
// Force WIN32 error (it is not set by RegOpenKeyEx).
if (m_nErrorCode!=ERROR_SUCCESS) {
SetLastError(m_nErrorCode);
}
return(m_nErrorCode);
}
LONG CWindowsRegistry::CreateKey(LPCTSTR pSubKeyName, HKEY pBaseKey, REGSAM nSecurityAccessMask)
{
m_nErrorCode=ERROR_SUCCESS;
HKEY pNewKeyHandle;
// Check parameter
if ( (pSubKeyName==NULL) || (pSubKeyName!=NULL && strlen(pSubKeyName)==0)) {
return(ERROR_INVALID_PARAMETER);
}
if (IsOpen() && pBaseKey != (HKEY)NULL) {
CloseKey();
} else {
m_pBaseKey = m_pKey; // reuse opened handle as the base
}
if (*pSubKeyName == '\\') {
// It's an absolute path: get base name
while (*pSubKeyName == '\\') {
pSubKeyName++; // skip the backslash(es)
}
for (int i=0; m_pBaseNameMap[i].String!=NULL; i++) {
if (!strncmp(m_pBaseNameMap[i].String, pSubKeyName, strlen(m_pBaseNameMap[i].String)))
{
if (pBaseKey!=(HKEY)NULL) {
CloseKey();
}
pBaseKey = m_pBaseNameMap[i].Handle;
m_pBaseKey=pBaseKey;
pSubKeyName += strlen(m_pBaseNameMap[i].String)+1;
break;
}
}
}
m_pSubKeyName=pSubKeyName;
DWORD nOptions=REG_OPTION_NON_VOLATILE;
DWORD nReserved=0;
DWORD nDisposition=0;
LPTSTR pClass="";
m_pSubKeyName=pSubKeyName;
m_nSecurityAccessMask=nSecurityAccessMask;
m_nErrorCode=RegCreateKeyEx(m_pBaseKey, // handle to base key
m_pSubKeyName, // name of subkey to open
nReserved, // reserved (must be zero)
pClass, // class of object to create
nOptions, // option flag
m_nSecurityAccessMask, // security access mask
NULL, // key security structure (NULL: use default)
&pNewKeyHandle, // handle of opened key
&nDisposition // disposition value buffer (created/open)
);
if (m_nErrorCode == ERROR_SUCCESS) {
// Immediately close the new key (avoid memory leaks)
RegCloseKey(pNewKeyHandle);
} else {
// Force WIN32 error (it is not set by RegCreateKeyEx).
SetLastError(m_nErrorCode);
}
return(m_nErrorCode);
}
LONG CWindowsRegistry::RecursivelyDeleteAllSubKeys( HKEY key_handle, LPCTSTR key_name )
{
HKEY child_key_handle = NULL;
LONG return_value = 0;
LPTSTR temporary_key_name = NULL;
return_value = RegOpenKeyEx( key_handle, key_name, NULL, KEY_ALL_ACCESS, &child_key_handle );
if ( return_value != ERROR_SUCCESS )
{
return( return_value );
}
try
{
temporary_key_name = new TCHAR[ MAX_PATH ];
}
catch( ... )
{
temporary_key_name = NULL;
}
if ( temporary_key_name == NULL )
{
return( ERROR_NOT_ENOUGH_MEMORY );
}
return_value = RegEnumKey( child_key_handle, 0, temporary_key_name, MAX_PATH );
while( return_value == ERROR_SUCCESS )
{
RecursivelyDeleteAllSubKeys( child_key_handle, temporary_key_name );
return_value = RegEnumKey( child_key_handle, 0, temporary_key_name, MAX_PATH );
}
delete [] temporary_key_name;
temporary_key_name = NULL;
RegCloseKey( child_key_handle );
return_value = RegDeleteKey( key_handle, key_name );
return( return_value );
}
LONG CWindowsRegistry::DeleteKey(LPCTSTR pSubKeyName, HKEY pKey, BOOL bRecursive)
{
m_nErrorCode=ERROR_SUCCESS;
// Check parameter
if ( (pSubKeyName==NULL) || (pSubKeyName!=NULL && strlen(pSubKeyName)==0)) {
return(ERROR_INVALID_PARAMETER);
}
if (IsOpen() && pKey != (HKEY)NULL) {
CloseKey();
}
if (*pSubKeyName == '\\') {
// It's an absolute path: get base name
while (*pSubKeyName == '\\') {
pSubKeyName++; // skip the backslash(es)
}
for (int i=0; m_pBaseNameMap[i].String!=NULL; i++) {
if (!strncmp(m_pBaseNameMap[i].String, pSubKeyName, strlen(m_pBaseNameMap[i].String)))
{
if (pKey!=(HKEY)NULL) {
CloseKey();
}
pKey = m_pBaseNameMap[i].Handle;
pSubKeyName += strlen(m_pBaseNameMap[i].String)+1;
break;
}
}
}
if (pKey != (HKEY)NULL) {
m_pKey=pKey;
} // else: reuse same handle
m_pSubKeyName=pSubKeyName;
if (bRecursive) {
m_nErrorCode=RecursivelyDeleteAllSubKeys(m_pKey, // handle to base key
m_pSubKeyName // name of subkey to open
);
} else {
m_nErrorCode = RegDeleteKey(m_pKey, m_pSubKeyName);
}
// Force WIN32 error (it is not set by default).
if (m_nErrorCode!=ERROR_SUCCESS) {
SetLastError(m_nErrorCode);
}
return(m_nErrorCode);
}
LONG CWindowsRegistry::CloseKey()
{
LONG nResult=ERROR_SUCCESS;
if (IsOpen()) {
nResult=RegCloseKey(m_pKey);
m_pKey=NULL;
}
return(nResult);
}
BOOL CWindowsRegistry::IsOpen()
{
if (m_pKey != (HKEY)NULL) {
return(TRUE);
} else {
return(FALSE);
}
}
LONG CWindowsRegistry::QueryValue(LPCTSTR pKeyName, char **KeyValue, LPDWORD pValueType)
{
LONG nResult=ERROR_SUCCESS;
DWORD nType=0;
DWORD nValueBufferSize=MaximumWriteSize;
LPBYTE pValueBuffer = new BYTE[nValueBufferSize+1];
//LPBYTE pValueBuffer = (LPBYTE)malloc( (nValueBufferSize+1)*sizeof(BYTE) );
DWORD pSupportedTypes[] = {
REG_EXPAND_SZ,
REG_SZ,
};
if (!KeyValue) {
return(ERROR_INVALID_PARAMETER);
}
// Reset value
if (*KeyValue!=NULL) {
*KeyValue=NULL;
}
// Fetch data in registry and check result type
nResult=RawQueryValue(pKeyName, pValueBuffer, &nValueBufferSize, &nType, pSupportedTypes, sizeof(pSupportedTypes) / sizeof(DWORD));
if (nResult==ERROR_SUCCESS) {
if (nValueBufferSize > 0) {
// Set the Key Value: note that the '\0' is counted in the buffer size, so we must substract one
pValueBuffer[nValueBufferSize-1]='\0';
*KeyValue = new char[nValueBufferSize+1];
if (*KeyValue) {
// We can use 'strcpy' since we put a '\0' at the end of the value buffer
strcpy(*KeyValue, (const char*)pValueBuffer);
} else {
nResult = ERROR_NOT_ENOUGH_MEMORY;
}
// Set the Key Type
if (pValueType != NULL) {
*pValueType=nType;
}
}
}
if (pValueBuffer) {
delete [] pValueBuffer;
//free(pValueBuffer);
}
return(nResult);
}
LONG CWindowsRegistry::QueryValue(LPCTSTR pKeyName, DWORD &KeyValue, LPDWORD pValueType)
{
LONG nResult=ERROR_SUCCESS;
DWORD nType=0;
DWORD nValueBufferSize=MaximumWriteSize;
LPBYTE pValueBuffer = new BYTE[nValueBufferSize+1];
DWORD pSupportedTypes[] = {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -