📄 win32registryimpl.cpp
字号:
//// This file is part of the "More for C++" library//// Copyright (c) 1999-2003 by Thorsten Goertz (thorsten@goertz.com)//// The "More for C++" library is free software; you can redistribute it and/or// modify it under the terms of the license that comes with this package.//// Read "license.txt" for more details.//// THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED// WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES// OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.////////////////////////////////////////////////////////////////////////////////#ifndef _WIN32#define _WIN32#endif#include <windows.h>#include <more/util/vector.hpp>#include "win32registryimpl.hpp"using namespace more;using namespace more::os;using namespace more::os::win32;using namespace more::util;////////////////////////////////////////////////////////////////////////////////Win32RegistryImpl::Win32RegistryImpl( HKEY hBaseKey): m_hBaseKey( hBaseKey ){}////////////////////////////////////////////////////////////////////////////////void Win32RegistryImpl::createKey( const String& sKey) throw( InvalidKey, KeyAlreadyExists ){ HKEY hKey; if( sKey == "" ) { throw InvalidKey( ); } if( RegOpenKey( m_hBaseKey, sKey, &hKey ) == ERROR_SUCCESS ) { CloseHandle( hKey ); throw KeyAlreadyExists( ); } if( RegCreateKey( m_hBaseKey, sKey, &hKey ) != ERROR_SUCCESS ) { throw InvalidKey( ); } CloseHandle( hKey );}////////////////////////////////////////////////////////////////////////////////void Win32RegistryImpl::destroyKey( const String& sKey) throw( InvalidKey ){ HKEY hKey; if( RegOpenKey( m_hBaseKey, sKey, &hKey ) != ERROR_SUCCESS ) { throw InvalidKey( ); } CloseHandle( hKey ); if( RegDeleteKey( m_hBaseKey, sKey ) != ERROR_SUCCESS ) { throw InvalidKey( ); }}////////////////////////////////////////////////////////////////////////////////Array<String> Win32RegistryImpl::getSubKeys( const String& sKey) throw( InvalidKey ){ HandleAllocator allocator( m_hBaseKey, sKey ); p<Vector<String> > pResult; DWORD nIndex = 0; char cName[512]; pResult = CREATE Vector<String>( ); while( RegEnumKey( allocator.getHandle( ), nIndex++, cName, 511 ) == ERROR_SUCCESS ) { pResult -> add( sKey + "\\" + cName ); } return pResult -> toArray( );}////////////////////////////////////////////////////////////////////////////////Array<Win32Registry::NumericValue> Win32RegistryImpl::getNumericValues( const String& sKey) throw( InvalidKey ){ HandleAllocator allocator( m_hBaseKey, sKey ); p<Vector<NumericValue> > pResult; DWORD nIndex = 0; NumericValue numericValue; DWORD nDword; DWORD nMaxDwordLength = sizeof( DWORD ); pResult = CREATE Vector<NumericValue>( ); while( getValue( allocator.getHandle( ), nIndex, REG_DWORD, numericValue.sName, &nDword, nMaxDwordLength ) ) { numericValue.nValue = nDword; pResult -> add( numericValue ); } return pResult -> toArray( );}////////////////////////////////////////////////////////////////////////////////Array<Win32Registry::StringValue> Win32RegistryImpl::getStringValues( const String& sKey) throw( InvalidKey ){ HandleAllocator allocator( m_hBaseKey, sKey ); p<Vector<StringValue> > pResult; DWORD nIndex = 0; StringValue stringValue; unsigned char cString[512]; pResult = CREATE Vector<StringValue>( ); while( getValue( allocator.getHandle( ), nIndex, REG_SZ, stringValue.sName, cString, 511 ) ) { stringValue.sValue = ( const char* ) cString; pResult -> add( stringValue ); } return pResult -> toArray( );}////////////////////////////////////////////////////////////////////////////////void Win32RegistryImpl::createNumericValue( const String& sKey, const NumericValue& value) throw( InvalidKey, InvalidValueName, ValueAlreadyExists ){ HandleAllocator allocator( m_hBaseKey, sKey ); DWORD lType; BYTE bytes[512]; DWORD nLength = 511; CONST BYTE* pData = ( CONST BYTE* ) &value.nValue; if( RegQueryValueEx( allocator.getHandle( ), value.sName, NULL, &lType, bytes, &nLength ) == ERROR_SUCCESS ) { throw ValueAlreadyExists( ); } if( RegSetValueEx( allocator.getHandle( ), value.sName, 0, REG_DWORD, pData, sizeof( value.nValue ) ) != ERROR_SUCCESS ) { throw InvalidValueName( ); }}////////////////////////////////////////////////////////////////////////////////void Win32RegistryImpl::createStringValue( const String& sKey, const StringValue& value) throw( InvalidKey, InvalidValueName, ValueAlreadyExists ){ HandleAllocator allocator( m_hBaseKey, sKey ); DWORD lType; BYTE bytes[512]; DWORD nLength = 511; CONST BYTE* pData = ( CONST BYTE* ) ( const char* ) value.sValue; if( RegQueryValueEx( allocator.getHandle( ), value.sName, NULL, &lType, bytes, &nLength ) == ERROR_SUCCESS ) { throw ValueAlreadyExists( ); } if( RegSetValueEx( allocator.getHandle( ), value.sName, 0, REG_SZ, pData, value.sValue.getLength( ) + 1 ) != ERROR_SUCCESS ) { throw InvalidValueName( ); }}////////////////////////////////////////////////////////////////////////////////void Win32RegistryImpl::destroyValue( const String& sKey, const String& sName) throw( InvalidKey, InvalidValueName ){ HandleAllocator allocator( m_hBaseKey, sKey ); if( RegDeleteValue( allocator.getHandle( ), sName ) != ERROR_SUCCESS ) { throw InvalidValueName( ); }}////////////////////////////////////////////////////////////////////////////////void Win32RegistryImpl::setNumericValue( const String& sKey, const NumericValue& value) throw( InvalidKey, InvalidValueName ){ HandleAllocator allocator( m_hBaseKey, sKey ); DWORD lType; BYTE bytes[512]; DWORD nLength = 511; CONST BYTE* pData = ( CONST BYTE* ) &value.nValue; if( RegQueryValueEx( allocator.getHandle( ), value.sName, NULL, &lType, bytes, &nLength ) != ERROR_SUCCESS ) { throw InvalidValueName( ); } if( lType != REG_DWORD ) { throw InvalidValueName( ); } if( RegSetValueEx( allocator.getHandle( ), value.sName, 0, REG_DWORD, pData, sizeof( DWORD ) ) != ERROR_SUCCESS ) { throw InvalidValueName( ); }}////////////////////////////////////////////////////////////////////////////////void Win32RegistryImpl::setStringValue( const String& sKey, const StringValue& value) throw( InvalidKey, InvalidValueName ){ HandleAllocator allocator( m_hBaseKey, sKey ); DWORD lType; BYTE bytes[512]; DWORD nLength = 511; CONST BYTE* pData = ( CONST BYTE* ) ( const char* ) value.sValue; if( RegQueryValueEx( allocator.getHandle( ), value.sName, NULL, &lType, bytes, &nLength ) != ERROR_SUCCESS ) { throw InvalidValueName( ); } if( lType != REG_SZ ) { throw InvalidValueName( ); } if( RegSetValueEx( allocator.getHandle( ), value.sName, 0, REG_SZ, pData, value.sValue.getLength( ) + 1 ) != ERROR_SUCCESS ) { throw InvalidValueName( ); }}////////////////////////////////////////////////////////////////////////////////size_t Win32RegistryImpl::getNumericValue( const String& sKey, const String& sName) throw( InvalidKey, InvalidValueName ){ HandleAllocator allocator( m_hBaseKey, sKey ); DWORD lType; DWORD nDword; DWORD nLength = sizeof( DWORD ); if( RegQueryValueEx( allocator.getHandle( ), sName, NULL, &lType, ( BYTE* ) &nDword, &nLength ) != ERROR_SUCCESS ) { throw InvalidValueName( ); } if( lType != REG_DWORD ) { throw InvalidValueName( ); } return nDword;}////////////////////////////////////////////////////////////////////////////////String Win32RegistryImpl::getStringValue( const String& sKey, const String& sName) throw( InvalidKey, InvalidValueName ){ HandleAllocator allocator( m_hBaseKey, sKey ); DWORD lType; BYTE bytes[512]; DWORD nLength = 511; if( RegQueryValueEx( allocator.getHandle( ), sName, NULL, &lType, bytes, &nLength ) != ERROR_SUCCESS ) { throw InvalidValueName( ); } if( lType != REG_SZ ) { throw InvalidValueName( ); } return ( const char* ) bytes;}////////////////////////////////////////////////////////////////////////////////bool Win32RegistryImpl::getValue( HKEY hKey, DWORD& rnIndex, DWORD nTypeFilter, String& rsName, void* pBuffer, DWORD nBufferSize){ bool bResult = false; char cName[512] = { 0x00 }; DWORD nNameLen = 511; DWORD nType; DWORD nValueSize = nBufferSize; while( !bResult && RegEnumValue( hKey, rnIndex++, cName, &nNameLen, NULL, &nType, ( unsigned char* ) pBuffer, &nValueSize ) == ERROR_SUCCESS ) { if( nType == nTypeFilter ) { bResult = true; rsName = cName; } else { cName[0] = 0x00; nNameLen = 511; nValueSize = nBufferSize; } } return bResult;}////////////////////////////////////////////////////////////////////////////////Win32RegistryImpl::HandleAllocator::HandleAllocator( HKEY hBaseKey, const String& sKey){ if( RegOpenKey( hBaseKey, sKey, &m_hKey ) != ERROR_SUCCESS ) { throw InvalidKey( ); }}////////////////////////////////////////////////////////////////////////////////Win32RegistryImpl::HandleAllocator::~HandleAllocator( ){ CloseHandle( m_hKey );}////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -