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

📄 vmregistry.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/*****************************************************************************/
/*                             SOURCE FILE                                   */
/*****************************************************************************/
/*
       $Archive: $

      $Revision: $
          $Date: $
        $Author: $

   Description: 

                Some specific portions of this derivation are based on the work
                of Michael Swartzendruber and was completed prior to employment
                at Intrix. Michael Swartzendruber grants to Intrix a "full source
                license" to this specific derivative of that work, but reserves 
                the right to continue to work on other derivatives of this code
                outside of the scope and business requirements of Intrix.
*/
static char OBJECT_ID[] = "$Revision: $ : $Date: $";
/*****************************************************************************/


#include "../TraceTools/VMTrace.h"
#include "VMString.h"
#include "VMRegistry.h"




/*****************************************************************************/
/*
     FUNCTION NAME:	RecurseDeleteRegKeys

       DESCRIPTION:	given a parent key and a keyname, deletes all children of
                    the key named lpKeyName

             INPUT: hKey - the parent key
                    lpKeyName - the name of the key to delete
           RETURNS:	ERROR_SUCCESS if successful, return code otherwise
*/
static LONG RecurseDeleteRegKeys( HKEY hKey, LPCTSTR lpKeyName )
{
   HKEY hKeyChild = NULL;

   LONG lReturn = 0;

   LPTSTR lpTempKey = NULL;

   lReturn = RegOpenKeyEx( hKey, lpKeyName, NULL, KEY_ALL_ACCESS, &hKeyChild );

   if ( ERROR_SUCCESS != lReturn )
   {
      return( lReturn );
   }

   lpTempKey = new char[ MAX_PATH ];

   if ( NULL == lpTempKey )
   {
      return( ERROR_NOT_ENOUGH_MEMORY );
   }

   lReturn = RegEnumKey( hKeyChild, 0, lpTempKey, MAX_PATH );

   while( ERROR_SUCCESS == lReturn )
   {
      RecurseDeleteRegKeys( hKeyChild, lpTempKey );
      lReturn = RegEnumKey( hKeyChild, 0, lpTempKey, MAX_PATH );
   }

   delete [] lpTempKey;

   lpTempKey = NULL;

   RegCloseKey( hKeyChild );

   lReturn = RegDeleteKey( hKey, lpKeyName );

   return( lReturn );
}
/*	end of function "RecurseDeleteRegKeys" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	VMRegistry

       DESCRIPTION:	ctor. initializes this

             INPUT: void
           RETURNS:	void
*/
VMRegistry::VMRegistry()
{
   Initialize();
}
/*	end of function "VMRegistry" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	~VMRegistry

       DESCRIPTION:	dtor. clean up

             INPUT: void
           RETURNS:	void
*/
VMRegistry::~VMRegistry()
{
   if ( (HKEY) NULL != m_RegistryHandle )
      Close();

   Initialize();
}
/*	end of function "~VMRegistry" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	Initialize

       DESCRIPTION:	ctor/dtor helper. cleans up / inits all vars

             INPUT: void	
           RETURNS:	void
*/
void VMRegistry::Initialize( void )
{
   // Make sure everything is zeroed out
   //
   m_ClassName.Empty();
   m_ComputerName.Empty();
   m_KeyName.Empty();
   m_RegistryName.Empty();

   m_KeyHandle                    = (HKEY) NULL;
   m_ErrorCode                    = 0L;
   m_NumberOfSubkeys              = 0;
   m_LongestSubkeyNameLength      = 0;
   m_LongestClassNameLength       = 0;
   m_NumberOfValues               = 0;
   m_LongestValueNameLength       = 0;
   m_LongestValueDataLength       = 0;
   m_SecurityDescriptorLength     = 0;
   m_LastWriteTime.dwLowDateTime  = 0;
   m_LastWriteTime.dwHighDateTime = 0;
   m_RegistryHandle               = (HKEY) NULL;
}
/*	end of function "Initialize" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	Close

       DESCRIPTION:	closes all registry connections

             INPUT: void
           RETURNS:	true if successful, false otherwise
*/
bool VMRegistry::Close( void )
{
   if ( (HKEY)NULL != m_KeyHandle )
   {
      ::RegCloseKey( m_KeyHandle );
      m_KeyHandle = (HKEY) NULL;
   }

   if ( (HKEY) NULL == m_RegistryHandle )
   {
      return( true );
   }

   m_ErrorCode = ::RegCloseKey( m_RegistryHandle );

   if ( ERROR_SUCCESS == m_ErrorCode )
   {
      m_RegistryHandle = (HKEY) NULL;
      Initialize();
      return( true );
   }
   else
      return( false );
}
/*	end of function "Close" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	Connect

       DESCRIPTION:	opens a connection to the registry

             INPUT:  hKeyToOpen - the hive key to open
                   lpSystemName - the system to open the hive on	
           RETURNS:	true on success, false otherwise
*/
bool VMRegistry::Connect( HKEY hKeyToOpen, LPCTSTR lpSystemName )
{
   // We were passed a pointer, do not trust it
   //
   try
   {
      // lpSystemName can be NULL
      //
      if ( ( (HKEY) keyClassesRoot == hKeyToOpen ) 
        || ( (HKEY) keyCurrentUser == hKeyToOpen ) )
      {
         if ( NULL == lpSystemName )
         {
            m_RegistryHandle = hKeyToOpen;
            m_ErrorCode      = ERROR_SUCCESS;
         }
         else
         {
            // NT won't allow you to connect to these hives via RegConnectRegistry so we'll just skip that step
            //
            m_ErrorCode = ERROR_INVALID_HANDLE;
         }
      }
      else
      {
         m_ErrorCode = ::RegConnectRegistry( (char *) lpSystemName, 
                                             hKeyToOpen, 
                                             &m_RegistryHandle );
      }

      if ( ERROR_SUCCESS == m_ErrorCode )
      {
         if ( NULL == lpSystemName )
         {
            char achHostName[ MAX_PATH ];
            DWORD dwsize = MAX_PATH;

            if ( ::GetComputerName( achHostName, &dwsize ) )
               m_ComputerName = achHostName;
            else
               m_ComputerName.Empty();
         }
         else
            m_ComputerName = lpSystemName;

         // It would be nice to use a switch statement here but 
         // compiler spits a "not integral" error!
         //
         if ( HKEY_LOCAL_MACHINE == hKeyToOpen )
            m_RegistryName = "HKEY_LOCAL_MACHINE";
         else 
         if ( HKEY_CLASSES_ROOT == hKeyToOpen )
            m_RegistryName = "HKEY_CLASSES_ROOT";
         else 
         if ( HKEY_USERS == hKeyToOpen )
            m_RegistryName = "HKEY_USERS";
         else 
         if ( HKEY_CURRENT_USER == hKeyToOpen )
            m_RegistryName = "HKEY_CURRENT_USER";
         else 
         if ( HKEY_PERFORMANCE_DATA == hKeyToOpen )
            m_RegistryName = "HKEY_PERFORMANCE_DATA";
#if ( WINVER >= 0x400 )
         else 
         if ( HKEY_CURRENT_CONFIG == hKeyToOpen )
            m_RegistryName = "HKEY_CURRENT_CONFIG";
         else 
         if ( HKEY_DYN_DATA == hKeyToOpen )
            m_RegistryName = "HKEY_DYN_DATA";
#endif
         else
            m_RegistryName = "Unknown";

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


/*****************************************************************************/
/*
     FUNCTION NAME:	Create

       DESCRIPTION:	Creates a new key in the registry

             INPUT: lpSubKey - the name of the subkey to create	
                    lpClassName - the class name for this key
                    xOptions - creation options
                    xPermissions - permissions to apply to the new key
                    pxSecurity - security info to apply to the new key
                    pxDisposition - 
           RETURNS:	true if successful, false otherwise
*/
bool VMRegistry::Create( LPCTSTR               lpSubKey, 
                       LPCTSTR               lpClassName,
                       CreateOptions         xOptions, 
                       CreatePermissions     xPermissions, 
                       LPSECURITY_ATTRIBUTES pxSecurity,
                       CreationDisposition*  pxDisposition  )
{
   if ( NULL == lpSubKey )
   {
      m_ErrorCode = ERROR_INVALID_PARAMETER;
      return( false );
   }

   // We were passed a pointer, do not trust it
   //
   try
   {
      DWORD dwDisposition = 0;

      if ( NULL == lpClassName )
         lpClassName = "";

      if ( (HKEY) NULL != m_KeyHandle )
      {
         ::RegCloseKey( m_KeyHandle );
         m_KeyHandle = (HKEY) NULL;
      }

      m_ErrorCode = ::RegCreateKeyEx( m_RegistryHandle,
                                      lpSubKey,
                                      (DWORD) 0,
                                      (char *) lpClassName,
                                      xOptions,
                                      xPermissions,
                                      pxSecurity,
                                      &m_KeyHandle,
                                      &dwDisposition );

      if ( ERROR_SUCCESS == m_ErrorCode )
      {
         if ( NULL != pxDisposition )
            *pxDisposition = (CreationDisposition) dwDisposition;

         m_KeyName = lpSubKey;

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


/*****************************************************************************/
/*
     FUNCTION NAME:	Create

       DESCRIPTION:	Creates a new key in the registry

             INPUT: lpSubKey - the name of the subkey to create	
                    lpClassName - the class name for this key
                    xOptions - creation options
                    xPermissions - permissions to apply to the new key
                    pxSecurity - security info to apply to the new key
                    pxDisposition - 
           RETURNS:	true if successful, false otherwise
*/
bool VMRegistry::CreateNode( LPCTSTR               lpSubKey, 
                             LPCTSTR               lpClassName,
                             CreateOptions         xOptions, 
                             CreatePermissions     xPermissions, 
                             LPSECURITY_ATTRIBUTES pxSecurity,
                             CreationDisposition*  pxDisposition  )
{
   if ( NULL == lpSubKey )
   {
      m_ErrorCode = ERROR_INVALID_PARAMETER;
      return( false );
   }

   // We were passed a pointer, do not trust it
   //
   try
   {
      DWORD dwDisposition = 0;

      if ( NULL == lpClassName )
         lpClassName = "";

      m_ErrorCode = ::RegCreateKeyEx( m_KeyHandle,
                                      lpSubKey,
                                      (DWORD) 0,
                                      (char *) lpClassName,
                                      xOptions,
                                      xPermissions,
                                      pxSecurity,
                                      &m_KeyHandle,
                                      &dwDisposition );

      if ( ERROR_SUCCESS == m_ErrorCode )
      {
         if ( NULL != pxDisposition )
            *pxDisposition = (CreationDisposition) dwDisposition;

         m_KeyName = lpSubKey;

         return( true );
      }
      else
      {
LPVOID lpMsgBuf;
FormatMessage( 
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM | 
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    (LPTSTR) &lpMsgBuf,
    0,
    NULL 
);
// Process any inserts in lpMsgBuf.
// ...
// Free the buffer.
LocalFree( lpMsgBuf );

         return( false );
      }
   }
   catch( ... )
   {
      m_ErrorCode = ERROR_EXCEPTION_IN_SERVICE;
      return( false );

⌨️ 快捷键说明

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