📄 vmenvironment.cpp
字号:
RETURNS: 0 if failed.
1 if successfully added or entry does not exist.
*/
int VMEnvironmentRegistry::RemoveEnvironmentVariable( char* pchName )
{
if ( true != Connect( (HKEY)HKEY_LOCAL_MACHINE ) )
{
return( false );
}
if ( true != Open( "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",
KEY_ALL_ACCESS ) )
{
return( false );
}
if ( true != DeleteValue( pchName ) )
{
return( false );
}
m_dwErrorCode = ::RegFlushKey( m_hKeyHandle );
if ( ERROR_SUCCESS == m_dwErrorCode )
{
return( true );
}
else
{
return( false );
}
}
/* end of function "VMEnvironmentRegistry::RemoveEnvironmentVariable" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMEnvironmentVariables::VMEnvironmentVariables
DESCRIPTION: ctor.
INPUT: none
OUTPUT: none
RETURNS: -
*/
VMEnvironmentVariables::VMEnvironmentVariables( void )
{
m_bLoaded = false;
m_oErrorText = _T("");
}
/* End of function "VMEnvironmentVariables::VMEnvironmentVariables"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMEnvironmentVariables::~VMEnvironmentVariables
DESCRIPTION: dtor
INPUT: none
OUTPUT: none
RETURNS: -
*/
VMEnvironmentVariables::~VMEnvironmentVariables( void )
{
if ( m_bLoaded )
{
m_oStringMap.erase( m_oStringMap.begin(), m_oStringMap.end() );
}
}
/* End of function "VMEnvironmentVariables::~VMEnvironmentVariables"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMEnvironmentVariables::GetValueDirect
DESCRIPTION: queries the os to retrieve the value for the given
variable
INPUT: pchName - the variable name
roOutput - the value for the variable is put here
OUTPUT: roOutput : contents are change
RETURNS: returns NONZERO on SUCCESS, ZERO otherwise
*/
int VMEnvironmentVariables::GetValueDirect( char* pchName, VMString& roOutput )
{
char* pchBuffer = NULL;
int iReturn = false; // assume failure
char chTemp[ 5 ];
roOutput = _T("");
// determine the size of the environment variables value
//
int iSize = GetEnvironmentVariable( pchName, chTemp, 0 );
//allocate buffer to receive contents of environment variable.
//
pchBuffer = new char[ iSize + 5 ];
if ( NULL == pchBuffer )
{
m_oErrorText = "Insufficient memory";
iReturn = 0;
}
else
{
memset( pchBuffer, '\0', iSize+5 );
if ( strlen( pchName ) )
{
iReturn = GetEnvironmentVariable( pchName, pchBuffer, iSize );
if ( ( iReturn > iSize ) || ( iReturn == 0 ) )
{
m_oErrorText = "No Environment Variable of that name";
}
else
{
roOutput = pchBuffer;
}
}
else
{
m_oErrorText = "Failed to pass name";
}
}
delete [] pchBuffer;
return ( iReturn );
}
/* End of function "VMEnvironmentVariables::GetValueDirect"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMEnvironmentVariables::RemoveKey
DESCRIPTION: Removes a key from the current environment by calling
SetValue with a NULL for the value string.
INPUT: pchVariable - the name of the key to remove
OUTPUT: none
RETURNS: NONZERO on SUCCESS, ZERO otherwise
*/
int VMEnvironmentVariables::RemoveKey( char* pchVariable, bool bRegWrite )
{
int iReturn = 0xFFFFFFFF;
if ( bRegWrite )
{
VMEnvironmentRegistry oRegBasedEnvironment;
iReturn &= oRegBasedEnvironment.RemoveEnvironmentVariable( pchVariable );
}
VMString oKey = pchVariable;
iReturn &= m_oStringMap.erase( oKey );
iReturn &= SetValue( pchVariable, NULL, false, true );
return( iReturn );
}
/* End of function "VMEnvironmentVariables::RemoveKey"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMEnvironmentVariables::SetValue
DESCRIPTION: attempts to store an environment variable's value
INPUT: pchName - the name of the variable
pchValue - the value to store
bRegWrite - if true, the key will be written to the registry
as a permanent environment setting. If false,
then setting the key only effects the local process
environment.
bReload - if true will cause this class to reload an
internal map of the environment.
OUTPUT:
RETURNS: NONZERO on SUCCESS, ZERO otherwise
*/
int VMEnvironmentVariables::SetValue( char* pchName, char* pchValue, bool bRegWrite, bool bReload )
{
int iReturn;
if ( strlen( pchName ) )
{
iReturn = SetEnvironmentVariable( pchName, pchValue );
if ( iReturn == 0 )
{
return( iReturn );
}
else
{
if ( bRegWrite )
{
VMEnvironmentRegistry oRegBasedEnvironment;
iReturn &= oRegBasedEnvironment.AddEnvironmentVariable( pchName, pchValue );
}
if ( bReload )
{
return( ReloadEnvironment() );
}
else
{
return( iReturn );
}
}
}
else
{
return( 0 );
}
}
/* End of function "VMEnvironmentVariables::SetValue"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMEnvironmentVariables::ReloadEnvironment
DESCRIPTION: Empties the map in this and then loads it up again
INPUT: void
OUTPUT: none
RETURNS: return code from subordinate
*/
int VMEnvironmentVariables::ReloadEnvironment( void )
{
if ( m_bLoaded )
{
m_oStringMap.erase( m_oStringMap.begin(), m_oStringMap.end() );
}
return ( LoadEnvironment() );
}
/* End of function "VMEnvironmentVariables::ReloadEnvironment"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMEnvironmentVariables::GetValueByName
DESCRIPTION: will look in the map for a variable of the given name.
If this is not loaded yet, then the map is loaded and
the key value is searched for.
INPUT: roKey - the name of the key
roOutput - the value of the key
OUTPUT: roOutput : contents are changed
RETURNS: true if successful, false otherwise
*/
bool VMEnvironmentVariables::GetValueByName( VMString& roKey, VMString& roOutput )
{
roOutput = _T("");
if ( !m_bLoaded )
{
LoadEnvironment();
}
m_oMapIter = m_oStringMap.find( roKey );
if ( m_oStringMap.end() != m_oMapIter )
{
roOutput = (*m_oMapIter).second;
return( true );
}
return( false );
}
/* End of function "VMEnvironmentVariables::GetValueByName"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMEnvironmentVariables::LoadEnvironment
DESCRIPTION: does all the work requried to load the environment string
into the internal map of key-value pairs
INFO:
Environment information comes back from the OS in the
following general form:
VarName1=Value0VarName2=Value0VarName3=Value00
Each key-value pair is seperated by a NULL, two NULLS in
a row terminate the list
INPUT: void
OUTPUT: none
RETURNS: NONZERO on sucess, ZERO otherwise
*/
int VMEnvironmentVariables::LoadEnvironment( void )
{
LPVOID lpEnvironment = GetEnvironmentStrings();
VMString oVarName;
VMString oValue;
bool bFirstPass = true;
if ( NULL != lpEnvironment )
{
char* pchWorking = (char*)lpEnvironment;
int iLen = strlen( (const char*)lpEnvironment );
while ( iLen )
{
if ( *pchWorking )
{
if ( !bFirstPass )
{
pchWorking = (char*)pchWorking + iLen + 1;
}
else
{
bFirstPass = false;
}
// this is required for the =D:=D:\somepath
// environment variables which appear to be
// recording the current path on each drive
//
// adjusting the pchWorking pointer in this
// manner trims off the first '=' sign, thereby
// allowing the string to be mapped
//
if ( *pchWorking == '=' )
{
++pchWorking;
}
iLen = strlen( (const char*)pchWorking );
// snap the key value pair at the = sign and
// then store each respective portion of the
// string into its constituent components
//
char* pchEquals = strchr( pchWorking , '=' );
if ( NULL != pchEquals )
{
char* pchVariable = (char*)new char[ pchEquals - pchWorking + 1 ];
if ( NULL != pchVariable )
{
memset( pchVariable, 0, pchEquals - pchWorking + 1 );
strncpy( pchVariable, pchWorking, pchEquals - pchWorking );
oVarName = pchVariable;
delete [] pchVariable;
if ( pchEquals + 1 )
{
char* pchValue = (char*)new char[ strlen( pchWorking ) ];
if ( NULL != pchValue )
{
memset( pchValue, 0, strlen( pchWorking ) );
strcpy( pchValue, pchEquals + 1 );
oValue = pchValue;
delete [] pchValue;
}
}
else
{
oValue = _T("");
}
// save off the name-value pair into the map
//
m_oStringMap.insert( VM_ENV_VAR_MAP::value_type( oVarName, oValue ) );
}
}
}
}
FreeEnvironmentStrings( (char*)lpEnvironment );
m_bLoaded = true;
return( 0 );
}
else
{
return( -1 );
}
}
/* End of function "VMEnvironmentVariables::LoadEnvironment"
/*****************************************************************************/
/*****************************************************************************/
/* Check-in history */
/*
*$Log: $
*/
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -