📄 vmregistry.cpp
字号:
}
}
/* end of function "CreateNode" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: DeleteKey
DESCRIPTION: removes a key from the registry
INPUT: pchDeleteKey - the name of the key to delete
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::DeleteKey( LPCTSTR pchDeleteKey )
{
if ( NULL == pchDeleteKey )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
// We were passed a pointer, do not trust it
//
try
{
// Can not delete a key given a full path.
// so back up one level and then do a delete
//
VMString oFullKeyName = pchDeleteKey;
if ( (-1) == oFullKeyName.Find( "\\" ) )
{
// User had not given a full path so assume
// the name of the key he passed is a key
// off of the current key
//
m_ErrorCode = ::RecurseDeleteRegKeys( m_KeyHandle, pchDeleteKey );
}
else
{
UINT iPosBackSlash = oFullKeyName.GetLength() - 1;
// this loop will succeed because a back slash was
// found in the above if statement
//
const char cchBackSlash = { '\\' };
char& chCurrent = oFullKeyName.Buffer()[ iPosBackSlash ];
while( cchBackSlash != chCurrent )
{
iPosBackSlash--;
chCurrent = oFullKeyName.Buffer()[ iPosBackSlash ];
}
VMString oCurKey = m_KeyName;
VMString oParent;
oParent = oFullKeyName.Left( iPosBackSlash );
VMString oChild;
oChild = oFullKeyName.Right( ( oFullKeyName.GetLength() - iPosBackSlash ) - 1 );
// Now we open the parent key and delete the child
//
if ( true == Open( oParent ) )
m_ErrorCode = ::RecurseDeleteRegKeys( m_KeyHandle, oChild );
else
{
m_KeyName = oCurKey;
return( false );
}
}
if ( ERROR_SUCCESS == m_ErrorCode )
return( true );
else
return( false );
}
catch( ... )
{
m_ErrorCode = ERROR_EXCEPTION_IN_SERVICE;
return( false );
}
}
/* end of function "DeleteKey" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: DeleteValue
DESCRIPTION: removes a value from the registry
INPUT: lpDelete - the name of the value to delete
RETURNS: true if SUCCESSFUL, false otherwise
*/
bool VMRegistry::DeleteValue( LPCTSTR pchDelete )
{
// We were passed a pointer, do not trust it
//
try
{
m_ErrorCode = ::RegDeleteValue( m_KeyHandle, (LPTSTR) pchDelete );
if ( ERROR_SUCCESS == m_ErrorCode )
return( true );
else
return( false );
}
catch( ... )
{
m_ErrorCode = ERROR_EXCEPTION_IN_SERVICE;
return( false );
}
}
/* end of function "DeleteValue" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: EnumerateKeys
DESCRIPTION: retrieves the results of a subkey enumeration
INPUT: dwIdxSubKey - Specifies the index of the subkey to retrieve.
This parameter should be zero for the first call to the
function and then incremented for subsequent calls. Because
subkeys are not ordered, any new subkey will have an arbitrary
index. This means that the function may return subkeys in any
order
roNameSubKey - receives the name of the subkey
roClassName - receives the value of the subkey class
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::EnumerateKeys( const DWORD dwIdxSubKey,
VMString& roNameSubKey,
VMString& roClassName )
{
char chSubKey[2048];
char chClassName[2048];
DWORD dwSizeSubKey = sizeof( chSubKey ) - 1;
DWORD dwSizeClassName = sizeof( chClassName ) - 1;
::ZeroMemory( chSubKey, sizeof( chSubKey ) );
::ZeroMemory( chClassName, sizeof( chClassName ) );
m_ErrorCode = ::RegEnumKeyEx( m_KeyHandle,
dwIdxSubKey,
chSubKey,
&dwSizeSubKey,
NULL,
chClassName,
&dwSizeClassName,
&m_LastWriteTime );
if ( ERROR_SUCCESS == m_ErrorCode )
{
roNameSubKey = chSubKey;
roClassName = chClassName;
return( true );
}
else
return( false );
}
/* end of function "EnumerateKeys" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: EnumerateValues
DESCRIPTION: used to enumerate values under a subkey
INPUT: dwIdx - Specifies the index of the subkey to retrieve.
This parameter should be zero for the first call to the
function and then incremented for subsequent calls. Because
subkeys are not ordered, any new subkey will have an arbitrary
index. This means that the function may return subkeys in any
order.
roValueName - receives the value of the key associated with
the value of dwIdx
roKeyType - the type of key associated with the given index
pbBuffer - a pointer to a buffer that will hold the value of
the key
dwBufferSize - the size of the space allocated for pbBuffer
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::EnumerateValues( const DWORD dwIdx,
VMString& roValueName,
KeyValueTypes& roKeyType,
LPBYTE pbBuffer,
DWORD& dwBufferSize )
{
// pbBuffer and dwBufferSize can be NULL
//
DWORD dwTempCode = roKeyType;
char chTemp[ 2048 ];
::ZeroMemory( chTemp, sizeof( chTemp ) );
DWORD dwSizeTemp = sizeof( chTemp );
// We were passed a pointer, do not trust it
//
try
{
m_ErrorCode = ::RegEnumValue( m_KeyHandle,
dwIdx,
chTemp,
&dwSizeTemp,
NULL,
&dwTempCode,
pbBuffer,
&dwBufferSize );
if ( ERROR_SUCCESS == m_ErrorCode )
{
roKeyType = (KeyValueTypes) dwTempCode;
roValueName = chTemp;
return( true );
}
else
return( false );
}
catch( ... )
{
m_ErrorCode = ERROR_EXCEPTION_IN_SERVICE;
return( false );
}
}
/* end of function "EnumerateValues" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: Flush
DESCRIPTION: explicitely pushes all registry changes to disk
INPUT: void
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::Flush( void )
{
m_ErrorCode = ::RegFlushKey( m_KeyHandle );
if ( ERROR_SUCCESS == m_ErrorCode )
return( true );
else
return( false );
}
/* end of function "Flush" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetBinaryValue
DESCRIPTION: retrieves a value from a given key name, the value is
explicitely treated as a binary array.
INPUT: roValueName - the name of the key whose value should be
retrieved
roArrayOut - receives the data
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::GetBinaryValue( LPCTSTR roValueName, VMByteArray& roArrayOut )
{
if ( NULL == roValueName )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
DWORD dwBufferSize = m_LongestValueDataLength;
LPBYTE lpBuffer = (LPBYTE) new BYTE[ dwBufferSize ];
if ( NULL == lpBuffer )
{
m_ErrorCode = ::GetLastError();
return( false );
}
bool bReturn = true;
KeyValueTypes type = typeBinary;
if ( true == QueryValue( roValueName, type, lpBuffer, dwBufferSize ) )
{
// We've got data so give it back to the caller
//
roArrayOut.FreeAll();
DWORD index = 0;
while ( index < dwBufferSize )
{
roArrayOut.Add( lpBuffer[ index ] );
index++;
}
bReturn = true;
}
else
bReturn = false;
delete [] lpBuffer;
return( bReturn );
}
/* end of function "GetBinaryValue" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetClassName
DESCRIPTION: retrieves the name of the currently active class in this
INPUT: roClassName - receieves the data
RETURNS: void
*/
void VMRegistry::GetClassName( VMString& roClassName ) const
{
roClassName = m_ClassName;
}
/* end of function "GetClassName" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetComputerName
DESCRIPTION: retrieves the machine name this is operating on
INPUT: roHostName - receives the data
RETURNS: void
*/
void VMRegistry::GetComputerName( VMString& roHostName ) const
{
roHostName = m_ComputerName;
}
/* end of function "GetComputerName" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetDoubleWordValue
DESCRIPTION: retrieves a registry value as a dword
INPUT: lpValueName - the name of the key whose value is to be
retrieved.
dwReturn - reference to a dword that will receive the data
RETURNS: true if successful, false otherwise
*/
bool VMRegistry::GetDoubleWordValue( LPCTSTR lpValueName, DWORD& dwReturn )
{
if ( NULL == lpValueName )
{
m_ErrorCode = ERROR_INVALID_PARAMETER;
return( false );
}
DWORD dwReturnSize = sizeof( DWORD );
KeyValueTypes type = typeDoubleWord;
return( QueryValue( lpValueName, type, (LPBYTE) &dwReturn, dwReturnSize ) );
}
/* end of function "GetDoubleWordValue" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetErrorCode
DESCRIPTION: retrieves this class' last error code
INPUT: void
RETURNS: the value of this' error code
*/
bool VMRegistry::GetErrorCode( void ) const
{
return( 0 != m_ErrorCode );
}
/* end of function "GetErrorCode" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetKeyName
DESCRIPTION: retrieves the value of this' current key name
INPUT: roKeyName - receives the data
RETURNS: void
*/
void VMRegistry::GetKeyName( VMString& roKeyName ) const
{
roKeyName = m_KeyName;
}
/* end of function "GetKeyName" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetNumberOfSubkeys
DESCRIPTION: retrieves the number of subkeys this knows about
INPUT: void
RETURNS: the value of subkeys this knows about
*/
DWORD VMRegistry::GetNumberOfSubkeys( void ) const
{
return( m_NumberOfSubkeys );
}
/* end of function "GetNumberOfSubkeys" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetNumberOfValues
DESCRIPTION: retrieves the number of values this knows about
INPUT: void
RETURNS: returns the value
*/
DWORD VMRegistry::GetNumberOfValues( void ) const
{
return( m_NumberOfValues );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -