www.gusucode.com > 采集网页选定部分全攻略C++源码程序 > 采集网页选定部分全攻略/collewpgtst2/test2/Registry.cpp

    // Registry.cpp

#include "stdafx.h"
#include "Registry.h"

CRegistry::CRegistry()
{

	m_hKey = NULL;

}

CRegistry::CRegistry( HKEY hKey, const char *lpszSubKey )
{

	Open( hKey, lpszSubKey );

}

CRegistry::~CRegistry()
{

	Close();

}

BOOL CRegistry::Open( HKEY hKey, const char *lpszSubKey )
{

//	Close();

	if( ::RegOpenKeyEx( hKey, lpszSubKey, 0, KEY_ALL_ACCESS, &m_hKey ) != ERROR_SUCCESS ){
		m_dwLastError = GetLastError();
		return( FALSE );
		}

	return( TRUE );

}

BOOL CRegistry::Close( void )
{
	BOOL bRet = TRUE;

	if( m_hKey == NULL )
		return( FALSE );

	bRet = ( ::RegCloseKey( m_hKey ) == ERROR_SUCCESS );
	m_hKey = NULL;

	return( bRet );

}

BOOL CRegistry::Read( const char *lpszValueName, LPVOID lpReturnBuffer )
{

	if( m_hKey == NULL )
		return( FALSE );

	DWORD dwSize = 5000;
	BOOL bRet = ( ::RegQueryValueEx( m_hKey, lpszValueName, NULL, NULL, (unsigned char *) lpReturnBuffer, &dwSize ) == ERROR_SUCCESS );

	m_dwLastError = GetLastError();

	return( bRet );

}

BOOL CRegistry::ReadDWORD( const char *lpszValueName, DWORD *pdwData, DWORD *pdwLastError )
{

	if( m_hKey == NULL )
		return( FALSE );

	BOOL bRet = Read( lpszValueName, pdwData );

	if( pdwLastError != NULL )
		*pdwLastError = m_dwLastError;

	return( bRet );

}

BOOL CRegistry::ReadString( const char *lpszValueName, LPVOID lpReturnBuffer, DWORD *pdwLastError )
{

	if( m_hKey == NULL )
		return( FALSE );

	BOOL bRet = Read( lpszValueName, lpReturnBuffer );

	if( pdwLastError != NULL )
		*pdwLastError = m_dwLastError;

	return( bRet );

}

BOOL CRegistry::WriteDWORD( const char *lpszValueName, DWORD dwData, DWORD *pdwLastError )
{

	if( m_hKey == NULL )
		return( FALSE );

	BOOL bRet = Write( lpszValueName, &dwData, REG_DWORD, sizeof( DWORD ) );

	if( pdwLastError != NULL )
		*pdwLastError = m_dwLastError;

	return( bRet );

}

BOOL CRegistry::WriteString( const char *lpszValueName, LPVOID lpData, DWORD *pdwLastError )
{

	if( m_hKey == NULL )
		return( FALSE );

	BOOL bRet = Write( lpszValueName, lpData, REG_SZ, (DWORD) strlen( (const char *) lpData ) + 1 );

	if( pdwLastError != NULL )
		*pdwLastError = m_dwLastError;

	return( bRet );

}

BOOL CRegistry::Write( const char *lpszValueName, LPVOID lpData, DWORD dwType, int nSize )
{

	if( m_hKey == NULL )
		return( FALSE );

	DWORD dwSize = 500;
	BOOL bRet = ( ::RegSetValueEx( m_hKey, lpszValueName, 0, dwType, (unsigned char *) lpData, nSize ) == ERROR_SUCCESS );

	m_dwLastError = GetLastError();

	return( bRet );

}

BOOL CRegistry::CreateKey( HKEY hKey, const char *lpszSubKey, const char *lpszClass )
{
	HKEY hOpenedKey;
	DWORD dwDisposition;
	DWORD dwLastError;

	BOOL bRet = ( ::RegCreateKeyEx( hKey, lpszSubKey, 0, (char *) lpszClass, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hOpenedKey, &dwDisposition ) == ERROR_SUCCESS );
	if( bRet ) ::RegCloseKey( hOpenedKey );
	dwLastError = GetLastError();

	return( bRet );

}

BOOL CRegistry::DeleteKey( HKEY hKey, const char *lpszSubKey )
{
	BOOL bRet;
	DWORD dwLastError;

	bRet = ( ::RegDeleteKey( hKey, lpszSubKey ) == ERROR_SUCCESS );
	dwLastError = GetLastError();

	return( bRet );

}