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

📄 registry.cpp

📁 A Model-View-Controller Framework that integrates with the MFC Doc/View architecture.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		//@rdesc Nonzero if key was deleted, otherwise 0.
		//@parm  LPCTSTR | lpszKeyToDelete | Name of the key to delete.
		//@parmopt  BOOL | bRecursive | FALSE | Indicates recursive delete
		// operation is requested.
		//@comm Deletes the named registry key. If bRecursive is set to
		// TRUE, it will delete keys in the registry which have subkeys.
		//
		// The name of the key to delete can be a relative name (to the 
		// currently open key), or can be an absolute key name.
		//
		// Unless the current key was deleted by this operation, the 
		// current open key is not affected for this Registry instance.
		// If however, the current key was deleted, the current key is 
		// set to the parent key of the deleted key.
		//
		//@devnote The recursive function is not available in the 16 bit
		// version.
		//@xref <c Registry> <mf Registry::DeleteValue> <mf Registry::Close>
		//@end
		BOOL Registry::DeleteKey( LPCTSTR lpszKeyToDelete, BOOL bRecursive /* = FALSE */)
		{
			ASSERT_VALID( this );
			ASSERT( lpszKeyToDelete != NULL );

			Registry reg;
			BOOL bSubKey = FALSE; // assume the key is an absolute path
			BOOL bSuccess = TRUE; // assume no failure so far

			if (NULL == lpszKeyToDelete)
			{

				m_lErrorCode = ERROR_INVALID_PARAMETER;
				return( FALSE );
			}

			CString strKeyToDelete(lpszKeyToDelete);

			/*
			** Whoa Nelly! If this is a recursive delete, fail if the 
			** key to delete is a system root key
			*/

			if (bRecursive && (NULL != lpszKeyToDelete) && !(_tcslen(lpszKeyToDelete)))
			{

				if (   (m_hRegistry == HKEY_CLASSES_ROOT)
					|| (m_hRegistry == HKEY_CURRENT_USER)
					|| (m_hRegistry == HKEY_LOCAL_MACHINE)
					|| (m_hRegistry == HKEY_USERS)
		#ifndef UNDER_CE
					|| (m_hRegistry == HKEY_PERFORMANCE_DATA)

		#if(WINVER >= 0x0400)			
					|| (m_hRegistry == HKEY_CURRENT_CONFIG)
					|| (m_hRegistry == HKEY_DYN_DATA)
		#endif /* WINVER >= 0x0400 */
		#endif  //UNDER_CE (WindowsCE)

					)
				{

					// A recursive delete from an empty key name will remove 
					// all subkeys from the root key.  This assert is here 
					// as a warning that this condition was present.
					ASSERT(FALSE);

					m_lErrorCode = ERROR_INVALID_PARAMETER;
					return FALSE;
				}

			}

		//	if (!reg.Connect(m_hRegistry, m_bRemote?m_strComputerName:(LPCTSTR)NULL, FALSE))
			if (!reg.Connect(StrToKey(m_strRegistryName), m_bRemote?m_strComputerName:(LPCTSTR)NULL, FALSE))
			{
				m_lErrorCode = reg.m_lErrorCode;
				return FALSE;
			}

			/*
			** Can the key be opened relative to the current key?
			*/

			if (strKeyToDelete.Find('\\') != 0) {
				CString strKey = ConcatenateKeys(m_strKeyName, lpszKeyToDelete);

				if (reg.Open(strKey))
					bSubKey = TRUE;
			}

			/*
			** else, try it as an absolute key name...
			*/
			if (!bSubKey) {
				if (!reg.Open(lpszKeyToDelete))
				{
					m_lErrorCode = reg.m_lErrorCode;
					return FALSE;
				}
			}


			if (bRecursive)
			{
				/*
				** Now do a recursive pre-order traversal.
				*/
				CString strEnumKey;
				
				while(reg.EnumerateKeys(0, strEnumKey) && bSuccess) {
					bSuccess = reg.DeleteKey(strEnumKey, TRUE);
					m_lErrorCode = reg.m_lErrorCode;
					}	

			}

			/*
			** Now delete the leaf whether recursive or not...
			*/

			if (bSuccess)
			{

				/*
				** Is the key a full path?
				*/

				if (bSubKey)
				{
					/*
					** User had not given us a full path so assume the name of the key he passed us
					** is a key off of the current key
					*/

					m_lErrorCode = ::RegDeleteKey( m_hKey, lpszKeyToDelete);
				}
				else
				{
					/*
					** You can't delete a key given a full path. What you have to do is back up 
					** one level and then do a delete
					*/
					
					CString strFullKeyName = lpszKeyToDelete;
					CString strParent;
					CString strChild;

					if ( strFullKeyName.Find( '\\' ) == (-1) )
					{
						strParent = "";
						strChild = lpszKeyToDelete;
					}

					else 
					{
						int nLastBackslashLocation = strFullKeyName.GetLength() - 1;

						/*
						** We know this loop will succeed because a back slash was found 
						** in the above if statement
						*/

						while( strFullKeyName[ nLastBackslashLocation ] != '\\' )
						{
							nLastBackslashLocation--;
						}

						CString strOpenKeyName = m_strKeyName;

						strParent = strFullKeyName.Left( nLastBackslashLocation );
						strChild = strFullKeyName.Right( ( strFullKeyName.GetLength() - nLastBackslashLocation ) - 1 );
					}

					/*
					** Now we open the parent key and delete the child
					** (Use the temporary Registry object so as not to
					** change the current key of this object!)
					*/

					if ( reg.Open( strParent ) == TRUE )
					{
						m_lErrorCode = ::RegDeleteKey( reg.m_hKey, strChild );

						/*
						** Now, make sure that the current key is still valid
						** (i.e., that it wasn't a key that was deleted)
						*/

						if (!reg.Open(m_strKeyName)) 
							Open(strParent);

					}
					else
					{
						m_lErrorCode = reg.m_lErrorCode;
						return( FALSE );
					}
				}
			}

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

		//@doc Registry
		//@mfunc Deletes the named registry value of the current key.
		//@rdesc Nonzero if value was deleted, otherwise 0.
		//@parm  LPCTSTR | lpszValueToDelete  | Name of value to delete.
		//@comm Deletes the named registry value of the current key of Registry.
		//@devnote For 32-bit only.
		//@xref <c Registry> <mf Registry::DeleteKey> <mf Registry::Close>
		BOOL Registry::DeleteValue( LPCTSTR lpszValueToDelete )
		{
			ASSERT_VALID( this );

			/*
			** lpszValueToDelete can be NULL
			*/

			m_lErrorCode = ::RegDeleteValue( m_hKey, (LPTSTR) lpszValueToDelete );

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


		//@doc Registry
		//@mfunc Enumerates subkeys of the currently open key.
		//@syntax (32-bit) EnumerateKeys(const DWORD dwSubkeyIndex,
		// CString& strSubkeyName);
		//@syntax (32-bit) EnumerateKeys(const DWORD dwSubkeyIndex,
		// CString& strSubkeyName,
		// CString& strClassName );
		//@syntax (32-bit) EnumerateKeys(const DWORD dwSubkeyIndex,
		// LPTSTR lpszSubkeyName,
		// LPDWORD lpdwSizeSubkeyName,
		// LPTSTR lpszClassName, 
		// LPDWORD lpdwSizeClassName);
		//@syntax (16-bit) EnumerateKeys( const DWORD dwIndex, 
		// LPTSTR lpszBuffer, 
		// DWORD dwBufferSize);
		//@rdesc Nonzero if subkey was enumerated, otherwise 0.
		//@comm The 32-bit version encapsulates RegEnumerateKeys API, 
		// the 16-bit version encapsulates RegEnumKey.
		//@parm const DWORD | dwSubkeyIndex | Index of desired subkey
		//@parm CString& | strSubkeyName | String to receive the
		// enumerated key name.
		//@parm CString& | strClassName | String to receive the 
		// enumerated class name.
		//@parm LPTSTR | lpszSubkeyName | Buffer to receive the
		// enumerated key name
		//@parm LPDWORD | lpdwSizeSubkeyName | The size of the 
		// lpszSubkeyName buffer in bytes
		//@parm LPTSTR | lpszClassName | Buffer to receive the 
		// enumerated class name
		//@parm LPDWORD | lpdwSizeClassName | The size of the 
		// lpdwSizeClassName buffer in bytes.
		//@xref <c Registry> <mf Registry::EnumerateValues>
		//@ex | UINT nIndex = 0;
		//CString strKeyName;
		//while (EnumerateKeys(nIndex++, strKeyName))
		//    m_listbox.AddString(strKeyName);
		//@end
		BOOL Registry::EnumerateKeys(const DWORD dwSubkeyIndex,
										LPTSTR lpszSubkeyName,
										LPDWORD lpdwSizeSubkeyName,
										LPTSTR lpszClassName, /* = NULL */
										LPDWORD lpdwSizeClassName /* = NULL */) 
		{
			ASSERT(lpszSubkeyName);
			ASSERT(lpdwSizeSubkeyName);
			// lpszClassName can be NULL
			// lpdwClassNameSize can be NULL

			m_lErrorCode = ::RegEnumKeyEx( m_hKey, 
							dwSubkeyIndex, 
							lpszSubkeyName, 
							lpdwSizeSubkeyName,
							NULL,
							lpszClassName,
							lpdwSizeClassName,
							&m_fileTimeLastWrite );

			return m_lErrorCode;	

		}

		BOOL Registry::EnumerateKeys(const DWORD dwSubkeyIndex,
										CString& strSubkeyName) 
		{
			TCHAR szSubkeyName[ 2048 ];
			DWORD dwSizeSubkeyName = sizeof( szSubkeyName ) - 1;

			LONG lResult;
			
			lResult = EnumerateKeys(dwSubkeyIndex, 
									(LPTSTR)szSubkeyName, &dwSizeSubkeyName, 
									NULL, NULL);

			if ((ERROR_SUCCESS == lResult) || (ERROR_MORE_DATA == lResult))
			{
				strSubkeyName = szSubkeyName;
				return TRUE;
			}

			return FALSE;

		}


		BOOL Registry::EnumerateKeys(const DWORD dwSubkeyIndex,
										CString& strSubkeyName,
										CString& strClassName )
		{
			ASSERT_VALID( this );

			TCHAR szSubkeyName[ 2048 ];
			TCHAR szClassName [ 2048 ];

			DWORD dwSizeSubkeyName = sizeof( szSubkeyName ) - 1;
			DWORD dwSizeClassName  = sizeof( szClassName  ) - 1;

			LONG lResult;

			lResult = EnumerateKeys(dwSubkeyIndex,
									szSubkeyName, &dwSizeSubkeyName,
									szClassName, &dwSizeClassName);

			if ((ERROR_SUCCESS == lResult) || (ERROR_MORE_DATA == lResult))
			{
				strSubkeyName = szSubkeyName;
				strClassName  = szClassName;

				return( TRUE );
			}
			return( FALSE );
		}

		//@doc Registry
		//@mfunc Enumerates values of the currently open key.
		// EnumerateValues( const DWORD dwValueIndex,
		// CString&	strValueName,
		// KeyValueTypes& type_code );
		//@syntax EnumerateValues( const DWORD dwValueIndex,
		// CString&	strValueName,
		// KeyValueTypes& type_code,
		// LPBYTE lpbDataBuffer,
		// DWORD& dwSizeDataBuffer );
		//@syntax EnumerateValues( const DWORD dwValueIndex,
		// CString& strValueName,
		// KeyValueTypes* pTypeCode,
		// LPBYTE lpbDataBuffer,
		// LPDWORD	lpdwSizeDataBuffer);
		//@rdesc Nonzero if value was deleted, otherwise 0.
		//@parm const DWORD | dwValueIndex | Index of value to query.
		//@parm CString& | strValueName | String to receive the 
		// enumerated value name.
		//@parm KeyValueTypes& | type_code | KeyValueType to receive
		// the data type.
		//@parm LPBYTE | lpbDataBuffer | A data buffer to receive
		// the value data.
		//@parm DWORD& | dwSizeDataBuffer | The size of the value
		// data buffer in bytes.
		//@comm Encapsulates the RegEnumValue() API.
		//@devnote For 32-bit only.  Check SECREG.H for optional parameters.
		//@xref <c Registry> <mf Registry::EnumerateKeys>
		//@end
		BOOL Registry::EnumerateValues( const DWORD dwValueIndex,
										  CString&		 strValueName,
										  KeyValueTypes& type_code,
										  LPBYTE			lpbDataBuffer,
										  DWORD&			dwSizeDataBuffer )
		{
			return EnumerateValues(	dwValueIndex, strValueName, 
									&type_code, lpbDataBuffer, &dwSizeDataBuffer);
		}

		BOOL Registry::EnumerateValues( const DWORD dwValueIndex,
							CString&		 strValueName,
							KeyValueTypes& type_code )
		{
			return EnumerateValues(	dwValueIndex, strValueName, 
									&type_code, NULL, NULL);
		}


		BOOL Registry::EnumerateValues( const DWORD dwValueIndex,
							CString&		 strValueName,
							KeyValueTypes* pTypeCode,			/* = NULL */
							LPBYTE			lpbDataBuffer,		/* = NULL */
							LPDWORD		  lpdwSizeDataBuffer	/* = NULL */)
		{

			ASSERT_VALID( this );

			/*
			** pTypeCode, lpbDataBuffer and dwSizeDataBuffer can be NULL
			*/

			DWORD dwTempCodeType;

			TCHAR szTempName[ 2048 ];

			DWORD dwTempNameSize = sizeof( szTempName );
			
			m_lErrorCode = ::RegEnumValue( m_hKey,
							dwValueIndex,
							szTempName,
							&dwTempNameSize,
							NULL,
							&dwTempCodeType,
							lpbDataBuffer,
							lpdwSizeDataBuffer );

			
			// LH: check for success or "more data" return codes
			// Note:  This ERROR_MORE_DATA return code is not listed in the 
			//		  Windows documentation for this function.
			if (( ERROR_SUCCESS == m_lErrorCode )  || ( ERROR_MORE_DATA == m_lErrorCode ))
			{
				if (pTypeCode)
				{
					*pTypeCode = (KeyValueTypes) dwTempCodeType;
				}

				strValueName  = szTempName;
				return( TRUE );

⌨️ 快捷键说明

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