393 lines
6.9 KiB
C++
393 lines
6.9 KiB
C++
//-----------------------------------------------------------------------------+
|
|
// Copyright (C), 1998-2007, Beijing Tangent Software Co. Ltd.
|
|
// = FileName : TRegProperty.cpp
|
|
// = Version : ver2.0
|
|
// = Author : xlc
|
|
// = CreateDate : 2002-09-09
|
|
// = Description: 类的定义
|
|
// = Maintainers:
|
|
//
|
|
//-----------------------------------------------------------------------------+
|
|
#include "StdAfx.h"
|
|
#include "TRegProperty.h"
|
|
|
|
void TRegProperty::init()
|
|
{
|
|
m_RegPropertyType = RegPTypeNONE;
|
|
SetMaster( this );
|
|
}
|
|
|
|
void TRegProperty::clear()
|
|
{
|
|
m_bDeleting = TRUE;
|
|
|
|
int nSize = m_CRegObjectArray.GetSize();
|
|
|
|
for ( int i = 0; i < nSize; i++ )
|
|
{
|
|
TRegObject * pobj = (TRegObject *)m_CRegObjectArray[i];
|
|
|
|
if ( pobj->m_bDeleteByRegProperty )
|
|
{
|
|
delete pobj;
|
|
}
|
|
|
|
}
|
|
|
|
m_CRegObjectArray.RemoveAll();
|
|
|
|
m_bDeleting = FALSE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TRegProperty::TRegProperty()
|
|
{
|
|
init();
|
|
}
|
|
|
|
TRegProperty::TRegProperty ( LPCTSTR pszRootKey , LPCTSTR pszObjKey )
|
|
: TRegObject ( pszObjKey )
|
|
{
|
|
init();
|
|
|
|
SetRootKey ( pszRootKey , 0.0 );
|
|
}
|
|
|
|
TRegProperty::TRegProperty ( LPCTSTR pszRootKey , LPCTSTR pszObjKey, double dVersion)
|
|
: TRegObject ( pszObjKey, (dVersion > 0) )
|
|
{
|
|
init();
|
|
|
|
SetRootKey ( pszRootKey , dVersion );
|
|
}
|
|
|
|
TRegProperty::TRegProperty ( const TRegProperty & obj )
|
|
{
|
|
*this = obj;
|
|
}
|
|
|
|
TRegProperty & TRegProperty::operator = ( const TRegProperty & obj )
|
|
{
|
|
if ( &obj != this )
|
|
{
|
|
clear();
|
|
init();
|
|
|
|
m_RootKey = obj.m_RootKey ;
|
|
m_VersionKey = obj.m_VersionKey ;
|
|
m_RegPropertyType = obj.m_RegPropertyType;
|
|
|
|
int nSize = obj.m_CRegObjectArray.GetSize();
|
|
|
|
m_CRegObjectArray.SetSize( nSize );
|
|
|
|
for ( int i = 0; i < nSize; i++ )
|
|
{
|
|
TRegObject * pobj = (TRegObject *)m_CRegObjectArray[i];
|
|
|
|
m_CRegObjectArray.Add( pobj );
|
|
}
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
TRegProperty::~TRegProperty()
|
|
{
|
|
clear();
|
|
}
|
|
|
|
void TRegProperty::Register ( TRegObject * pobj )
|
|
{
|
|
if ( pobj != this )
|
|
{
|
|
m_CRegObjectArray.Add ( pobj );
|
|
}
|
|
}
|
|
|
|
bool TRegProperty::LookUp ( TRegObject * pobjLook , int *pIndex )
|
|
{
|
|
bool bRet = FALSE;
|
|
|
|
int nSize = m_CRegObjectArray.GetSize();
|
|
|
|
for ( int i = 0; i < nSize; i++ )
|
|
{
|
|
TRegObject * pobj = (TRegObject *)m_CRegObjectArray[i];
|
|
|
|
if ( pobjLook == pobj )
|
|
{
|
|
if ( pIndex )
|
|
{
|
|
*pIndex = i;
|
|
}
|
|
bRet = TRUE;
|
|
break;
|
|
}
|
|
}
|
|
|
|
ASSERT( bRet );
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
TRegObject * TRegProperty::LookUp ( DWORD_PTR hSubkey )
|
|
{
|
|
TRegObject * pCRegObject = NULL;
|
|
TRegObject * pObject = (TRegObject *)hSubkey;
|
|
|
|
if ( LookUp( pObject ) )
|
|
{
|
|
pCRegObject = pObject;
|
|
}
|
|
return pCRegObject;
|
|
}
|
|
|
|
void TRegProperty::Unregister( TRegObject * pobjUn )
|
|
{
|
|
if ( !m_bDeleting )
|
|
{
|
|
int Index = 0;
|
|
|
|
if ( pobjUn != this )
|
|
{
|
|
if ( LookUp ( pobjUn , &Index ) )
|
|
{
|
|
m_CRegObjectArray.RemoveAt( Index );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bool TRegProperty::Store()
|
|
{
|
|
bool bRet = TRUE;
|
|
|
|
int nSize = m_CRegObjectArray.GetSize();
|
|
|
|
for ( int i = 0; i < nSize; i++ )
|
|
{
|
|
TRegObject * pobj = (TRegObject *)m_CRegObjectArray[i];
|
|
|
|
if ( pobj )
|
|
{
|
|
bRet = pobj->Store();
|
|
}
|
|
|
|
ASSERT( bRet );
|
|
|
|
}
|
|
return bRet;
|
|
}
|
|
|
|
bool TRegProperty::Read()
|
|
{
|
|
bool bRet = TRUE;
|
|
|
|
int nSize = m_CRegObjectArray.GetSize();
|
|
|
|
for ( int i = 0; i < nSize; i++ )
|
|
{
|
|
TRegObject * pobj = (TRegObject *)m_CRegObjectArray[i];
|
|
|
|
if ( pobj )
|
|
{
|
|
bRet = pobj->Read();
|
|
}
|
|
ASSERT( bRet );
|
|
}
|
|
return bRet;
|
|
}
|
|
|
|
bool TRegProperty::Init()
|
|
{
|
|
bool bRet = TRUE;
|
|
|
|
int nSize = m_CRegObjectArray.GetSize();
|
|
|
|
for ( int i = 0; i < nSize; i++ )
|
|
{
|
|
TRegObject * pobj = (TRegObject *)m_CRegObjectArray[i];
|
|
|
|
if ( pobj )
|
|
bRet = pobj->Init();
|
|
|
|
ASSERT( bRet );
|
|
|
|
}
|
|
return bRet;
|
|
}
|
|
|
|
CString TRegProperty::GetRootKey ( bool bVersionized ) const
|
|
{
|
|
return bVersionized ? m_VersionKey : m_CommonKey;
|
|
}
|
|
|
|
CString TRegProperty::GetVersion() const
|
|
{
|
|
return m_Version;
|
|
}
|
|
|
|
void TRegProperty::SetRootKey ( LPCTSTR pszRootKey , double dVersion )
|
|
{
|
|
m_RootKey = pszRootKey;
|
|
|
|
m_Version.Format( _T("%lf") , dVersion );
|
|
|
|
m_VersionKey = m_RootKey;
|
|
m_VersionKey += '\\';
|
|
m_VersionKey += m_Version;
|
|
m_CommonKey = m_RootKey;
|
|
|
|
}
|
|
|
|
DWORD_PTR TRegProperty::CreateSubKey ( LPCTSTR pszSubkey , bool bVersionized , LPCTSTR pszClassname )
|
|
{
|
|
DWORD_PTR hSubkey = 0;
|
|
|
|
CString Classname;
|
|
|
|
if ( pszClassname )
|
|
{
|
|
Classname = pszClassname;
|
|
}
|
|
else
|
|
{
|
|
Classname = "Class";
|
|
Classname += pszSubkey;
|
|
|
|
}
|
|
|
|
TRegObject * pCRegObject = new TRegObject( pszSubkey , bVersionized , Classname , this , TRUE );
|
|
|
|
hSubkey = (DWORD_PTR)pCRegObject;
|
|
|
|
return hSubkey;
|
|
|
|
}
|
|
|
|
TRegObject * TRegProperty::GetRegObject ( DWORD_PTR Handle )
|
|
{
|
|
ASSERT( Handle );
|
|
|
|
return (TRegObject *)Handle;
|
|
|
|
}
|
|
|
|
bool TRegProperty::AttachValue ( DWORD_PTR hSubkey , int *pintValue , LPCTSTR pszName )
|
|
{
|
|
bool bRet = FALSE;
|
|
|
|
TRegObject * pRegObject = LookUp ( hSubkey );
|
|
|
|
if ( pRegObject )
|
|
{
|
|
bRet = pRegObject->AttachValue ( pintValue , pszName );
|
|
}
|
|
return bRet;
|
|
}
|
|
|
|
bool TRegProperty::AttachValue ( DWORD_PTR hSubkey , bool *pboolValue , LPCTSTR pszName )
|
|
{
|
|
bool bRet = FALSE;
|
|
|
|
TRegObject * pRegObject = LookUp ( hSubkey );
|
|
|
|
if ( pRegObject )
|
|
{
|
|
bRet = pRegObject->AttachValue ( pboolValue , pszName );
|
|
}
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
bool TRegProperty::AttachValue ( DWORD_PTR hSubkey , DWORD *pDWORDValue , LPCTSTR pszName )
|
|
{
|
|
bool bRet = FALSE;
|
|
|
|
TRegObject * pRegObject = LookUp ( hSubkey );
|
|
|
|
if ( pRegObject )
|
|
{
|
|
bRet = pRegObject->AttachValue ( pDWORDValue , pszName );
|
|
}
|
|
return bRet;
|
|
}
|
|
|
|
bool TRegProperty::AttachValue ( DWORD_PTR hSubkey , long *plongValue , LPCTSTR pszName )
|
|
{
|
|
return AttachValue ( hSubkey , (DWORD *)plongValue , pszName );
|
|
}
|
|
|
|
bool TRegProperty::AttachValue ( DWORD_PTR hSubkey , CString *pCStringValue , LPCTSTR pszName )
|
|
{
|
|
bool bRet = FALSE;
|
|
|
|
TRegObject * pRegObject = LookUp ( hSubkey );
|
|
|
|
if ( pRegObject )
|
|
{
|
|
bRet = pRegObject->AttachValue ( pCStringValue , pszName );
|
|
}
|
|
|
|
return bRet;
|
|
}
|
|
|
|
bool TRegProperty::ReadValue ( HKEY key
|
|
, const TCHAR * pszKey
|
|
, const TCHAR * pszValuename
|
|
, BYTE * pBuffer
|
|
, DWORD BufferSize )
|
|
{
|
|
bool bRet = FALSE;
|
|
|
|
DWORD dwReserved = 0;
|
|
HKEY hkObject = NULL;
|
|
|
|
LONG Retcode = ::RegOpenKeyEx ( HKEY_CURRENT_USER // handle of an open key
|
|
, pszKey
|
|
, dwReserved // reserved
|
|
, KEY_QUERY_VALUE // desired security access
|
|
, &hkObject // address of buffer for opened handle
|
|
);
|
|
if ( Retcode == ERROR_SUCCESS )
|
|
{
|
|
DWORD DataSize = 0;
|
|
|
|
Retcode = RegQueryValueEx ( hkObject // query size
|
|
, pszValuename
|
|
, NULL // &dwReserved
|
|
, NULL
|
|
, NULL
|
|
, &DataSize
|
|
);
|
|
ASSERT( DataSize <= BufferSize );
|
|
|
|
if ( Retcode == ERROR_SUCCESS && DataSize <= BufferSize )
|
|
{
|
|
Retcode = RegQueryValueEx ( hkObject // get value
|
|
, pszValuename
|
|
, NULL // &dwReserved
|
|
, NULL
|
|
, pBuffer
|
|
, &DataSize
|
|
);
|
|
|
|
if ( Retcode == ERROR_SUCCESS )
|
|
{
|
|
bRet = TRUE;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if ( Retcode != ERROR_SUCCESS )
|
|
{
|
|
AfxMessageBox(_T("Reg Fail"));
|
|
}
|
|
::RegCloseKey( hkObject );
|
|
return bRet;
|
|
} |