📄 registry.cpp
字号:
// -------------------------------------------------------------------
// Filename : registry.cpp
// Description : registry manipulation class
// By : John Cruise
// Date : 1996 Mar 25
// Last Modified : 1997 Jul 14
// Notes : tabstop = 4
// -------------------------------------------------------------------
// Modifications : 1996 Mar 25 John Cruise
// : none as of now
// :
// : 1997 Jul 14 John Cruise
// : modified to support exception handling
// -------------------------------------------------------------------
#pragma message( "If the target mode is not a windows application...\n" )
#pragma message( "remember to comment out the stdafx.h" )
#include "stdafx.h"
#include "registry.h"
CRegistry::CRegistry()
{
m_bIsOpen = FALSE;
}
CRegistry::CRegistry( HKEY hKey, LPCTSTR cSubKey, DWORD nOptions/*=0*/,
REGSAM samDesired/*=KEY_ALL_ACCESS*/ )
{
if(( m_nLastError=RegOpenKeyEx( hKey, cSubKey, nOptions, samDesired, &m_hKey ))
!=ERROR_SUCCESS )
throw( new CRegistryException( m_nLastError ));
m_bIsOpen = TRUE;
}
CRegistry::~CRegistry()
{
// if the registry is still open ... then close it
try {
if( m_bIsOpen )
Close();
}
catch( CRegistryException *regError ) {
regError->ReportError();
regError->Delete();
}
}
void CRegistry::Create( DWORD &nDisposition, HKEY hKey, LPCTSTR cName,
DWORD nOptions/*=REG_OPTION_NON_VOLATILE*/,
REGSAM samDesired/*=KEY_ALL_ACCESS*/,
LPSECURITY_ATTRIBUTES lpSecurityAttributes/*=NULL*/ )
{
// if open, close it
try {
if( m_bIsOpen )
Close();
}
catch( CRegistryException *regError ) {
throw( regError );
}
// create the key
if( ( m_nLastError = RegCreateKeyEx( hKey, cName, NULL, "", nOptions,
samDesired, lpSecurityAttributes, &m_hKey, &nDisposition )) !=ERROR_SUCCESS )
throw( new CRegistryException( m_nLastError ));
m_bIsOpen = TRUE;
}
void CRegistry::Open( HKEY hKey, LPCTSTR cSubKey, DWORD nOptions/*=0*/,
REGSAM samDesired/*=KEY_ALL_ACCESS*/ )
{
// if has previously open something, close it
try {
if( m_bIsOpen )
Close();
}
catch( CRegistryException *regError ) {
throw( regError );
}
// open the key
if(( m_nLastError=RegOpenKeyEx( hKey, cSubKey, nOptions, samDesired, &m_hKey ))
!=ERROR_SUCCESS )
throw( new CRegistryException( m_nLastError ));
m_bIsOpen = TRUE;
}
void CRegistry::Close()
{
if( m_bIsOpen ) {
RegFlushKey( m_hKey );
m_nLastError = RegCloseKey( m_hKey );
m_bIsOpen = FALSE;
}
else
m_nLastError = ERROR_SUCCESS;
}
void CRegistry::Remove( LPCTSTR cSubKey )
{
if(( m_nLastError=RegDeleteKey( m_hKey, cSubKey ))!=ERROR_SUCCESS )
throw( new CRegistryException( m_nLastError ));
}
void CRegistry::Write( LPSTR cValueName, CONST BYTE *pData, DWORD nSize,
DWORD nType/*=REG_SZ*/ )
{
if(( m_nLastError=RegSetValueEx( m_hKey, cValueName, NULL, nType, pData, nSize ))
!=ERROR_SUCCESS )
throw( new CRegistryException( m_nLastError ));
}
void CRegistry::Read( LPBYTE pDest, DWORD *nSize, DWORD *nType, LPSTR cValueName )
{
if(( m_nLastError=RegQueryValueEx( m_hKey, cValueName, NULL, nType, pDest, nSize ))
!=ERROR_SUCCESS )
throw( new CRegistryException( m_nLastError ));
}
LONG CRegistry::GetLastError()
{
return( m_nLastError );
}
BOOL CRegistry::EnumValue( DWORD nIndex, LPSTR szValueName, LPDWORD nValueNameLen,
LPDWORD nType/*=NULL*/,
LPBYTE lpValue/*=NULL*/,
LPDWORD nValueSize/*=NULL*/ )
{
// enumerate the values
m_nLastError = RegEnumValue( m_hKey, nIndex, szValueName, nValueNameLen,
NULL, nType, lpValue, nValueSize );
// check the return value
if( m_nLastError==ERROR_NO_MORE_ITEMS )
return( FALSE );
else if( m_nLastError!=ERROR_SUCCESS )
throw( new CRegistryException( m_nLastError ));
return( TRUE );
}
BOOL CRegistry::EnumKey( DWORD nIndex, LPSTR szKeyName, LPDWORD nKeyNameLen,
PFILETIME stTimeWrite,
LPSTR szClass/*=NULL*/,
LPDWORD nClassLen/*=NULL*/ )
{
// enumerate the keys
m_nLastError = RegEnumKeyEx( m_hKey, nIndex, szKeyName, nKeyNameLen,
NULL, szClass, nClassLen, stTimeWrite );
// check the return value
if( m_nLastError==ERROR_NO_MORE_ITEMS )
return( FALSE );
else if( m_nLastError!=ERROR_SUCCESS )
throw( new CRegistryException( m_nLastError ));
return( TRUE );
}
void CRegistry::SaveKey( LPCTSTR szFilename, LPSECURITY_ATTRIBUTES lpSecurity/*=NULL*/ )
{
// save the key
m_nLastError = RegSaveKey( m_hKey, szFilename, lpSecurity );
// check the return value
if( m_nLastError!=ERROR_SUCCESS )
throw( new CRegistryException( m_nLastError ));
}
void CRegistry::LoadKey( LPCTSTR szSubKey, LPCTSTR szFilename )
{
// load the key
m_nLastError = RegLoadKey( m_hKey, szSubKey, szFilename );
// check the return value
if( m_nLastError!=ERROR_SUCCESS )
throw( new CRegistryException( m_nLastError ));
}
void CRegistry::UnloadKey( LPCTSTR szSubKey )
{
// unload the previously loaded key
m_nLastError = RegUnLoadKey( m_hKey, szSubKey );
// check the return value
if( m_nLastError!=ERROR_SUCCESS )
throw( new CRegistryException( m_nLastError ));
}
void CRegistry::RestoreKey( LPCTSTR szFilename, DWORD nFlags/*=0*/ )
{
// restore the key from the file
m_nLastError = RegRestoreKey( m_hKey, szFilename, nFlags );
// check the return value
if( m_nLastError!=ERROR_SUCCESS )
throw( new CRegistryException( m_nLastError ));
}
//////////////////////////////////////////////////////////////////////
// CRegistryException Class
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CRegistryException::CRegistryException( UINT nError )
{
m_nError = nError;
}
CRegistryException::~CRegistryException()
{
}
int CRegistryException::ReportError(UINT nType, UINT nMessageID)
{
// format the message
LPVOID lpMsgBuf=NULL;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, \
NULL, \
m_nError, \
MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language
( LPTSTR )&lpMsgBuf, \
0, \
NULL );
CString szError;
szError.Format( "Registry Error\n\nError Code: %d\nDescription: %s", m_nError, lpMsgBuf );
// Free the buffer.
LocalFree( lpMsgBuf );
// display the error message
int nRet=AfxMessageBox( szError, nType, 0 );
// return the value that the afxMessageBox() has returned
return( nRet );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -