📄 registry.cpp
字号:
//@xref <c Registry> <mf Registry::Close>
BOOL Registry::Open( LPCTSTR lpszSubkey, const CreatePermissions security_access_mask )
{
ASSERT_VALID( this );
/*
** lpszSubkey can be NULL
*/
// The registry key member can be null at this point. If so,
// use the default registry key (HKEY_CLASSES_ROOT)
if ((HKEY) NULL == m_hRegistry )
m_hRegistry = HKEY_CLASSES_ROOT;
// 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 );
}
CString strSubkey(lpszSubkey);
NormalizeKey(strSubkey);
m_lErrorCode = ::RegOpenKeyEx( m_hRegistry, strSubkey, NULL, security_access_mask, &m_hKey );
if ( ERROR_SUCCESS == m_lErrorCode )
{
QueryInfo();
m_strKeyName = lpszSubkey;
return( TRUE );
}
return( FALSE );
}
//@doc Registry
//@mfunc Queries for information about the currently opened key
//@rdesc Nonzero if successful, othewise 0.
//@comm Queries for information about the currently opened key.
// The following public members are updated to reflect the query:
//
//m_strClassName class name<nl>
//m_dwNumberOfSubkeys number of subkeys<nl>
//m_dwLongestSubkeyName length of longest subkey name<nl>
//m_dwLongestClassNameLength length of longest subkey class name<nl>
//m_dwNumberOfValues number of subkey values<nl>
//m_dwLongestValueNameLength length of longest value name<nl>
//m_dwLongestValueDataLength longest value data length<nl>
//m_dwSecurityDescription security descriptor length<nl>
//m_fileTimeLastWrite last write time of buffer<nl>
//@devnote For 32-bit only.
//@xref <c Registry> <mf Registry::QueryValue>
BOOL Registry::QueryInfo( void )
{
ASSERT_VALID( this );
TCHAR szClassName[ 2048 ];
DWORD dwSizeClassName = sizeof( szClassName ) - 1;
#ifndef UNDER_CE
#if defined(WIN32)
::ZeroMemory( szClassName, sizeof( szClassName ) );
#endif
#endif //UNDER_CE (WindowsCE)
m_lErrorCode = ::RegQueryInfoKey( m_hKey,
szClassName,
&dwSizeClassName,
(LPDWORD) NULL,
&m_dwNumberOfSubkeys,
&m_dwLongestSubkeyNameLength,
&m_dwLongestClassNameLength,
&m_dwNumberOfValues,
&m_dwLongestValueNameLength,
&m_dwLongestValueDataLength,
&m_dwSecurityDescriptorLength,
&m_fileTimeLastWrite );
if ( ERROR_SUCCESS == m_lErrorCode )
{
m_strClassName = szClassName;
m_timeLastWrite = CTime( m_fileTimeLastWrite );
return( TRUE );
}
return( FALSE );
}
//@doc Registry
//@mfunc Encapsulates the RegQueryValueEx API.
//@syntax (32-bit) BOOL QueryValue( LPCTSTR lpszValueName,
// KeyValueTypes& value_type,
// LPBYTE lpbBuffer,
// DWORD& dwBufferSize );
//@syntax (16-bit) BOOL QueryValue( LPCTSTR lpszSubkeyName,
// SECPBYTE lpbBuffer,
// LONG& lBufferSize );
//@rdesc Nonzero if value was successfully queried, otherwise 0.
//@comm Queries the value of the currently opened registry key and copies
// the value data into the buffer. Encapsulates the RegQueryValue API.
//@parm LPCTSTR | lpszValueName | Name of value to query.
//@parm LPCTSTR | lpszSubkeyName | name of subkey to query.
//@parm KeyValueTypes& | value_type | Buffer to hold value type.
//@parm LPBYTE | lpbBuffer | Buffer to hold value data.
//@parm DWORD& | dwBufferSize | Size of buffer pointed to by lpbBuffer;
//@parm DWORD& | dwBufferSize | Size of buffer pointed to by lpbBuffer;
// on return contains size of data actually copied to buffer.
//@parm LONG& | lBufferSize | Size of buffer pointed to by lpbBuffer;
// on return contains size of data actually copied to buffer.
//@xref <c Registry> <mf Registry::QueryInfo>
BOOL Registry::QueryValue( LPCTSTR lpszValueName,
KeyValueTypes& value_type,
LPBYTE lpbBuffer,
DWORD& dwBufferSize )
{
ASSERT_VALID( this );
ASSERT( lpszValueName != NULL );
/*
** lpbBuffer and dwBufferSize can be NULL
*/
if ( NULL == lpszValueName )
{
m_lErrorCode = ERROR_INVALID_PARAMETER;
return( FALSE );
}
DWORD dwTempDataType = value_type;
m_lErrorCode = ::RegQueryValueEx( m_hKey,
(TCHAR *) lpszValueName,
NULL,
&dwTempDataType,
lpbBuffer,
&dwBufferSize );
if ( ERROR_SUCCESS == m_lErrorCode )
{
value_type = (KeyValueTypes) dwTempDataType;
return( TRUE );
}
return( FALSE );
}
#ifndef UNDER_CE
//@doc Registry
//@mfunc Encapsulates the RegReplaceKey API.
//@rdesc Nonzero if value was successfully replaced, otherwise 0.
//@parm LPCTSTR | lpszSubkeyName | Name of subkey to replace.
//@parm LPCTSTR lpszNewFile Name of the file storing registration information.
//@parm LPCTSTR lpszBackupFile Name of the file to store a backup of the key being
//@comm Replaces the subkey data with the information contained in the lpszNewFile.
// Encapsulates the RegReplaceKey API.
//@devnote For 32-bit only.
//@xref <c Registry> <mf Registry::Load> <mf Registry::UnLoad>
// <mf Registry::Restore>
BOOL Registry::Replace( LPCTSTR lpszSubkeyName,
LPCTSTR lpszNewFile,
LPCTSTR lpszBackupFile )
{
ASSERT_VALID( this );
ASSERT( lpszSubkeyName != NULL );
ASSERT( lpszNewFile != NULL );
ASSERT( lpszBackupFile != NULL );
if ( NULL == lpszSubkeyName ||
NULL == lpszNewFile ||
NULL == lpszBackupFile )
{
m_lErrorCode = ERROR_INVALID_PARAMETER;
return( FALSE );
}
m_lErrorCode = ::RegReplaceKey( m_hKey,
lpszSubkeyName,
lpszNewFile,
lpszBackupFile );
if ( ERROR_SUCCESS == m_lErrorCode )
{
return( TRUE );
}
return( FALSE );
}
//@doc Registry
//@mfunc Encapsulates the RegRestoreKey API.
//@rdesc Nonzero if key was successfully restored, otherwise 0.
//@parm LPCTSTR | lpszSavedTreeFile | Name of file with registry information.
//@parm const DWORD | dwVolatilityFlags | Specifies volatility of the key.
//@comm Overwrites the current registry key with the information contained in the saved tree file.
// Encapsulates the RegRestoreKey API.
//@devnote For 32-bit only.
//@xref <c Registry> <mf Registry::Load> <mf Registry::Replace>
BOOL Registry::Restore( LPCTSTR lpszSavedTreeFile, const DWORD dwVolatilityFlags )
{
ASSERT_VALID( this );
ASSERT( lpszSavedTreeFile != NULL );
if ( NULL == lpszSavedTreeFile )
{
m_lErrorCode = ERROR_INVALID_PARAMETER;
return( FALSE );
}
m_lErrorCode = ::RegRestoreKey( m_hKey,
lpszSavedTreeFile,
dwVolatilityFlags );
if ( ERROR_SUCCESS == m_lErrorCode )
{
return( TRUE );
}
return( FALSE );
}
//@doc Registry
//@mfunc Encapsulates the RegSaveKey API.
//@rdesc Nonzero if key was successfully saved, otherwise 0.
//@comm Saves the registration information for the current key.
// Encapsulates the RegSaveKey API.
//@devnote For 32-bit only.
//@parm LPCTSTR | lpszDestFile | Specifies the file where
// registration information associated with the current key will be stored.
//@parm LPSECURITY_ATTRIBUTES | pSecurityAttributes | Points to a
// SECURITY_ATTRIBUTES structure for the new file.
//@xref <c Registry> <mf Registry::Load> <mf Registry::Replace>
// <mf Registry::Restore>
BOOL Registry::Save( LPCTSTR lpszDestFile, LPSECURITY_ATTRIBUTES pSecurityAttributes )
{
ASSERT_VALID( this );
ASSERT( lpszDestFile != NULL );
if ( NULL == lpszDestFile )
{
m_lErrorCode = ERROR_INVALID_PARAMETER;
return( FALSE );
}
m_lErrorCode = ::RegSaveKey( m_hKey, lpszDestFile, pSecurityAttributes );
if ( ERROR_SUCCESS == m_lErrorCode )
{
return( TRUE );
}
return( FALSE );
}
#endif //UNDER_CE (WindowsCE)
//@doc Registry
//@mfunc Sets a value for the key name specified as REG_BINARY.
//@rdesc Nonzero if successful, otherwise 0.
//@parm LPCTSTR | lpszValueName | The value name.
//@parm const CByteArray& | bytes_to_write | The binary data to set.
//@devnote For 32-bit only.
//@xref <c Registry> <mf Registry::SetValue> <mf Registry::GetBinaryValue>
BOOL Registry::SetBinaryValue( LPCTSTR lpszValueName, const CByteArray& bytes_to_write )
{
ASSERT_VALID( this );
ASSERT( lpszValueName != NULL );
if ( NULL == lpszValueName )
{
m_lErrorCode = ERROR_INVALID_PARAMETER;
return( FALSE );
}
DWORD dwBufferSize = (DWORD)bytes_to_write.GetSize();
LPBYTE lpbMemoryBuffer = (LPBYTE) ::malloc( dwBufferSize );
if ( NULL == lpbMemoryBuffer )
{
m_lErrorCode = ::GetLastError();
return( FALSE );
}
DWORD dwIndex = 0;
while( dwIndex < dwBufferSize )
{
lpbMemoryBuffer[ dwIndex ] = bytes_to_write[ dwIndex ];
dwIndex++;
}
BOOL bReturn = SetValue( lpszValueName, typeBinary, lpbMemoryBuffer, dwBufferSize );
::free( lpbMemoryBuffer );
return( bReturn );
}
//@doc Registry
//@mfunc Sets a value for the key name specified as REG_DWORD.
//@rdesc Nonzero if successful, otherwise 0.
//@devnote For 32-bit only.
//@parm LPCTSTR | lpszValueName | The value name.
//@parm DWORD | dwValue | The value to set.
//@xref <c Registry> <mf Registry::SetValue> <mf Registry::GetDoubleWordValue>
BOOL Registry::SetDoubleWordValue( LPCTSTR lpszValueName, DWORD dwValue )
{
ASSERT_VALID( this );
ASSERT( lpszValueName != NULL );
if ( NULL == lpszValueName )
{
m_lErrorCode = ERROR_INVALID_PARAMETER;
return( FALSE );
}
return( SetValue( lpszValueName, typeDoubleWord, (const PBYTE) &dwValue, sizeof( DWORD ) ) );
}
#ifndef UNDER_CE
//@doc Registry
//@mfunc Encapsulates the RegSetKeySecurity API.
//@rdesc Nonzero if security was successfully set, otherwise 0.
//@parm const SECURITY_INFORMATION& | SecurityInformation | Descriptor structure
// of what security information is being set
//@parm const PSECURITY_DESCRIPTOR | pSecurityDescriptor | Points to the
// security attributes being set for the currently open key.
//@comm Sets the security for the currently open registry key.
// Encapsulates the RegSetKeySecurity API.
//@devnote For 32-bit only.
//@xref <c Registry> <mf Registry::SetValue>
BOOL Registry::SetSecurity( const SECURITY_INFORMATION& SecurityInformation,
const PSECURITY_DESCRIPTOR pSecurityDescriptor )
{
ASSERT_VALID( this );
ASSERT( pSecurityDescriptor != NULL );
if ( NULL == pSecurityDescriptor )
{
m_lErrorCode = ERROR_INVALID_PARAMETER;
return( FALSE );
}
m_lErrorCode = ::RegSetKeySecurity( m_hKey, SecurityInformation, pSecurityDescriptor );
if ( ERROR_SUCCESS == m_lErrorCode )
{
return( TRUE );
}
return( FALSE );
}
#endif //UNDER_CE (WindowsCE)
//@doc Registry
//@mfunc Sets a value for the key name specified as REG_SZ (32-bit only).
//@rdesc Nonzero if successful, otherwise 0.
//@devnote For 32-bit only.
//@parm LPCTSTR | lpszValueName | The value name.
//@parm const CString& | string_value | The string data to set.
//@xref <c Registry> <mf Registry::SetValue> <mf Registry::SetStringArrayValue>
// <mf Registry::GetStringValue>
BOOL Registry::SetStringValue( LPCTSTR lpszValueName, const CString& string_value )
{
ASSERT_VALID( this );
ASSERT( lpszValueName != NULL );
if ( NULL == lpszValueName )
{
m_lErrorCode = ERROR_INVALID_PARAMETER;
return( FALSE );
}
return( SetValue( lpszValueName, typeString, (const PBYTE) (const TCHAR *) string_value, string_value.GetLength()*sizeof(TCHAR) + 1 ) );
}
//@doc Registry
//@mfunc Sets a value for the key name specified as REG_MULTI_SZ.
//@rdesc Nonzero if successful, otherwise 0.
//@devnote For 32-bit only.
//@parm LPCTSTR | lpszValueName | The value name.
//@parm const CStringArray& | string_array | The string data to set.
//@xref <c Registry> <mf Registry::SetValue> <mf Registry::SetStringValue>
// <mf Registry::GetStringArrayValue>
BOOL Registry::SetStringArrayValue( LPCTSTR lpszValueName, const CStringArray& string_array )
{
ASSERT_VALID( this );
ASSERT( lpszValueName != NULL );
if ( NULL == lpszValueName )
{
m_lErrorCode = ERROR_INVALID_PARAMETER;
return( FALSE );
}
DWORD dwBufferSize = 0;
/*
** Find out how big our buffer needs to be...
*/
int nIndex = 0;
int nNumStrings = (int)string_array.GetSize();
while( nIndex < nNumStrings )
{
dwBufferSize += string_array[nIndex].GetLength() + 1;
nIndex++;
}
/*
** Don't forget the second NULL needed for double null terminated strings...
*/
dwBufferSize++;
dwBufferSize *= sizeof(TCHAR);
LPBYTE lpbMemoryBuffer = (LPBYTE) ::malloc( dwBufferSize );
if ( NULL == lpbMemoryBuffer )
{
m_lErrorCode = ::GetLastError();
return( FALSE );
}
#ifndef UNDER_CE
::ZeroMemory( lpbMemoryBuffer, dwBufferSize );
#endif //UNDER_CE (WindowsCE)
/*
** OK, now add the strings to the memory buffer
*/
LPTSTR lpszString = (LPTSTR) lpbMemoryBuffer;
nIndex = 0;
int nStringLength = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -