📄 vmregistry.cpp
字号:
{
VMTRACEINIT( "VMRegistry::Replace()" );
if ( ( NULL == lpszSubKeyName )
|| ( NULL == lpszNewDataFileName )
|| ( NULL == lpszBackupFileName ) )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
// We were passed a pointer, do not trust it
try
{
m_ErrorCode = ::RegReplaceKey( m_KeyHandle,
lpszSubKeyName,
lpszNewDataFileName,
lpszBackupFileName );
if ( ERROR_SUCCESS == m_ErrorCode )
return( true );
else
return( false );
}
catch( ... )
{
m_ErrorCode = ERROR_EXCEPTION_IN_SERVICE;
return( false );
}
}
/* end of function "Replace" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: Restore
DESCRIPTION: Function reads the registry information in a specified file
and copies it over the specified key. This registry information
may be in the form of a key and multiple levels of subkeys.
INPUT: lpszSavedTreeFile - Points to a null-terminated string containing
the name of the file with registry information. This file is
typically created by using the RegSaveKey function.
dwVolatilityFlags - Specifies a flag indicating whether the key
is volatile. (A volatile key is valid only until the next time the
system is started.) This parameter is optional; if no value is
specified, the key is not volatile. This parameter can be the
REG_WHOLE_HIVE_VOLATILE flag set. Instead of restoring the given
key, this flag causes a new, volatile (memory only) set of registry
information to be created. (A hive is a large set of registry
information, typically containing all of the pertinent information
for part of the system. For example, HKEY_LOCAL_MACHINE\Hardware
is a volatile hive.) If REG_WHOLE_HIVE_VOLATILE is specified, the
key identified by the hKey parameter must be either the HKEY_USERS
or HKEY_LOCAL_MACHINE value.
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::Restore( LPCTSTR lpszSavedTreeFile,
const DWORD dwVolatilityFlags )
{
if ( NULL == lpszSavedTreeFile )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
// We were passed a pointer, do not trust it
try
{
m_ErrorCode = ::RegRestoreKey( m_KeyHandle,
lpszSavedTreeFile,
dwVolatilityFlags );
if ( ERROR_SUCCESS == m_ErrorCode )
return( true );
else
return( false );
}
catch( ... )
{
m_ErrorCode = ERROR_EXCEPTION_IN_SERVICE;
return( false );
}
}
/* end of function "Restore" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: Save
DESCRIPTION: Saves the specified key and all of its subkeys and values
to a new file
INPUT: lpszSaveTreeFile - Points to a null-terminated string
containing the name of the file in which the specified key
and subkeys are saved.
pxSecurity - Pointer to a SECURITY_ATTRIBUTES structure that
specifies a security descriptor for the new file. If the
parameter is NULL, the file gets a default security descriptor
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::Save( LPCTSTR lpszSaveTreeFile,
LPSECURITY_ATTRIBUTES pxSecurity )
{
if ( NULL == lpszSaveTreeFile )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
// We were passed a pointer, do not trust it
try
{
m_ErrorCode = ::RegSaveKey( m_KeyHandle, lpszSaveTreeFile, pxSecurity );
if ( ERROR_SUCCESS == m_ErrorCode )
return( true );
else
return( false );
}
catch( ... )
{
m_ErrorCode = ERROR_EXCEPTION_IN_SERVICE;
return( false );
}
}
/* end of function "Save" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: SetBinaryValue
DESCRIPTION: writes binary data to a specific key
INPUT: lpszNameOfValue - the key to write to
roByteArrayToWrite - a reference to a byte array to write
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::SetBinaryValue( LPCTSTR lpszNameOfValue,
const VMByteArray& roByteArrayToWrite )
{
if ( NULL == lpszNameOfValue )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
DWORD dwBufferSize = roByteArrayToWrite.GetSize();
LPBYTE lpMemBuffer = new BYTE[ dwBufferSize ];
if ( NULL == lpMemBuffer )
{
m_ErrorCode = ::GetLastError();
return( false );
}
DWORD dwIdx = 0;
while( dwIdx < dwBufferSize )
{
lpMemBuffer[ dwIdx ] = roByteArrayToWrite.GetAt( dwIdx );
dwIdx++;
}
bool bReturn = SetValue( lpszNameOfValue, typeBinary, lpMemBuffer, dwBufferSize );
delete [] lpMemBuffer;
return( bReturn );
}
/* end of function "SetBinaryValue" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: SetDoubleWordValue
DESCRIPTION: writes a dword to any specified key
INPUT: lpszValueName - the name of the key to write to
dwToWrite - the dword to write
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::SetDoubleWordValue( LPCTSTR lpszValueName, DWORD dwToWrite )
{
if ( NULL == lpszValueName )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
return( SetValue( lpszValueName, typeDoubleWord, (const PBYTE) &dwToWrite, sizeof( DWORD ) ) );
}
/* end of function "SetDoubleWordValue" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: SetSecurity
DESCRIPTION: sets the security info for the current registry key
INPUT: pxSecurityInfo - Specifies the components of the security
descriptor to set.
pxSecurityDescriptor - Points to a SECURITY_DESCRIPTOR
structure that specifies the security attributes to set
for the specified key.
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::SetSecurity( const SECURITY_INFORMATION& pxSecurityInfo,
const PSECURITY_DESCRIPTOR pxSecurityDescriptor )
{
if ( NULL == pxSecurityDescriptor )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
// We were passed a pointer, do not trust it
try
{
m_ErrorCode = ::RegSetKeySecurity( m_KeyHandle, pxSecurityInfo, pxSecurityDescriptor );
if ( ERROR_SUCCESS == m_ErrorCode )
return( true );
else
return( false );
}
catch( ... )
{
m_ErrorCode = ERROR_EXCEPTION_IN_SERVICE;
return( false );
}
}
/* end of function "SetSecurity" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: SetStringValue
DESCRIPTION: writes a string to the registry
INPUT: lpszValueName - the key to write to
roStringToWrite - the string to write to the key
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::SetStringValue( LPCTSTR lpszValueName, const VMString& roStringToWrite )
{
if ( NULL == lpszValueName )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
return( SetValue( lpszValueName, typeString, (const PBYTE) (const char *) roStringToWrite, roStringToWrite.GetLength() + 1 ) );
}
/* end of function "SetStringValue" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: SetValue
DESCRIPTION: writes an array of bytes to a given registry key
INPUT: lpszValueName - the name of the key to write to
roBytesToWrite - any array of bytes to write
RETURNS:
*/
bool VMRegistry::SetValue( LPCTSTR lpszValueName, const VMByteArray& roBytesToWrite )
{
if ( NULL == lpszValueName )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
return( SetBinaryValue( lpszValueName, roBytesToWrite ) );
}
/* end of function "SetValue" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: SetValue
DESCRIPTION: writes a dword to the given key
INPUT: lpszValueName - the name of the key to write to
dwValue - the dword to write
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::SetValue( LPCTSTR lpszValueName, DWORD dwValue )
{
if ( NULL == lpszValueName )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
return( SetDoubleWordValue( lpszValueName, dwValue ) );
}
/* end of function "SetValue" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: SetValue
DESCRIPTION: writes a string to any given registry key
INPUT: lpszValueName - the name of the key to write to
roStringToWrite - the string to write
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::SetValue( LPCTSTR lpszValueName, const VMString& roStringToWrite )
{
if ( NULL == lpszValueName )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
return( SetStringValue( lpszValueName, roStringToWrite ) );
}
/* end of function "SetValue" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: SetValue
DESCRIPTION: writes any type of data to any given registry key
INPUT: lpszValueName - the name of the subkey to write to
xValueType - the type of data to write
pData - pointer to the data to be written
dwDataSize - the number of bytes in the pData buffer
RETURNS:
*/
bool VMRegistry::SetValue( LPCTSTR lpszValueName,
const KeyValueTypes xValueType,
const PBYTE pData,
const DWORD dwDataSize )
{
if ( ( NULL == lpszValueName ) || ( NULL == pData ) )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
// We were passed a pointer, do not trust it
try
{
m_ErrorCode = ::RegSetValueEx( m_KeyHandle,
lpszValueName,
0,
xValueType,
pData,
dwDataSize );
if ( ERROR_SUCCESS == m_ErrorCode )
return( true );
else
return( false );
}
catch( ... )
{
m_ErrorCode = ERROR_EXCEPTION_IN_SERVICE;
return( false );
}
}
/* end of function "SetValue" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: UnLoad
DESCRIPTION: Specifies the components of the security descriptor to set.
INPUT: lpszSubkeyToUnload - Points to a null-terminated string
containing the name of the subkey to be unloaded.
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::UnLoad( LPCTSTR lpszSubkeyToUnload )
{
if ( NULL == lpszSubkeyToUnload )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
// We were passed a pointer, do not trust it
try
{
m_ErrorCode = ::RegUnLoadKey( m_KeyHandle, lpszSubkeyToUnload );
if ( ERROR_SUCCESS == m_ErrorCode )
return( true );
else
return( false );
}
catch( ... )
{
m_ErrorCode = ERROR_EXCEPTION_IN_SERVICE;
return( false );
}
}
/* end of function "UnLoad" */
/*****************************************************************************/
/*****************************************************************************/
/* Check-in history */
/*
*$Log: $
*/
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -