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

📄 vmenvironment.cpp

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

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

    Description: This is the implementation of a class that knows how to
                 interface to the NT Environment

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
static char OBJECT_ID[] = "$Revision: $ : $Date: $";
/*****************************************************************************/


#include "../../../stdafx.h"
#include <memory.h>
#include "VMEnvironment.h"


VMEnvironmentRegistry::VMEnvironmentRegistry( void )
{ 
  m_oClassName.Empty();
  m_oComputerName.Empty();
  m_oKeyName.Empty();
  m_oRegistryName.Empty();

  m_hKeyHandle                     = (HKEY) NULL;
  m_dwErrorCode                    = 0L;
  m_dwNumberOfSubkeys              = 0;
  m_dwLongestSubkeyNameLength      = 0;
  m_dwLongestClassNameLength       = 0;
  m_dwNumberOfValues               = 0;
  m_dwLongestValueNameLength       = 0;
  m_dwLongestValueDataLength       = 0;
  m_dwSecurityDescriptorLength     = 0;
  m_xLastWriteTime.dwLowDateTime   = 0;
  m_xLastWriteTime.dwHighDateTime  = 0;
  m_hRegistryHandle                = (HKEY) NULL;
}


VMEnvironmentRegistry::~VMEnvironmentRegistry( void )
{
  if ( (HKEY)NULL != m_hKeyHandle )
  {
    ::RegCloseKey( m_hKeyHandle );
    m_hKeyHandle = (HKEY) NULL;
  }

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

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

       DESCRIPTION:	opens a connection to the registry

             INPUT:  hKeyToOpen - the hive key to open
                     pchSystemName - the system to open the hive on	

           RETURNS:	true on success, false otherwise
*/
bool VMEnvironmentRegistry::Connect( HKEY hKeyToOpen, const char* pchSystemName )
{
  // We were passed a pointer, do not trust it
  //
  try
  {
    // pchSystemName can be NULL
    //
    if ( ( (HKEY) HKEY_CLASSES_ROOT == hKeyToOpen ) 
      || ( (HKEY) HKEY_CURRENT_USER == hKeyToOpen ) )
    {
      if ( NULL == pchSystemName )
      {
         m_hRegistryHandle  = hKeyToOpen;
         m_dwErrorCode      = ERROR_SUCCESS;
      }
      else
      {
         // NT won't allow you to connect to these hives via RegConnectRegistry so we'll just skip that step
         //
         m_dwErrorCode = ERROR_INVALID_HANDLE;
      }
    }
    else
    {
      m_dwErrorCode = ::RegConnectRegistry( (char *) pchSystemName, 
                                            hKeyToOpen, 
                                            &m_hRegistryHandle );
    }

    if ( ERROR_SUCCESS == m_dwErrorCode )
    {
      if ( NULL == pchSystemName )
      {
        char  achHostName[ MAX_PATH ];
        DWORD dwsize = MAX_PATH;

        if ( ::GetComputerName( achHostName, &dwsize ) )
        {
          m_oComputerName = achHostName;
        }
        else
        {
          m_oComputerName.Empty();
        }
      }
      else
      {
        m_oComputerName = pchSystemName;
      }

      // It would be nice to use a switch statement here but 
      // compiler spits a "not integral" error!
      //
      if ( HKEY_LOCAL_MACHINE == hKeyToOpen )
      {
        m_oRegistryName = "HKEY_LOCAL_MACHINE";
      }
      else 
      if ( HKEY_CLASSES_ROOT == hKeyToOpen )
      {
        m_oRegistryName = "HKEY_CLASSES_ROOT";
      }
      else 
      if ( HKEY_USERS == hKeyToOpen )
      {
        m_oRegistryName = "HKEY_USERS";
      }
      else 
      if ( HKEY_CURRENT_USER == hKeyToOpen )
      {
        m_oRegistryName = "HKEY_CURRENT_USER";
      }
      else 
      if ( HKEY_PERFORMANCE_DATA == hKeyToOpen )
      {
        m_oRegistryName = "HKEY_PERFORMANCE_DATA";
#if ( WINVER >= 0x400 )
      }
      else 
      if ( HKEY_CURRENT_CONFIG == hKeyToOpen )
      {
        m_oRegistryName = "HKEY_CURRENT_CONFIG";
      }
      else 
      if ( HKEY_DYN_DATA == hKeyToOpen )
      {
        m_oRegistryName = "HKEY_DYN_DATA";
#endif
      }
      else
      {
        m_oRegistryName = "Unknown";
      }
      return( true );
    }
    else
    {
      return( false );
    }
  }
  catch( ... )
  {
    m_dwErrorCode = ERROR_EXCEPTION_IN_SERVICE;
    return( false );
  }
}
/*	end of function "VMEnvironmentRegistry::Connect" */
/*****************************************************************************/


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

       DESCRIPTION:	opens a registry key

             INPUT: pchKeyName - the name of the key to open
                    dwAccessMask -  the level of access that is being requested
           RETURNS:	true if successful, false otherwise
*/
bool VMEnvironmentRegistry::Open( const char* pchKeyName, DWORD dwAccessMask )
{
  // We were passed a pointer, do not trust it
  //
  try
  {
    if ( (HKEY) NULL != m_hKeyHandle )
    {
      ::RegCloseKey( m_hKeyHandle );
      m_hKeyHandle = (HKEY) NULL;
    }

    m_dwErrorCode = ::RegOpenKeyEx( m_hRegistryHandle, 
		                                pchKeyName, 
                  									NULL, 
									                  dwAccessMask, 
									                  &m_hKeyHandle );

    if ( ERROR_SUCCESS == m_dwErrorCode )
    {
      QueryInfo();
      m_oKeyName = pchKeyName;
      return( true );
    }
    else
    {
      return( false );
    }
  }
  catch( ... )
  {
     m_dwErrorCode = ERROR_EXCEPTION_IN_SERVICE;
     return( false );
  }
}
/*	end of function "VMEnvironmentRegistry::Open" */
/*****************************************************************************/



/*****************************************************************************/
/*
     FUNCTION NAME:	VMEnvironmentRegistry::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 VMEnvironmentRegistry::QueryInfo( void )
{
  char achClassName[ 2048 ];

  ::ZeroMemory( achClassName, sizeof( achClassName ) );

   DWORD dwBufferSize = sizeof( achClassName ) - 1;
   
   m_dwErrorCode = ::RegQueryInfoKey( m_hKeyHandle,
                                      achClassName,
                                      &dwBufferSize,
                                      (LPDWORD) NULL,
                                      &m_dwNumberOfSubkeys,
                                      &m_dwLongestSubkeyNameLength,
                                      &m_dwLongestClassNameLength,
                                      &m_dwNumberOfValues,
                                      &m_dwLongestValueNameLength,
                                      &m_dwLongestValueDataLength,
                                      &m_dwSecurityDescriptorLength,
                                      &m_xLastWriteTime );

  if ( ERROR_SUCCESS == m_dwErrorCode )
  {
    m_oClassName = achClassName;
    return( true );
  }
  else
  {
    return( false );
  }
}
/*	end of function "VMEnvironmentRegistry::QueryInfo" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME: VMEnvironmentRegistry::AddEnvironmentVariable

       DESCRIPTION:	adds a new key to the environment section of the registry

             INPUT: void

           RETURNS:	false if failed.
                    true  if successfully added or entry already exists.
*/
int VMEnvironmentRegistry::AddEnvironmentVariable( char* pchName, char* pchValue )
{
	int status = 0;

  if ( true != Connect( (HKEY)HKEY_LOCAL_MACHINE ) )
  {
    return( false );
  }

  if ( true != Open( "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",
                     KEY_CREATE_SUB_KEY | KEY_READ | KEY_SET_VALUE ) )
  {
    return( false );
  }


  m_dwErrorCode = ::RegSetValueEx( m_hKeyHandle,
                                   pchName,
                                   0,
                                   REG_SZ,
                                   (const unsigned char*)pchValue,
                                   strlen( pchValue ) );

  if ( ERROR_SUCCESS == m_dwErrorCode )
  {
    m_dwErrorCode = ::RegFlushKey( m_hKeyHandle );

    if ( ERROR_SUCCESS == m_dwErrorCode )
    {
      return( true );
    }
    else
    {
      return( false );
    }
  }
  else
  {
    return( false );
  }
}
/*	end of function "VMEnvironmentRegistry::AddEnvironmentVariable" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	VMEnvironmentRegistry::DeleteValue

       DESCRIPTION:	removes a value from the registry

             INPUT: lpDelete - the name of the value to delete
           RETURNS:	true if SUCCESSFUL, false otherwise
*/
bool VMEnvironmentRegistry::DeleteValue( char* pchDelete )
{
  // We were passed a pointer, do not trust it
  //
  try
  {
    m_dwErrorCode = ::RegDeleteValue( m_hKeyHandle, pchDelete );

    if ( ERROR_SUCCESS == m_dwErrorCode )
    {
      return( true );
    }
    else
    {
      return( false );
    }
  }
  catch( ... )
  {
    m_dwErrorCode = ERROR_EXCEPTION_IN_SERVICE;
    return( false );
  }
}
/*	end of function "VMEnvironmentRegistry::DeleteValue" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME: VMEnvironmentRegistry::RemoveEnvironmentVariable

       DESCRIPTION:	

             INPUT: void

⌨️ 快捷键说明

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