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

📄 registry.cpp

📁 A Model-View-Controller Framework that integrates with the MFC Doc/View architecture.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
			}
			return( FALSE );
		}

		//@doc Registry
		//@mfunc Writes the attributes of the currently open key.
		//@rdesc Nonzero if registry was flushed, otherwise 0.
		//@comm  Encapsulates RegFlushKey() API.
		//@devnote For 32-bit only.
		//@xref <c Registry> <mf Registry::Open> <mf Registry::Close>
		BOOL Registry::Flush( void )
		{
			ASSERT_VALID( this );

			m_lErrorCode = ::RegFlushKey( m_hKey );

			if ( ERROR_SUCCESS == m_lErrorCode )
			{
				return( TRUE );
			}
			return( FALSE );
		}

		//@doc Registry
		//@mfunc Retrieves a REG_BINARY value.
		//@rdesc Nonzero if successful, otherwise 0.
		//@parm  LPCTSTR | lpszValueName | The value name.
		//@parm  CByteArray& | return_array  | A CByteArray
		// to receive the binary data.
		//@devnote For 32-bit only.
		//@xref <c Registry> <mf Registry::GetValue> <mf Registry::SetBinaryValue>
		BOOL Registry::GetBinaryValue( LPCTSTR lpszValueName, CByteArray& return_array )
		{
			ASSERT_VALID( this );
			ASSERT( lpszValueName != NULL );

			if ( NULL == lpszValueName )
			{
				m_lErrorCode = ERROR_INVALID_PARAMETER;
				return( FALSE );
			}

			DWORD dwBufferSize = m_dwLongestValueDataLength;

			LPBYTE lpbMemoryBuffer = (LPBYTE) ::malloc( dwBufferSize );

			if ( NULL == lpbMemoryBuffer )
			{
				m_lErrorCode = ::GetLastError();
				return( FALSE );
			}

			BOOL bReturn = TRUE;

			KeyValueTypes type = typeBinary;

			if ( QueryValue( lpszValueName, type, lpbMemoryBuffer, dwBufferSize ) == TRUE )
			{
				/*
				** We've got data so give it back to the caller
				*/

				return_array.RemoveAll();

				DWORD dwIndex = 0;

				while( dwIndex < dwBufferSize )
				{
					return_array.Add( lpbMemoryBuffer[ dwIndex ] );
					dwIndex++;
				}

				bReturn = TRUE;
			}
			else
			{
				bReturn = FALSE;
			}

			::free( lpbMemoryBuffer );

			return( bReturn );
		}

		//@doc Registry
		//@mfunc Retrieves a REG_DWORD value (32-bit only).
		//@rdesc BOOL 
		//@devnote For 32-bit only.
		//@parm  LPCTSTR | lpszValueName | The value name.
		//@parm  DWORD& | dwReturnValue | A DWORD to receive
		// the data.
		//@xref <c Registry> <mf Registry::GetValue> <mf Registry::SetDoubleWordValue>
		BOOL Registry::GetDoubleWordValue( LPCTSTR lpszValueName, DWORD& dwReturnValue )
		{
			 ASSERT_VALID( this );
			 ASSERT( lpszValueName != NULL );

			if ( NULL == lpszValueName )
			{
				m_lErrorCode = ERROR_INVALID_PARAMETER;
				return( FALSE );
			}

			DWORD dwBufferSize = sizeof( DWORD );

			KeyValueTypes type = typeDoubleWord;

			return( QueryValue( lpszValueName, type, (LPBYTE) &dwReturnValue, dwBufferSize ) );
		}

		#ifndef UNDER_CE
		//@doc Registry
		//@mfunc Encapsulates the RegGetSecurity() call.
		//@rdesc Nonzero if security information was successfully retrieved, otherwise 0.
		//@devnote For 32-bit only.
		//@parm  const SECURITY_INFORMATION | security_info | Descriptor structure 
		// of what security information is being requested.
		//@parm PSECURITY_DESCRIPTOR | lpbDataBuffer | A buffer to receive the
		// requested information.
		//@parm DWORD& | dwSizeDataBuffer | Size of retrieved buffer.
		//@xref <c Registry> <mf Registry::GetValue>
		BOOL Registry::GetSecurity( const SECURITY_INFORMATION security_info,
									  PSECURITY_DESCRIPTOR	lpbDataBuffer,
									  DWORD&					dwSizeDataBuffer )
		{
			ASSERT_VALID( this );
			ASSERT( lpbDataBuffer != NULL );

			if ( NULL == lpbDataBuffer )
			{
				m_lErrorCode = ERROR_INVALID_PARAMETER;
				return( FALSE );
			}

			m_lErrorCode = ::RegGetKeySecurity( m_hKey,
												security_info,
												lpbDataBuffer,
												&dwSizeDataBuffer );

			if ( ERROR_SUCCESS == m_lErrorCode )
			{
				return( TRUE );
			}
			return( FALSE );
		}
		#endif //UNDER_CE (WindowsCE)
		//@doc Registry
		//@mfunc Retrieves a REG_SZ value.
		//@rdesc Nonzero if successful, otherwise 0.
		//@devnote For 32-bit only.
		//@parm  LPCTSTR | lpszValueName | The name of the value.
		//@parm  CString& | strReturn | A CString to receive the string data.
		//@xref <c Registry> <mf Registry::GetValue> <mf Registry::SetStringValue>
		BOOL Registry::GetStringValue( LPCTSTR lpszValueName, CString& strReturn )
		{
			ASSERT_VALID( this );
			ASSERT( lpszValueName != NULL );

			if ( NULL == lpszValueName )
			{
				m_lErrorCode = ERROR_INVALID_PARAMETER;
				return( FALSE );
			}

			TCHAR szTemp[ 2048 ];
			DWORD dwBufferSize = 2048;

		#if defined(WIN32)
			::ZeroMemory( szTemp, sizeof( szTemp ) );
		#endif //win32

			KeyValueTypes type = typeString;

			if ( QueryValue( lpszValueName, type, (LPBYTE) szTemp, dwBufferSize ) == TRUE )
			{
				strReturn = szTemp;
				return( TRUE );
			}
			else
			{
				strReturn.Empty();
				return( FALSE );
			}
		}

		//@doc Registry
		//@mfunc Retrieves a REG_MULTI_SZ value.
		//@rdesc Nonzero if successful, otherwise 0.
		//@devnote For 32-bit only.
		//@parm  LPCTSTR | lpszValueName | The value name.
		//@parm  CStringArray& | return_array | A CStringArray to receive
		// the string data.
		//@xref <c Registry> <mf Registry::GetValue> <mf Registry::SetStringArrayValue>
		BOOL Registry::GetStringArrayValue( LPCTSTR lpszValueName, CStringArray& return_array )
		{
			ASSERT_VALID( this );
			ASSERT( lpszValueName != NULL );

			if ( NULL == lpszValueName )
			{
				m_lErrorCode = ERROR_INVALID_PARAMETER;
				return( FALSE );
			}

			DWORD dwBufferSize = m_dwLongestValueDataLength;

			LPBYTE lpbMemoryBuffer = (LPBYTE) ::malloc( dwBufferSize );

			if ( NULL == lpbMemoryBuffer )
			{
				m_lErrorCode = ::GetLastError();
				return( FALSE );
			}

			BOOL bReturn = TRUE;

			KeyValueTypes type = typeMultipleString; // A double NULL terminated string

			if ( QueryValue( lpszValueName, type, lpbMemoryBuffer, dwBufferSize ) == TRUE )
			{
				/*
				** We've got data so give it back to the caller
				*/

				LPTSTR lpszStrings = (LPTSTR) lpbMemoryBuffer;

				return_array.RemoveAll();

				DWORD dwLen = 0;
				while(((dwLen*sizeof(TCHAR)) < (dwBufferSize-sizeof(TCHAR))))
				{
					return_array.Add( (LPCTSTR) lpszStrings );
					int nLen = (int)_tcslen( (LPCTSTR) lpszStrings) + 1;
					lpszStrings += nLen;
					dwLen += nLen;
				}

				bReturn = TRUE;
			}
			else
			{
				bReturn = FALSE;
			}

			::free( lpbMemoryBuffer );

			return( bReturn );
		}

		//@doc Registry
		//@mfunc Retrieves the value of the current key.
		//@syntax virtual BOOL GetValue(LPCTSTR lpszValueName, CByteArray& return_array);
		//@syntax virtual BOOL GetValue(LPCTSTR lpszValueName, DWORD& dwReturnValue);
		//@syntax virtual BOOL GetValue(LPCTSTR lpszValueName, CStringArray& return_array);
		//@syntax virtual BOOL GetValue(LPCTSTR lpszValueName, CString& strReturn);
		//@rdesc Nonzero if value was successfully retrieved, otherwise 0.
		//@comm Retrieves data from named value.  Encapsulates RegQueryValueEx API.
		//
		//These methods assume the type of data is known when called.  This can
		// be determined by either enumerating the data with 
		// <mf Registry::EnumerateValues>, or calling <mf Registry::QueryValue>.
		//@devnote For 32-bit only.
		//@parm LPCTSTR | lpszValueName | The value name
		//@parm CByteArray& |return_array | A CByteArray to receive binary data.
		//@parm DWORD& | dwReturnValue | A DWORD to receive DWORD data
		//@parm CStringArray& | return_array | A CStringArray to receive
		// multi-string data.
		//@parm CString& | strReturn | A CString to receive string data.
		//@xref <c Registry> <mf Registry::Open> <mf Registry::Close>
		// <mf Registry::EnumerateValues> <mf Registry::QueryValue>
		// <mf Registry::GetBinaryValue> <mf Registry::GetDoubleWordValue>
		// <mf Registry::GetStringValue> <mf Registry::GetStringArrayValue>
		// <mf Registry::SetValue>
		BOOL Registry::GetValue( LPCTSTR lpszValueName, CByteArray& return_array )
		{
			ASSERT_VALID( this );
			ASSERT( lpszValueName != NULL );

			if ( NULL == lpszValueName )
			{
				m_lErrorCode = ERROR_INVALID_PARAMETER;
				return( FALSE );
			}

			return( GetBinaryValue( lpszValueName, return_array ) );
		}

		BOOL Registry::GetValue( LPCTSTR lpszValueName, DWORD& dwReturnValue )
		{
			ASSERT_VALID( this );
			ASSERT( lpszValueName != NULL );

			if ( NULL == lpszValueName )
			{
				m_lErrorCode = ERROR_INVALID_PARAMETER;
				return( FALSE );
			}

			return( GetDoubleWordValue( lpszValueName, dwReturnValue ) );
		}

		BOOL Registry::GetValue( LPCTSTR lpszValueName, CString& strReturn )
		{
			ASSERT_VALID( this );
			ASSERT( lpszValueName != NULL );

			if ( NULL == lpszValueName )
			{
				m_lErrorCode = ERROR_INVALID_PARAMETER;
				return( FALSE );
			}

			return( GetStringValue( lpszValueName, strReturn ) );
		}

		BOOL Registry::GetValue( LPCTSTR lpszValueName, CStringArray& return_array )
		{
			ASSERT_VALID( this );
			ASSERT( lpszValueName != NULL );

			if ( NULL == lpszValueName )
			{
				m_lErrorCode = ERROR_INVALID_PARAMETER;
				return( FALSE );
			}

			return( GetStringArrayValue( lpszValueName, return_array ) );
		}
		#ifndef UNDER_CE
		//@doc Registry
		//@mfunc Encapsulates the RegLoadKey API.
		//@rdesc Nonzero if loaded correctly, otherwise 0.
		//@parm  LPCTSTR | lpszSubkeyName | Nonzero if loaded correctly, otherwise 0.
		//@parm  LPCTSTR | lpszFileName | Name of file that stores the hive of registry info.
		//@comm Deletes the named registry value.  Based on the RegLoadKey API.
		//@devnote For 32-bit only.
		//@xref <c Registry> <mf Registry::Save>
		BOOL Registry::Load( LPCTSTR lpszSubkeyName, LPCTSTR lpszFileName )
		{
			ASSERT_VALID( this );
			ASSERT( lpszSubkeyName != NULL );
			ASSERT( lpszFileName != NULL );

			if ( NULL == lpszSubkeyName  || NULL == lpszFileName )
			{
				m_lErrorCode = ERROR_INVALID_PARAMETER;
				return( FALSE );
			}

			m_lErrorCode = ::RegLoadKey( m_hRegistry, lpszSubkeyName, lpszFileName );

			if ( ERROR_SUCCESS == m_lErrorCode )
			{
				return( TRUE );
			}
			return( FALSE );
		}

		//@doc Registry
		//@mfunc Indicates when a registry key has changed.  
		//@rdesc BOOL 
		//@comm Encapsulates RegNotifyChangeKeyValue API to indicate when a registry 
		// key has changed.  If bWaitForChange is TRUE, the wait is performed 
		// synchronously, and control returns immediately to the program.  
		// Otherwise the function will wait for the event to occur.  
		// The changes_to_be_reported parameter accepts the same arguments as 
		// the fdwNotifyFilter parameter in the RegNotifyChangeKeyValue API, and 
		// can include any combination of the following values:
		//@flag REG_NOTIFY_CHANGE_NAME | Changes to key names that occur in the 
		// specified key or in the specified key and its subkeys cause a change 
		// notification. This includes key creations and deletions.
		//@flag REG_NOTIFY_CHANGE_ATTRIBUTES | Attribute changes that occur in a 
		// key or in a key and its subkeys cause a change notification.
		//@flag REG_NOTIFY_CHANGE_LAST_SET | Changes to the last write time that 
		// occur in a key or in a key and its subkeys cause a change notification.
		//@flag REG_NOTIFY_CHANGE_SECURITY | Security-descriptor changes that occur 
		// in a key or in a key and its subkeys cause a change notification.
		//@devnote For 32-bit only.
		//@parm  const HANDLE | hEvent | Handle of signalled event.
		//@parm  const NotifyChangeFilter | reported_changes | Type of change 
		// to be reported (see Comments).
		//@parm  const BOOL | bAllSubkeys | Boolean flag to indicate whether any 
		// change in subkeys should cause notification.
		//@parm  const BOOL | bWaitForChange | Specifies how a change should be 
		// reported.
		//@xref <c Registry>
		BOOL Registry::NotifyChange( const HANDLE	hEvent,
									   const NotifyChangeFilter reported_changes,
									   const BOOL		bAllSubkeys,
									   const BOOL		bWaitForChange )
		{
			ASSERT_VALID( this );

			m_lErrorCode = ::RegNotifyChangeKeyValue( m_hKey,
							bAllSubkeys,
							reported_changes,
							hEvent,
							bWaitForChange);

			if ( ERROR_SUCCESS == m_lErrorCode )
			{
				return( TRUE );
			}
			return( FALSE );
		}
		#endif //UNDER_CE (WindowsCE)

		//@doc Registry
		//@mfunc Opens a registry key.
		//@syntax (32-bit) BOOL Open( LPCTSTR lpszSubkey, 
		// const CreatePermissions security_access_mask );
		//@syntax (16-bit) BOOL Open( LPCTSTR lpszSubkey);
		//@rdesc Nonzero if key was successfully opened, otherwise 0.
		//@parm  LPCTSTR | lpszSubkey | Name of key to open.
		//@parm  const CreatePermissions | security_access_mask  | Security flags 
		// to associate with the open key (see Comments).
		//@comm Opens the specified key with the requested permissions.  The 32-bit 
		// version encapsulates the RegOpenKeyEx API, while the 16-bit version 
		// encapsulates the RegOpenKey API.  The security_access_mask can be 
		// a combination of the following parameters:
		//@flag KEY_ALL_ACCESS | Combination of KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, 
		// KEY_NOTIFY, KEY_CREATE_SUB_KEY, KEY_CREATE_LINK, and KEY_SET_VALUE access.
		//@flag KEY_CREATE_LINK | Permission to create a symbolic link.
		//@flag KEY_CREATE_SUB_KEY | Permission to create subkeys.
		//@flag KEY_ENUMERATE_SUB_KEYS | Permission to enumerate subkeys.
		//@flag KEY_EXECUTE | Permission for read access.
		//@flag KEY_NOTIFY | Permission for change notification.
		//@flag KEY_QUERY_VALUE | Permission to query subkey data.
		//@flag KEY_READ | Combination of KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, 
		// and KEY_NOTIFY access.
		//@flag KEY_SET_VALUE | Permission to set subkey data.
		//@flag KEY_WRITE | Combination of KEY_SET_VALUE and 
		// KEY_CREATE_SUB_KEY access.

⌨️ 快捷键说明

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