📄 registry.cpp
字号:
//------------------------------------------------------------------------------
//$Workfile: Registry.cpp $
//$Header: /DevNet/SbjCore/SbjCore/Sys/Registry.cpp 2 4/19/07 11:36a Steve $
//
// Stingray Software Extension Classes
// Copyright (C) 1995 Stingray Software Inc.
// All Rights Reserved
//
// This source code is only intended as a supplement to the
// Stingray Extension Class product.
// See the SEC help files for detailed information
// regarding using SEC classes.
//
//
//
//$Revision: 2 $
//
//@doc
//
//@module Module | Module Description
//
//-----------------------------------------------------------------------------
#include "stdafx.h"
#ifdef UNDER_CE
#define ZeroMemory(Destination,Length) memset((Destination),0,(Length))
#endif //UNDER_CE
#include "Registry.h"
namespace SbjCore
{
namespace Sys
{
IMPLEMENT_DYNAMIC( Registry, CObject );
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
#define new DEBUG_NEW
#if defined(WIN32)
// The following static data members are not accessed
// correctly when compiled as an extension dll. Please
// use the corresponding constants defined in WINREG.H
//HKEY Registry::hKeyLocalMachine = HKEY_LOCAL_MACHINE;
//HKEY Registry::hKeyClassesRoot = HKEY_CLASSES_ROOT;
//HKEY Registry::hKeyUsers = HKEY_USERS;
//HKEY Registry::hKeyCurrentUser = HKEY_CURRENT_USER;
#endif
//@doc Registry
//@mfunc Constructs a Registry object.
//@xref <c Registry>
Registry::Registry()
{
// Warning: Initialize is a virtual method. Calling virtual methods in
// constructors is not a good idea if the method is expected to be
// overridden in a derived class.
// Rob 1/99
Initialize();
}
//@doc Registry
//@mfunc Destructor.
//@xref <c Registry>
Registry::~Registry()
{
if ( m_hRegistry != (HKEY) NULL || m_hKey != (HKEY) NULL )
{
Close();
}
Initialize();
}
//@doc Registry
//@mfunc Initializes data members.
//@rdesc void
//@comm This method is only intended to be called in the implementation of
// the Registry class. Override this method to perform custom
// initialization.
//@xref <c Registry>
void Registry::Initialize( void )
{
ASSERT_VALID( this );
/*
** Make sure everything is zeroed out
*/
m_strClassName.Empty();
m_strComputerName.Empty();
m_strKeyName.Empty();
m_strRegistryName.Empty();
// default initialization
m_hRegistry = (HKEY) NULL;
m_hKey = (HKEY) NULL;
m_bRemote = FALSE;
m_lErrorCode = 0L;
m_dwNumberOfSubkeys = 0;
m_bCloseKeyOnDisconnect = TRUE;
#ifdef WIN32
m_dwLongestSubkeyNameLength = 0;
m_dwLongestClassNameLength = 0;
m_dwNumberOfValues = 0;
m_dwLongestValueNameLength = 0;
m_dwLongestValueDataLength = 0;
m_dwSecurityDescriptorLength = 0;
m_fileTimeLastWrite.dwLowDateTime = 0;
m_fileTimeLastWrite.dwHighDateTime = 0;
#endif
}
#ifdef WIN32 // WIN32 methods
//@doc Registry
//@mfunc Closes the Registry object connection
//@rdesc Nonzero if successful, otherwise 0.
//@comm Releases the handle of the Registry current key.
// Encapsulates the RegCloseKey API. This method is available
// in both 16 and 32 bit versions.
//@xref <c Registry> <mf Registry::Open>
BOOL Registry::Close( void )
{
ASSERT_VALID( this );
BOOL bSuccess = TRUE;
if ( (m_hKey != (HKEY)NULL) && (m_hKey != m_hRegistry))
{
m_lErrorCode = ::RegCloseKey( m_hKey );
if ( m_lErrorCode != ERROR_SUCCESS )
bSuccess = FALSE;
}
m_hKey = (HKEY)NULL;
if ( m_hRegistry != (HKEY)NULL )
{
if (m_bCloseKeyOnDisconnect)
{
m_lErrorCode = ::RegCloseKey( m_hRegistry );
if ( m_lErrorCode != ERROR_SUCCESS )
bSuccess = FALSE;
}
m_hRegistry = (HKEY) NULL;
}
Initialize();
return bSuccess;
}
#ifndef UNDER_CE
//@doc Registry
//@mfunc Establishes a connection with the registry.
//@rdesc Nonzero if a connection to the key was established, otherwise 0.
//@comm This method is called as the first step in accessing the registry.
// It connects to an existing key in the registry on the
// local or remote computer named by lpszComputerName. If
// lpszComputerName is NULL or contains an empty string,
// the local system is assumed.
//
//Once a connection to the registry is established, use the
// <mf Registry::Open> method to open keys in the registry.
//@devnote 32-bit only. Also, if connecting to a remote registry,
// and the name of the remote system cannot be found, this method
// will not return until a time-out occurs.
//@parm HKEY | hKeyToOpen | The handle of the registry key to connect to.
//@parm LPCTSTR | lpszComputerName | Name of the computer to connect to.
//@xref <c Registry> <mf Registry::Open> <mf Registry::Close>
//@ex | Registry reg;
//BOOL rc = reg.Connect(HKEY_LOCAL_MACHINE, "RemoteSystemName");
BOOL Registry::Connect( HKEY hKeyToOpen, LPCTSTR lpszComputerName,BOOL bCloseKeyOnDisconnect)
{
ASSERT_VALID( this );
/*
** lpszComputerName can be NULL
*/
if ( m_hRegistry != (HKEY) NULL || m_hKey != (HKEY) NULL )
{
Close();
}
BOOL bSaveCloseKey = m_bCloseKeyOnDisconnect;
m_bCloseKeyOnDisconnect = bCloseKeyOnDisconnect;
m_bRemote = ((NULL != lpszComputerName) && (_tcslen(lpszComputerName)));
if ( hKeyToOpen == HKEY_CLASSES_ROOT || hKeyToOpen == HKEY_CURRENT_USER )
{
if (m_bRemote)
{
// NT won't allow you to connect to these hives via RegConnectRegistry
// on a remote system, so we'll just skip that step
m_lErrorCode = ERROR_INVALID_HANDLE;
}
else
{
m_hRegistry = hKeyToOpen;
m_hKey = m_hRegistry;
m_lErrorCode = ERROR_SUCCESS;
}
}
else
{
m_lErrorCode = ::RegConnectRegistry( (TCHAR *) lpszComputerName, hKeyToOpen, &m_hRegistry );
m_hKey = m_hRegistry;
}
if ( ERROR_SUCCESS == m_lErrorCode )
{
if (!m_bRemote)
{
TCHAR szComputerName[ MAX_PATH ] = _T("");
DWORD dwSize = MAX_PATH;
m_strComputerName.Empty();
if ( ::GetComputerName( szComputerName, &dwSize ) == TRUE )
{
m_strComputerName = szComputerName;
}
}
else
{
m_strComputerName = lpszComputerName;
}
// Derive the root key string
KeyToStr(hKeyToOpen,m_strRegistryName);
return( TRUE );
}
else
{
// if unsuccessful, do not allow state to change
m_bCloseKeyOnDisconnect = bSaveCloseKey;
return( FALSE );
}
}
#else
// This is a replacement version of the Connect method
// for CE that just calls the Open method
BOOL Registry::Connect( HKEY hKeyToOpen, LPCTSTR lpszComputerName,BOOL bCloseKeyOnDisconnect)
{
ASSERT_VALID( this );
if ( m_hRegistry != (HKEY) NULL || m_hKey != (HKEY) NULL )
{
Close();
}
BOOL bSaveCloseKey = m_bCloseKeyOnDisconnect;
m_bCloseKeyOnDisconnect = bCloseKeyOnDisconnect;
m_bRemote = FALSE;
if ( hKeyToOpen == HKEY_CLASSES_ROOT || hKeyToOpen == HKEY_CURRENT_USER )
{
m_hRegistry = hKeyToOpen;
m_hKey = m_hRegistry;
m_lErrorCode = ERROR_SUCCESS;
}
else
{
if ((hKeyToOpen == HKEY_LOCAL_MACHINE) || (hKeyToOpen == HKEY_USERS))
{
m_lErrorCode = ::RegOpenKeyEx( hKeyToOpen,NULL,0,0, &m_hRegistry );
m_hKey = m_hRegistry;
}
else
{
m_lErrorCode = ERROR_INVALID_HANDLE;
}
}
if ( ERROR_SUCCESS == m_lErrorCode )
{
TCHAR szComputerName[ MAX_PATH ] = _T("");
// CE REIMPL
// Get the local device name as the computer name
m_strComputerName = szComputerName;
// Derive the root key string
KeyToStr(hKeyToOpen,m_strRegistryName);
return( TRUE );
}
else
{
// if unsuccessful, do not allow state to change
m_bCloseKeyOnDisconnect = bSaveCloseKey;
return( FALSE );
}
}
#endif //UNDER_CE (WindowsCE)
//@doc Registry
//@mfunc Creates key in registry.
//@rdesc Nonzero if subkey was successfully created, otherwise 0.
//@syntax Create( LPCTSTR lpszSubkeyName,
// LPCTSTR lpszClassName, CreateOptions options,
// CreatePermissions permissions, LPSECURITY_ATTRIBUTES pSecurityAttributes,
// CreationDisposition * pDisposition);
//@syntax Create(LPCTSTR lpszSubkeyName, LPCTSTR lpszClassName);
//@parm LPCTSTR | lpszSubkeyName | (32-bit) Name of the subkey to create.
//@parm LPCTSTR | name_of_subkey | (16-bit) Name of the subkey to create.
//@parm LPCTSTR | lpszClassName | Name of the class to associate
// with the created key.
//@parm CreateOptions | options | Specifies special options for
// the created key. See Comments for more information.
//@parm CreatePermissions | permissions | Specifies access permissions
// for the key. See Comments for more information.
//@parm LPSECURITY_ATTRIBUTES | pSecurityAttributes | Points to a
// SECURITY_ATTRIBUTES structure containing security attributes for
// the new key.
//@parm CreationDisposition * | pDisposition | Points to a variable to
// receive disposition information. See Comments for further information.
//@comm Creates a new registry subkey. Based on RegCreateKey API for 16-bit,
// and RegCreateKeyEx for 32-bit. Possible values for options include :
//@flag REG_OPTION_VOLATILE | This key is volatile; the information is stored
// in memory and is not preserved when the system is restarted. The
// RegSaveKey function does not save volatile keys.
//@flag REG_OPTION_NON_VOLATILE | This key is not volatile; the information
// is stored in a file and is preserved when the system is restarted.
// The RegSaveKey function saves keys that are not volatile.
//
//Possible values for permissions include:
//
//@flag KEY_ALL_ACCESS | Combination of KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS,
// KEY_NOTIFY, KEY_CREATE_SUB_KEY, KEY_CREATE_LINK, and KEY_SET_VALUE access.
//@flag KEY_CREATE_LINK | Permission to create a symbolic link.
//@flag KEY_CREATE_SUB_KEY | Permission to create subkeys.
//@flag KEY_ENUMERATE_SUB_KEYS | Permission to enumerate subkeys.
//@flag KEY_EXECUTE | Permission for read access.
//@flag KEY_NOTIFY | Permission for change notification.
//@flag KEY_QUERY_VALUE | Permission to query subkey data.
//@flag KEY_READ | Combination of KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS,
// and KEY_NOTIFY access.
//@flag KEY_SET_VALUE | Permission to set subkey data.
//@flag KEY_WRITE | Combination of KEY_SET_VALUE and KEY_CREATE_SUB_KEY
// access.
//
//Possible return values for pDisposition include:
//
//@flag REG_CREATED_NEW_KEY | The key did not exist and was created.
//@flag REG_OPENED_EXISTING_KEY | The key existed and was simply opened
// without being changed.
//@xref <c Registry> <mf Registry::Open> <mf Registry::Close>
BOOL Registry::Create( LPCTSTR lpszSubkeyName,
LPCTSTR lpszClassName,
CreateOptions options,
CreatePermissions permissions,
LPSECURITY_ATTRIBUTES pSecurityAttributes,
CreationDisposition * pDisposition
)
{
ASSERT_VALID( this );
ASSERT( lpszSubkeyName != NULL );
if ( NULL == lpszSubkeyName )
{
m_lErrorCode = ERROR_INVALID_PARAMETER;
return( FALSE );
}
DWORD disposition = 0;
if ( NULL == lpszClassName )
{
lpszClassName = _T("");
}
CString strSubkey(lpszSubkeyName);
NormalizeKey(strSubkey);
// The key handle (can) be equal to the registry handle.
// If this is the case, closing the key also closes the
// registry, so we should avoid this.
if ( (m_hKey != (HKEY) NULL) && (m_hKey != m_hRegistry) ) {
m_lErrorCode = ::RegCloseKey(m_hKey);
ASSERT( ERROR_SUCCESS == m_lErrorCode );
}
m_lErrorCode = ::RegCreateKeyEx( m_hRegistry,
strSubkey,
(DWORD) 0,
(TCHAR *) lpszClassName,
options,
permissions,
pSecurityAttributes,
&m_hKey,
&disposition );
if ( ERROR_SUCCESS == m_lErrorCode )
{
if ( pDisposition != NULL )
{
*pDisposition = (CreationDisposition) disposition;
}
m_strKeyName = lpszSubkeyName;
return( TRUE );
}
else
{
return( FALSE );
}
}
//@doc Registry
//@mfunc Deletes the named registry value of the current key.
//@syntax (32-bit) BOOL DeleteKey( LPCTSTR lpszKeyToDelete, BOOL bRecursive);
//@syntax (16-bit) BOOL DeleteKey( LPCTSTR lpszKeyToDelete);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -