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

📄 registry.cpp

📁 A Model-View-Controller Framework that integrates with the MFC Doc/View architecture.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
			while( nIndex < nNumStrings )
			{
				 _tcscpy_s( &lpszString[ nStringLength ], dwBufferSize, string_array[nIndex] );
				 nStringLength += string_array[nIndex].GetLength() + 1;

				nIndex++;
			}

			nStringLength++;

			BOOL bReturn = TRUE;

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

			if ( SetValue( lpszValueName, type, lpbMemoryBuffer, dwBufferSize ) != TRUE )
			{
				bReturn = FALSE;
			}

			::free( lpbMemoryBuffer );

			return( bReturn );
		}

		//@doc Registry
		//@mfunc Sets a value for the key name specified.
		//@syntax (32-bit) virtual BOOL SetValue(LPCTSTR lpszValueName, const CByteArray& bytes_to_write);
		//@syntax (32-bit) virtual BOOL SetValue(LPCTSTR lpszValueName, DWORD dwValue);
		//@syntax (32-bit) virtual BOOL SetValue(LPCTSTR lpszValueName, const CStringArray& strings_to_write);
		//@syntax (32-bit) virtual BOOL SetValue(LPCTSTR lpszValueName, const CString& strWrite);
		//@syntax (32-bit) virtual BOOL SetValue(LPCTSTR lpszValueName, const KeyValueTypes type_of_value_to_set, LPBYTE lpbValueData, const DWORD dwSize);
		//@syntax (16-bit) virtual BOOL SetValue(LPCTSTR lpSubKey, const CString& strWrite);
		//@rdesc Nonzero if value was successfully set, otherwise 0.
		//@parm LPCTSTR | lpszValueName | Name of value to set (32-bit).
		//@parm LPCTSTR | lpSubKey | Name of the subkey to set value for )16-bit).
		//@parm const CByteArray& | bytes_to_write | Array of bytes to write as binary type.
		//@parm DWORD | dwValue | DWORD value to write.
		//@parm const CStringArray& | strings_to_write | Array of strings to write.
		//@parm const CString&  | strWrite | Single string to write.
		//@parm const KeyValueTypes | type_of_value_to_set | Type of value to be set.  
		// Can be one of the following:
		//@flag typeBinary |
		//@flag typeDoubleWord |
		//@flag typeDoubleWordLittleEndian |
		//@flag typeDoubleWordBigEndian | 
		//@flag typeUnexpandedString |
		//@flag typeSymbolicLink |
		//@flag typeMultipleString |
		//@flag typeNone |
		//@flag typeResourceList |
		//@flag typeString |
		//@parm LPBYTE  | lpbValueData | Pointer to buffer of data to be set.
		//@parm const DWORD | dwSize | Size of buffer pointed to by lpbBuffer.
		//@comm Sets the named value with the data value passed in.  The 32-bit version
		// encapsulates the RegSetValueEx API, 16-bit encapsulates the RegSetValue API.
		//@xref <c Registry> <mf Registry::GetValue>
		BOOL Registry::SetValue( LPCTSTR lpszValueName, const CByteArray& bytes_to_write )
		{
			ASSERT_VALID( this );
			ASSERT( lpszValueName != NULL );

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

			return( SetBinaryValue( lpszValueName, bytes_to_write ) );
		}

		BOOL Registry::SetValue( LPCTSTR lpszValueName, DWORD dwValue )
		{
			ASSERT_VALID( this );
			ASSERT( lpszValueName != NULL );

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

			return( SetDoubleWordValue( lpszValueName, dwValue ) );
		}

		BOOL Registry::SetValue( LPCTSTR lpszValueName, const CStringArray& strings_to_write )
		{
			ASSERT_VALID( this );
			ASSERT( lpszValueName != NULL );

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

			return( SetStringArrayValue( lpszValueName, strings_to_write ) );
		}

		BOOL Registry::SetValue( LPCTSTR lpszValueName, const CString& strWrite )
		{
			ASSERT_VALID( this );
			ASSERT( lpszValueName != NULL );

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

			return( SetStringValue( lpszValueName, strWrite ) );
		}

		BOOL Registry::SetValue( LPCTSTR		lpszValueName, 
						 const KeyValueTypes	type_of_value_to_set, 
						 LPBYTE					lpbValueData, 
						 const DWORD			dwSize )
		{
			ASSERT_VALID( this );
			ASSERT( lpszValueName != NULL );
			ASSERT( lpbValueData != NULL );

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

			m_lErrorCode = ::RegSetValueEx( m_hKey,
											lpszValueName,
											0,
											type_of_value_to_set,
											lpbValueData,
											dwSize );
			
			if (  ERROR_SUCCESS == m_lErrorCode  )
			{
				return( TRUE );
			}
			return( FALSE );
		}
		#ifndef UNDER_CE
		//@doc Registry
		//@mfunc Encapsulates the RegUnLoadKey API.
		//@rdesc Nonzero if key was successfully unloaded, otherwise 0.
		//@parm  LPCTSTR lpszSubkey 
		//@comm Unloads the specified key and its subkeys from the registry.  
		// Encapsulates the RegUnLoadKey API.
		//@devnote For 32-bit only.
		//@xref <c Registry> <mf Registry::Load>
		BOOL Registry::UnLoad( LPCTSTR lpszSubkey )
		{
			ASSERT_VALID( this );
			ASSERT( lpszSubkey != NULL );

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

			m_lErrorCode = ::RegUnLoadKey( m_hKey, lpszSubkey );

			if (  ERROR_SUCCESS == m_lErrorCode  )
			{
				return( TRUE );
			}
			return( FALSE );
		}
		#endif //UNDER_CE (WindowsCE)
		// Utility routine to convert a string to the HKEY_ equiv.
		//@doc Registry
		//@mfunc Converts an HKEY constant to its string equivalent.
		//@rdesc Nonzero if successful, otherwise 0.
		//@parm HKEY | hKey | HKEY handle to convert.
		//@parm CString& | strKey | String to receive conversion
		//@comm This method maps the standard HKEY values to strings
		//
		//The keys this method recognizes (and their mapped strings) include:<nl>
		//HKEY_CLASSES_ROOT		"HKEY_CLASSES_ROOT"<nl>
		//HKEY_CURRENT_USER		"HKEY_CURRENT_USER"<nl>
		//HKEY_LOCAL_MACHINE	"HKEY_LOCAL_MACHINE"<nl>
		//HKEY_USERS			"HKEY_USERS"<nl>
		//HKEY_CURRENT_CONFIG	"HKEY_CURRENT_CONFIG"<nl>
		//HKEY_DYN_DATA			"HKEY_DYN_DATA"<nl>
		//
		//If a the key cannot be matched, the string reference
		// is not altered.
		//@xref <c Registry> <mf Registry::StrToKey>
		BOOL Registry::KeyToStr(HKEY hKey,CString& strKey) {

			// assume failure
			BOOL rc = FALSE;

			if ( HKEY_CLASSES_ROOT == hKey ) {
				strKey = _T("HKEY_CLASSES_ROOT");
				rc = TRUE;
			}
			else if ( HKEY_CURRENT_USER  == hKey ) {
				strKey = _T("HKEY_CURRENT_USER");
				rc = TRUE;
			}
			else if ( HKEY_LOCAL_MACHINE == hKey ) {
				strKey = _T("HKEY_LOCAL_MACHINE");
				rc = TRUE;
			}
			else if ( HKEY_USERS == hKey ) {
				strKey = _T("HKEY_USERS");
				rc = TRUE;
			}
		#ifndef UNDER_CE
			else if ( HKEY_CURRENT_CONFIG  == hKey ) {
				strKey = _T("HKEY_CURRENT_CONFIG");
				rc = TRUE;
			}
			else if ( HKEY_DYN_DATA  == hKey ) {
				strKey = _T("HKEY_DYN_DATA");
				rc = TRUE;
			}
		#endif //UNDER_CE (WindowsCE)

			// if no match was found, do not modify string,
			// simply return false

			return rc;
		}
			
		//@doc Registry
		//@mfunc Converts an string equivalent to its HKEY constant.
		//@rdesc The matching HKEY successful, otherwise 0.
		//@parm CString& | strKey | String to receive conversion
		//@comm This method maps the standard strings to HKEY values.
		// See <mf Registry::KeyToStr> for recognized strings.
		//@xref <c Registry> <mf Registry::KeyToStr>
		HKEY Registry::StrToKey(const CString& strKey){
			HKEY hKey=NULL;
			if(strKey==_T("HKEY_CLASSES_ROOT"))
				hKey=HKEY_CLASSES_ROOT;
			else if(strKey==_T("HKEY_CURRENT_USER"))
				hKey=HKEY_CURRENT_USER;
			else if(strKey==_T("HKEY_LOCAL_MACHINE"))
				hKey=HKEY_LOCAL_MACHINE;
			else if(strKey==_T("HKEY_USERS"))
				hKey=HKEY_USERS;
		#ifndef UNDER_CE
			else if(strKey==_T("HKEY_CURRENT_CONFIG"))
				hKey=HKEY_CURRENT_CONFIG;
			else if(strKey==_T("HKEY_DYN_DATA"))
				hKey=HKEY_DYN_DATA;
		#endif //UNDER_CE (WindowsCE)
				
			return hKey;		
		}

		#else // WIN16 routines

		BOOL Registry::Close( void )
		{
			ASSERT_VALID( this );

			BOOL bSuccess = TRUE;

			if ( m_hKey != (HKEY) NULL )
			{
				m_lErrorCode = ::RegCloseKey( m_hKey );

				if ( m_lErrorCode != ERROR_SUCCESS )
					bSuccess = FALSE;

				// if the key handle is equal to the registry handle, 
				// let's null out the registry handle so that we 
				// don't try to close the same handle twice below
				// (thus avoiding a return code of SUCCESS_INVALID_HANDLE)
				if (m_hKey == m_hRegistry) 
					m_hRegistry = (HKEY) NULL;

				m_hKey = (HKEY) NULL;

			}

			if ( m_hRegistry != (HKEY) NULL )
			{
				if (m_bCloseKeyOnDisconnect)
				{
					m_lErrorCode = ::RegCloseKey( m_hRegistry );

					if ( m_lErrorCode != ERROR_SUCCESS )
						bSuccess = FALSE;
				}

				m_hRegistry = (HKEY) NULL;
			}

			Initialize();

			return bSuccess;
		}

		BOOL Registry::Create( LPCTSTR lpszSubkeyName, 
						LPCTSTR lpszClassName
					  )
		{
			ASSERT_VALID( this );
			ASSERT( lpszSubkeyName != NULL );

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


			if (  NULL == lpszClassName  )
			{
				lpszClassName = "";
			}
			
			// The key handle (can) be equal to the registry handle.
			// If this is the case, closing the key also closes the
			// registry, so we should avoid this.
			if ( (m_hKey != (HKEY) NULL) && (m_hKey != m_hRegistry) ) {
				m_lErrorCode = ::RegCloseKey(m_hKey);
				ASSERT(  ERROR_SUCCESS == m_lErrorCode  );
			}

			m_lErrorCode = ::RegCreateKey( m_hRegistry,
							lpszSubkeyName,
							&m_hKey);
			
			if (  ERROR_SUCCESS == m_lErrorCode  )
			{
				m_strKeyName = lpszSubkeyName;		 
				return( TRUE );
			}
			return( FALSE );
		}

		BOOL Registry::DeleteKey( LPCTSTR lpszKeyToDelete)
		{
			ASSERT_VALID( this );
			ASSERT( lpszKeyToDelete != NULL );

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

			/*
			** 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;

			if ( strFullKeyName.Find( '\\' ) == (-1) )
			{
				/*
				** 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
			{
				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;

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

				/*
				** Now we open the parent key and delete the child
				*/

				if ( Open( strParent ) == TRUE )
				{
					m_lErrorCode = ::RegDeleteKey( m_hKey, strChild );
				}
				else
				{
					m_strKeyName = strOpenKeyName;
					return( FALSE );
				}
			}

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

		BOOL Registry::EnumerateKeys( const DWORD dwIndex, LPTSTR lpszBuffer, DWORD dwBufferSize)
		{
			ASSERT_VALID( this );

			m_lErrorCode = ::RegEnumKey( m_hKey, 
							dwIndex,
							lpszBuffer,
							dwBufferSize);	

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

		//@doc Registry
		//@mfunc Determines the number of subkeys in the curently open key.
		//@rdesc Nonzero if the key was enumerated, otherwise 0.
		//@parm  LPCTSTR | lpszBuffer | The key to be enumerated.
		//@parm  UINT | &nKeys | The number of subkeys result.
		//@devnote For 16-bit only.
		//@xref <c Registry> <mf Registry::EnumerateKeys>
		BOOL Registry::GetSubkeys( LPCTSTR lpszBuffer, UINT &nKeys)
		{
			// Determine number of subkeys  
			char temp_buf[1000]; 
			HKEY hKeyHolder = m_hKey;
			nKeys = 0;

			Open(l

⌨️ 快捷键说明

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