⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmregistry.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 4 页
字号:
}
/*	end of function "GetNumberOfValues" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	GetRegistryName

       DESCRIPTION:	returns the name of the registry this is dealing with

             INPUT: roRegistryName - receives the data
           RETURNS:	void
*/
void VMRegistry::GetRegistryName( VMString& roRegistryName ) const
{
   roRegistryName = m_RegistryName;
}
/*	end of function "GetRegistryName" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	GetSecurity

       DESCRIPTION:	returns security information for this' current key

             INPUT: xSecurityInfo - Specifies a SECURITY_INFORMATION value 
                    that indicates the requested security information. 
                    pSecurityDescriptor - Points to a buffer that receives a 
                    copy of the requested security descriptor.
                    dwBufferSize - Points to a variable that specifies the size,
                    in bytes, of the buffer pointed to by the pSecurityDescriptor 
                    parameter. When the function returns, the variable contains 
                    the number of bytes written to the buffer. 
           RETURNS:	true if successful, false otherwise
*/
bool VMRegistry::GetSecurity( const SECURITY_INFORMATION xSecurityInfo,
                            PSECURITY_DESCRIPTOR       pSecurityDescriptor,
                            DWORD&                     dwBufferSize )
{
   if ( NULL == pSecurityDescriptor )
   {
      m_ErrorCode = ERROR_INVALID_PARAMETER;
      return( false );
   }

   // We were passed a pointer, do not trust it

   try
   {
      m_ErrorCode = ::RegGetKeySecurity( m_KeyHandle,
                                         xSecurityInfo,
                                         pSecurityDescriptor,
                                         &dwBufferSize );

      if ( ERROR_SUCCESS == m_ErrorCode )
         return( true );
      else
         return( false );
   }
   catch( ... )
   {
      m_ErrorCode = ERROR_EXCEPTION_IN_SERVICE;
      return( false );
   }
}
/*	end of function "GetSecurity" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	GetStringValue

       DESCRIPTION:	retrieves a string value from the registry

             INPUT: lpValue - the name of the keys whose value should be 
                    retrieved.
                    roReturn - receives the key data
           RETURNS:	true if successful, false otherwise
*/
bool VMRegistry::GetStringValue( LPCTSTR lpValue, VMString& roReturn )
{
   if ( NULL == lpValue )
   {
      m_ErrorCode = ERROR_INVALID_PARAMETER;
      return( false );
   }

   char chTemp[ 2048 ];
   DWORD dwBufSize = 2048;

   ::ZeroMemory( chTemp, sizeof( chTemp ) );

   KeyValueTypes type = typeString;

   if ( true == QueryValue( lpValue, type, (LPBYTE) chTemp, dwBufSize ) )
   {
      roReturn = chTemp;
      return( true );
   }
   else
   {
      roReturn.Empty();
      return( false );
   }
}
/*	end of function "GetStringValue" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	GetValue

       DESCRIPTION:	provides means to get an array of bytes from a registry key

             INPUT: lpValueName - the key to retrieve the data from
                    roArrayOut - will contain the data
           RETURNS:	true if successful, false otherwise
*/
bool VMRegistry::GetValue( LPCTSTR lpValueName, VMByteArray& roArrayOut )
{
   if ( NULL == lpValueName )
   {
      m_ErrorCode = ERROR_INVALID_PARAMETER;
      return( false );
   }

   return( GetBinaryValue( lpValueName, roArrayOut ) );
}
/*	end of function "GetValue" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	GetValue

       DESCRIPTION:	provides means to get a dword from a registry key

             INPUT: lpValueName - the key to retrieve the data from
                    rdwReturn - will contain the data
           RETURNS:	true if successful, false otherwise
*/
bool VMRegistry::GetValue( LPCTSTR lpValueName, DWORD& rdwReturn )
{
   if ( NULL == lpValueName )
   {
      m_ErrorCode = ERROR_INVALID_PARAMETER;
      return( false );
   }

   return( GetDoubleWordValue( lpValueName, rdwReturn ) );
}
/*	end of function "GetValue" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	GetValue

       DESCRIPTION:	provides means to get a string from a registry key

             INPUT: lpValueName - the key to retrieve the data from
                    roReturn - will contain the data
           RETURNS:	true if successful, false otherwise
*/
bool VMRegistry::GetValue( LPCTSTR lpValueName, VMString& roReturn )
{
   if ( NULL == lpValueName )
   {
      m_ErrorCode = ERROR_INVALID_PARAMETER;
      return( false );
   }

   return( GetStringValue( lpValueName, roReturn ) );
}
/*	end of function "GetValue" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	Load

       DESCRIPTION:	function creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE 
                    and stores registration information from a specified file into 
                    that subkey. This registration information is in the form of a 
                    hive. A hive is a discrete body of keys, subkeys, and values 
                    that is rooted at the top of the registry hierarchy. A hive is 
                    backed by a single file and .LOG file.

             INPUT: lpSubkeyName - Points to a null-terminated string that specifies 
                    the name of the key to be created under hKey. This subkey is where 
                    the registration information from the file will be loaded.	
                    lpFileName - Points to a null-terminated string containing the name 
                    of a file that has registration information. This file must have 
                    been created with the RegSaveKey function. 
           RETURNS:	true if successful, false otherwise
*/
bool VMRegistry::Load( LPCTSTR lpSubkeyName, LPCTSTR lpFileName )
{
   if ( ( NULL == lpSubkeyName ) || ( NULL == lpFileName ) )
   {
      m_ErrorCode = ERROR_INVALID_PARAMETER;
      return( false );
   }

   // We were passed a pointer, do not trust it
   //
   try
   {
      m_ErrorCode = ::RegLoadKey( m_RegistryHandle, lpSubkeyName, lpFileName );

      if ( ERROR_SUCCESS == m_ErrorCode )
         return( true );
      else
         return( false );
   }
   catch( ... )
   {
      m_ErrorCode = ERROR_EXCEPTION_IN_SERVICE;
      return( false );
   }
}
/*	end of function "Load" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	NotifyChange

       DESCRIPTION:	attaches an event to the registry, so that when the registy
                    is changed the event will become signalled function notifies 
                    the caller about changes to the attributes or contents of a 
                    specified registry key. Note that the function does not notify 
                    the caller if the specified key is deleted.

             INPUT: hEvent - the event to associate with the registry
                    xFilter - type of change the registrar is interested in
                    receiving notification about.
                    bWatchSubtree - true to watch all subkeys as, false will trigger
                    only on changes in the key only 
                    bWaitOrSignal - Specifies a flag that indicates how the function
                    reports changes. If this parameter is true, the function returns 
                    immediately and reports changes by signaling the specified event. 
                    When this parameter is false, the function does not return until 
                    a change has occurred. If hEvent does not specify a valid event, 
                    this parameter cannot be true.
           RETURNS:	true if successful, false otherwise
*/
bool VMRegistry::NotifyChange( const HANDLE             hEvent, 
                             const NotifyChangeFilter xFilter,
                             const bool               bWatchSubtree,
                             const bool               bWaitOrSignal )
{
   m_ErrorCode = ::RegNotifyChangeKeyValue( m_KeyHandle,
                                            bWatchSubtree,
                                            xFilter,
                                            hEvent,
                                            bWaitOrSignal );

   if ( ERROR_SUCCESS == m_ErrorCode )
      return( true );
   else
      return( false );
}
/*	end of function "NotifyChange" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	Open

       DESCRIPTION:	opens a registry key

             INPUT: lpKeyName - the name of the key to open
                    xAccessMask -  the level of access that is being requested
           RETURNS:	true if successful, false otherwise
*/
bool VMRegistry::Open( LPCTSTR lpKeyName, 
				               const CreatePermissions xAccessMask )
{
   // We were passed a pointer, do not trust it
   //
   try
   {
      if ( (HKEY) NULL != m_KeyHandle )
      {
         ::RegCloseKey( m_KeyHandle );
         m_KeyHandle = (HKEY) NULL;
      }

      m_ErrorCode = ::RegOpenKeyEx( m_RegistryHandle, 
		                                lpKeyName, 
                  									NULL, 
									                  xAccessMask, 
									                  &m_KeyHandle );

      if ( ERROR_SUCCESS == m_ErrorCode )
      {
         QueryInfo();
         m_KeyName = lpKeyName;
         return( true );
      }
      else
         return( false );
   }
   catch( ... )
   {
      m_ErrorCode = ERROR_EXCEPTION_IN_SERVICE;
      return( false );
   }
}
/*	end of function "Open" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	QueryInfo

       DESCRIPTION:	examines this' current key and fills out a number of class
                    vars accordingly

             INPUT: void
           RETURNS:	true if successful, false otherwise, a number of class vars
                    are modified by this procedure
*/
bool VMRegistry::QueryInfo( void )
{
   char chClassName[ 2048 ];

   ::ZeroMemory( chClassName, sizeof( chClassName ) );

   DWORD dwBufSize = sizeof( chClassName ) - 1;
   
   m_ErrorCode = ::RegQueryInfoKey( m_KeyHandle,
                                    chClassName,
                                    &dwBufSize,
                                    (LPDWORD) NULL,
                                    &m_NumberOfSubkeys,
                                    &m_LongestSubkeyNameLength,
                                    &m_LongestClassNameLength,
                                    &m_NumberOfValues,
                                    &m_LongestValueNameLength,
                                    &m_LongestValueDataLength,
                                    &m_SecurityDescriptorLength,
                                    &m_LastWriteTime );

   if ( ERROR_SUCCESS == m_ErrorCode )
   {
      m_ClassName = chClassName;
      return( true );
   }
   else
      return( false );
}
/*	end of function "QueryInfo" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	QueryValue

       DESCRIPTION:	retrieves the type and data for a specified value name 
                    associated with an open registry key.

             INPUT: lpValueName - the keyname whose data will be retrieved
                    rxKeyType - type of key to open
                    lpBufferOut - retrieves the data
                    rdwBufferSize - the size of the buffer associated with
                    lpBufferSize	
           RETURNS:	true if successful, false otherwise
*/
bool VMRegistry::QueryValue( LPCTSTR        lpValueName, 
                           KeyValueTypes& rxKeyType, 
                           LPBYTE         lpBufferOut, 
                           DWORD&         rdwBufferSize )
{
   // lpBufferOut and rdwBufferSize can be NULL
   //
   if ( NULL == lpValueName )
   {
      m_ErrorCode = ERROR_INVALID_PARAMETER;
      return( false );
   }

   // We were passed a pointer, do not trust it
   //
   try
   {
      DWORD dwTemp = (DWORD) rxKeyType;

      m_ErrorCode = ::RegQueryValueEx( m_KeyHandle,
                                       (char *) lpValueName,
                                       NULL,
                                       &dwTemp,
                                       lpBufferOut,
                                       &rdwBufferSize );

      if ( ERROR_SUCCESS == m_ErrorCode )
      {
         rxKeyType = (KeyValueTypes) dwTemp;
         return( true );
      }
      else
         return( false );
   }
   catch( ... )
   {
      m_ErrorCode = ERROR_EXCEPTION_IN_SERVICE;
      return( false );
   }
}
/*	end of function "QueryValue" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	Replace

       DESCRIPTION:	Replaces the file backing a key and all its subkeys with 
                    another file, so that when the system is next started, the 
                    key and subkeys will have the values stored in the new file.

             INPUT: lpszSubKeyName - Points to a null-terminated string containing 
                    the name of a key whose subkeys and values are replaced by this 
                    function. This key must be a subkey of the key identified by the 
                    hKey parameter. This parameter can be NULL. The selected key 
                    must be the root of a hive; that is, it must be an immediate 
                    descendent of HKEY_LOCAL_MACHINE or HKEY_USERS.
                    lpszNewDataFileName - Points to a null-terminated string 
                    containing the name of the file with registration information. 
                    This file is typically created by using the RegSaveKey function.
                    lpszBackupFileName - Points to a null-terminated string containing 
                    the name of a file that receives a backup copy of the registry 
                    information being replaced.  	
           RETURNS:	true if successful, false otherwise
*/
bool VMRegistry::Replace( LPCTSTR lpszSubKeyName,
                        LPCTSTR lpszNewDataFileName,
                        LPCTSTR lpszBackupFileName )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -