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

📄 registry.cpp

📁 网络游戏龙族服务器端管理工具 rmtool 源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:


/////////////////////////////////////////////////////////////////////////////
// CRegVal extensions to use with MFC

CRegVal :: CRegVal( const CStringArray & arrInitial_p )
	: m_hHeap( HeapCreate( 0, 0, 0) )
	, m_pbyteData( 0 )
	, m_dwDataSize( 0 )
	, m_dwType( REG_NONE ) {
	SetValue( arrInitial_p ) ;
}

BOOL CRegVal :: GetValue( CString & rString_p ) const {
	ASSERT( m_dwDataSize > 0 ) ;
	ASSERT( m_pbyteData ) ;

	if(	m_dwType != REG_SZ
		&& m_dwType != REG_EXPAND_SZ
		&& m_dwType != REG_LINK ) {
		TRACE0("CRegVal::GetValue(CString &): wrong type.\n");
		return FALSE ;
	}
	
	rString_p = LPCTSTR(m_pbyteData);

	return TRUE ;
}


BOOL CRegVal :: GetValue( CStringArray & rArray_p ) const {
	ASSERT( m_dwDataSize > 0 ) ;
	ASSERT( m_pbyteData ) ;

	if(	m_dwType != REG_MULTI_SZ ) {
		TRACE0("CRegVal::GetValue(CStringArray &): wrong type.\n");
		return FALSE ;
	}

	rArray_p.RemoveAll();

	for( register LPCTSTR p = LPCTSTR(m_pbyteData); *p; p += _tcslen(p)+1 )
		rArray_p.Add( CString(p) );
	
	return TRUE ;
}


void CRegVal :: SetValue( const CStringArray & rArray_p ) {
	// first step: calculate size ...
	register int i ;
	DWORD nSize = 0 ;
	const int nElem = rArray_p.GetSize();

	for( i = 0 ; i < nElem ; ++i )
		nSize += (rArray_p[i].GetLength() * sizeof(TCHAR)) + sizeof(TCHAR);
	nSize += sizeof(TCHAR);	// for the trailing '\0' !

	AllocateDataBuffer( nSize + (nElem ? 0 : sizeof(TCHAR))) ;
	m_dwType = REG_MULTI_SZ ;

	register LPTSTR p = LPTSTR(m_pbyteData);
	if( ! nElem )
		*p++ = TEXT('\0');

	// last step: copy the strings together
	for( i = 0 ; i < nElem ; ++i ) {
		CString str = rArray_p[i];
		register LPCTSTR c = LPCTSTR(str);
		while( *p++ = *c++ );
	}
	*p = TEXT('\0');
}






BOOL CRegistry ::	SaveKey(
						LPCTSTR pszSubKey_p,
						LPCTSTR pszValueName_p,
						const CRegVal & rData_p,
						HKEY hKey_p
					) {
	ASSERT( pszSubKey_p != 0 ) ;
	ASSERT( pszValueName_p != 0 ) ;
	ASSERT( hKey_p != 0 ) ;

	HKEY hSubKey ;
	DWORD dwDisposition ;

	LONG lRet =	RegCreateKeyEx(
					hKey_p,					// handle of an open key 
					pszSubKey_p,			// address of subkey name 
					0,						// reserved 
					0,						// address of class string 
					REG_OPTION_NON_VOLATILE,// special options flag 
					KEY_WRITE,				// desired security access 
					0,						// address of key security structure 
					& hSubKey,				// address of buffer for opened handle  
					& dwDisposition 		// address of disposition value buffer 
				) ;

	if( lRet != ERROR_SUCCESS ) {
		TRACE0( "CRegistry::SaveKey(): RegCreateKeyEx() failed.\n" ) ;
		return FALSE ;
	}
	
	TRACE1( "CRegistry::SaveKey(): Disposition is %hs\n",
		dwDisposition == REG_CREATED_NEW_KEY
		? "REG_CREATED_NEW_KEY"
		: "REG_OPENED_EXISTING_KEY" ) ;


	lRet =	RegSetValueEx(
				hSubKey,				// handle of key to set value for
				pszValueName_p,			// address of value to set
				0,						// reserved
				rData_p.m_dwType,		// flag for value type
				rData_p.m_pbyteData,	// address of value data
				rData_p.m_dwDataSize	// size of value data
			) ;


	RegCloseKey( hSubKey ) ;
	RegCloseKey( hKey_p ) ;

	if( lRet != ERROR_SUCCESS ) {
		TRACE0( "CRegistry::SaveKey(): RegSetValueEx() failed.\n" );
		return FALSE ;
	}

	return TRUE ;
}


BOOL CRegistry ::	DeleteKey(
						LPCTSTR pszSubKey_p,
						LPCTSTR pszValueName_p,
						HKEY hKey_p
					) {
	ASSERT( pszSubKey_p != 0 ) ;
	ASSERT( hKey_p != 0 ) ;

	HKEY hSubKey ;
	LONG lRet =		RegOpenKeyEx(
						hKey_p,			// key handle at root level
						pszSubKey_p,	// path name of child key
						0,				// reserved
						KEY_WRITE | KEY_READ,	// requesting access
						&hSubKey		// address of key to be returned
					) ;
	
	if( lRet != ERROR_SUCCESS ) {
		TRACE0( "CRegistry::DeleteKey(): RegOpenKeyEx() failed\n" ) ;
		return FALSE ;
	}

	if( pszValueName_p ) {
		// delete the given value only.
		lRet = RegDeleteValue( hSubKey, pszValueName_p ) ;
		RegCloseKey( hSubKey ) ;
		RegCloseKey( hKey_p ) ;
		if( lRet != ERROR_SUCCESS ) {
			TRACE0( "CRegistry::DeleteKey(): RegDeleteValue() failed\n" );
			return FALSE ;
		}
	} else {
		// delete the entire key. NOTE: Windows NT cannot delete
		// subkeys of the key, so we have to handle it ourself, so
		// the code will work on both: NT and 95.

		DWORD dwSubKeyCnt = 0 ;
		do {	// loop until all subkeys are removed ...
				// Do not use the number of subkeys for the enumeration
				// (as in the RegistryTreeWalk() function),
				// because it changes, if a subkey is deleted !!!

			// first get an info about this subkey ...
			DWORD dwMaxSubKey ;
			LONG lRet =	RegQueryInfoKey(
							hSubKey,
							0,					// buffer for class name
							0,					// length of class name string
							0,					// reserved
							&dwSubKeyCnt,		// # of subkeys
							&dwMaxSubKey,		// length of longest subkey
							0,					// length of longest class name string
							0,					// # of values
							0,					// length of longest value name
							0,					// length of longest value data
							0,					// security descriptor
							0					// last write time
						) ;
			if( lRet != ERROR_SUCCESS ) {
				TRACE0( "CRegistry::DeleteKey(): RegQueryInfoKey() failed.\n" ) ;
				RegCloseKey( hSubKey ) ;
				return FALSE ;
			}

			if( dwSubKeyCnt > 0 ) {
				// retrieve the first subkey and call DeleteKey() recursivly.
				// if such a call fails we have to break the deletion !
				// NOTE: that the initially key may stay "half removed" ...
				LPTSTR pszKeyName = new TCHAR [ dwMaxSubKey + 1 ] ;
				DWORD dwKeyNameLen = dwMaxSubKey ;
				lRet =	RegEnumKey(
							hSubKey,
							0,				// index
							pszKeyName,		// address of buffer for key name string
							dwKeyNameLen+1	// max. length of key name string
						) ;

				if( lRet != ERROR_SUCCESS ) {
					TRACE0( "CRegistry::DeleteKey(): RegEnumKey() failed\n" ) ;
					delete [] pszKeyName ;
					RegCloseKey( hSubKey ) ;
					return FALSE ;
				}
				if( ! DeleteKey( pszKeyName, pszValueName_p, hSubKey ) ) {
					delete [] pszKeyName ;
					RegCloseKey( hSubKey ) ;
					return FALSE ;
				}
				delete [] pszKeyName ;
			}
		} while( dwSubKeyCnt > 0 ) ;
		
		RegCloseKey( hSubKey ) ;

		lRet = RegDeleteKey( hKey_p, pszSubKey_p ) ;
		//RegCloseKey( hKey_p ) ;

		if( lRet != ERROR_SUCCESS ) {
			TRACE0( "CRegistry::DeleteKey(): RegDeleteKey() failed\n" ) ;
			return FALSE ;
		}
	}

	return TRUE ;
}


BOOL CRegistry ::	HasEntries(
						LPCTSTR pszSubKey_p,
						DWORD * pdwSubKeyCnt_p,
						DWORD * pdwValueCnt_p,
						HKEY hKey_p
					) {
	ASSERT( pszSubKey_p != 0 ) ;
	ASSERT( hKey_p != 0 ) ;

	if( pdwSubKeyCnt_p ) * pdwSubKeyCnt_p = 0 ;
	if( pdwValueCnt_p ) * pdwValueCnt_p = 0 ;

	HKEY hSubKey ;
	LONG lRet =		RegOpenKeyEx(
						hKey_p,			// key handle at root level
						pszSubKey_p,	// path name of child key
						0,				// reserved
						KEY_READ,		// requesting read access
						&hSubKey		// address of key to be returned
					) ;
	
	if( lRet != ERROR_SUCCESS ) {
		TRACE0( "CRegistry::HasEntries(): RegOpenKeyEx() failed\n" ) ;
		return FALSE ;
	}
	
	DWORD dwSubKeyCnt ;
	DWORD dwValueCnt ;
	lRet =	RegQueryInfoKey(
				hSubKey,
				0,					// buffer for class name
				0,					// length of class name string
				0,					// reserved
				&dwSubKeyCnt,		// # of subkeys
				0,					// length of longest subkey
				0,					// length of longest class name string
				&dwValueCnt,		// # of values
				0,					// length of longest value name
				0,					// length of longest value data
				0,					// security descriptor
				0					// last write time
			) ;
	
	RegCloseKey( hSubKey ) ;
	
	if( lRet != ERROR_SUCCESS ) {
		TRACE0( "CRegistry::HasEntries(): RegQueryInfoKey() failed.\n" ) ;
		return FALSE ;
	}

	if( pdwSubKeyCnt_p )
		* pdwSubKeyCnt_p = dwSubKeyCnt ;

	if( pdwValueCnt_p )
		* pdwValueCnt_p = dwValueCnt ;

	return (dwSubKeyCnt > 0 || dwValueCnt > 0) ? TRUE : FALSE ;
}





/////////////////////////////////////////////////////////////////////////////
// class CRegVal	--	construction / destruction ...

CRegVal :: CRegVal()
	: m_hHeap( HeapCreate( 0, 0, 0) )
	, m_pbyteData( 0 )
	, m_dwDataSize( 0 )
	, m_dwType( REG_NONE ) {
}


CRegVal :: CRegVal( DWORD dwInitial_p )
	: m_hHeap( HeapCreate( 0, 0, 0) )
	, m_pbyteData( 0 )
	, m_dwDataSize( 0 )
	, m_dwType( REG_NONE ) {
	SetValue( dwInitial_p ) ;
}


CRegVal :: CRegVal( LPCTSTR pszInitial_p )
	: m_hHeap( HeapCreate( 0, 0, 0) )
	, m_pbyteData( 0 )
	, m_dwDataSize( 0 )
	, m_dwType( REG_NONE ) {
	SetValue( pszInitial_p ) ;
}


CRegVal :: CRegVal( const LPBYTE pbyteInitial_p, DWORD dwSize_p )
	: m_hHeap( HeapCreate( 0, 0, 0) )
	, m_pbyteData( 0 )
	, m_dwDataSize( 0 )
	, m_dwType( REG_NONE ) {
	SetValue( pbyteInitial_p, dwSize_p ) ;
}


CRegVal :: ~CRegVal() {
	if( m_pbyteData )
		HeapFree( m_hHeap, 0, m_pbyteData ) ;
	HeapDestroy( m_hHeap ) ;
}


/////////////////////////////////////////////////////////////////////////////
// class CRegVal	--	public members ...


void CRegVal :: SetType( DWORD dwType_p ) { m_dwType = dwType_p ; }
DWORD CRegVal :: GetType() const { return m_dwType ; }
DWORD CRegVal :: GetSize() const { return m_dwDataSize ; }


BOOL CRegVal :: GetValue( DWORD & rNum_p ) const {
	ASSERT( m_dwDataSize > 0 ) ;
	ASSERT( m_pbyteData ) ;

	if(	m_dwType != REG_DWORD
		&& m_dwType != REG_DWORD_LITTLE_ENDIAN
		&& m_dwType != REG_DWORD_BIG_ENDIAN ) {
		TRACE0("CRegVal::GetValue(DWORD&): wrong type.\n");
		return FALSE ;
	}
	
	rNum_p = *((DWORD *) m_pbyteData) ;

	return TRUE ;
}


BOOL CRegVal :: GetValue( LPCTSTR & rpString_p ) const {
	ASSERT( m_dwDataSize > 0 ) ;
	ASSERT( m_pbyteData ) ;

	if(	m_dwType != REG_SZ
		&& m_dwType != REG_EXPAND_SZ
		&& m_dwType != REG_LINK
		&& m_dwType != REG_MULTI_SZ ) {
		TRACE0("CRegVal::GetValue(const char * &): wrong type.\n");
		return FALSE ;
	}
	
	rpString_p = LPCTSTR(m_pbyteData);

	return TRUE ;
}


BOOL CRegVal :: GetValue( LPBYTE & rpbyteBuffer_p, DWORD dwBufferSize_p ) const {
	ASSERT( m_dwDataSize > 0 ) ;
	ASSERT( m_pbyteData ) ;

	if( (m_dwType != REG_BINARY
		&& m_dwType != REG_RESOURCE_LIST
		&& m_dwType != REG_FULL_RESOURCE_DESCRIPTOR)
		|| dwBufferSize_p != m_dwDataSize ) {
		TRACE0("CRegVal::GetValue(BYTE * &, DWORD): wrong type or size.\n");
		return FALSE ;
	}
	
	for( register DWORD i = 0 ; i < m_dwDataSize ; ++i )
		rpbyteBuffer_p[i] = m_pbyteData[i] ;

	return TRUE ;
}


void CRegVal :: SetValue( DWORD dwValue_p ) {
	AllocateDataBuffer( sizeof( dwValue_p ) ) ;
	m_dwType = REG_DWORD ;
	*((DWORD *)m_pbyteData) = dwValue_p ;
}


void CRegVal :: SetValue( LPCTSTR pszValue_p, BOOL bExpanded_p ) {
	const DWORD dwSize = pszValue_p ? (_tcslen(pszValue_p) * sizeof(TCHAR)) : 0 ;
	AllocateDataBuffer( dwSize + sizeof(TCHAR) ) ;
	m_dwType = bExpanded_p ? REG_EXPAND_SZ : REG_SZ ;
	if( pszValue_p )
		::CopyMemory(PVOID(m_pbyteData), pszValue_p, dwSize);
	m_pbyteData[ dwSize ] = TEXT('\0') ;
}


void CRegVal :: SetValue( LPCTSTR pszValue_p [], const DWORD nArray_p ) {
	// first step: calculate size ...
	register DWORD i ;
	DWORD nSize = 0 ;
	for( i = 0 ; i < nArray_p ; ++i )
		nSize += (_tcslen( pszValue_p[i] ) * sizeof(TCHAR)) + sizeof(TCHAR);
	nSize += sizeof(TCHAR);	// for the trailing '\0' !

	AllocateDataBuffer( nSize + (nArray_p ? 0 : sizeof(TCHAR)) ) ;
	m_dwType = REG_MULTI_SZ ;

	register LPTSTR p = LPTSTR(m_pbyteData);

	if( ! nArray_p )
		*p++ = TEXT('\0');

	// last step: copy the strings together
	for( i = 0 ; i < nArray_p ; ++i ) {
		register LPCTSTR c = pszValue_p[i] ;
		while( *p++ = *c++ );
	}
	*p = TEXT('\0');
}

void CRegVal :: SetValue( const LPBYTE pBuffer_p, const DWORD nSize_p ) {
	ASSERT( pBuffer_p ) ;
	ASSERT( nSize_p > 0 ) ;

	AllocateDataBuffer( nSize_p ) ;
	m_dwType = REG_BINARY ;
	CopyMemory(m_pbyteData, pBuffer_p, nSize_p);
}



/////////////////////////////////////////////////////////////////////////////
// class CRegVal	--	private members (for use in friend classes only)

BYTE * CRegVal :: AllocateDataBuffer( DWORD dwSize_p ) {
	if( m_pbyteData )
		HeapFree( m_hHeap, 0, m_pbyteData ) ;
	m_pbyteData = (BYTE *) HeapAlloc( m_hHeap, 0, dwSize_p ) ;
	if( ! m_pbyteData ) {
		TRACE0( "CRegVal::AllocateDataBuffer(): HeapAlloc() system call failed.\n" );
	}
	m_dwDataSize = dwSize_p ;
	return m_pbyteData ;
}


void CRegVal :: FreeDataBuffer() {
	if( m_pbyteData )
		HeapFree( m_hHeap, 0, m_pbyteData ) ;
	m_pbyteData = 0 ;
	m_dwDataSize = 0 ;
}

⌨️ 快捷键说明

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