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

📄 registerwin32.cpp

📁 这个文件中如何在EVC 中c++对注册表的操作。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name:   Write
//
/*!Access:        Public

   Parameters

   IN             Description

   <LPCTSTR>      The Key

   <LPCTSTR>      The string value to write

   OUT            Description

   <BOOL>         TRUE if succeeded.

   Notes:         Must at least have 1 char to write.
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::Write(LPCTSTR _szKey, LPCTSTR _szValue)
{
	BOOL bResult = FALSE;

	DWORD dLen = strlen(_szValue);
	if ( dLen > 0)
	{
		dLen++;
		bResult = ( RegSetValueEx(m_hKey, _szKey,0,
						REG_SZ, (CONST BYTE*)_szValue,
						dLen ) == ERROR_SUCCESS );
	}
	return bResult;
}


//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name:   WriteInt
//
/*!Access:        Public

   Parameters

   IN             Description

   <LPCTSTR>      The Key Name

   <int>          The int val to write

   OUT            Description

   <BOOL>         TRUE if succeeded.

   Notes:
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::WriteInt(LPCTSTR _szKey, int _iVal)
{
	return SetValue(_szKey, (DWORD)_iVal);
}


//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name:   WriteDword
//
/*!Access:        Public

   Parameters

   IN             Description

   <LPCTSTR>      The Key Name

   <DWORD>        The value.

   OUT            Description

   <BOOL>         TRUE if Succeeded.

   Notes
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::WriteDword(LPCTSTR _szKey, DWORD _dwVal)
{
	return SetValue(_szKey, _dwVal);
}


//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name:   WriteArray
//
/*!Access:        Public

   Parameters

   IN             Description

   <LPCTSTR>      The Key Name

   <LPBYTE>       The BYTE array

   <DWORD>        The Array size

   OUT            Description

   <BOOL>         TRUE if Succeeded.

   Notes:         Write a Binary value array
         Example: BYTE MonByte[10]={1,2,3,4,5,6,7,8,9,10}; 
                  MyReg.Write("BinVal", MonByte, 10);
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::WriteArray(LPCTSTR _szKey, LPBYTE _pValue, DWORD _nLen)
{
	return ( RegSetValueEx(m_hKey, _szKey, 0, REG_BINARY, _pValue, _nLen) == ERROR_SUCCESS );
}



//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name:   Read
//
/*!Access:        Public

   Parameters

   IN             Description

   <LPCTSTR>      The Key Name.

   <LPCTSTR>      Default value if not founded.

   OUT            Description

   <LPCTSTR>      The readed result.

   Notes
*/
//--------------------------------------------------------------------------
LPCTSTR CRegisterWIN32::Read(LPCTSTR _szKey, LPCTSTR _szDefault)
{

   m_szTemp.erase();
   m_szTemp = _szDefault;
   
   if ( StringIN(_szKey) )
      m_szTemp = m_pszTemp;

   return m_szTemp.c_str();
}



//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name:   ReadInt
//
/*!Access:        Public

   Parameters

   IN             Description

   <LPCTSTR>      The Key Name

   <int>          Default value

   OUT            Description

   <int>          The int result.

   Notes
*/
//--------------------------------------------------------------------------
int CRegisterWIN32::ReadInt(LPCTSTR _szKey, int _iDefaultVal)
{

	if ( GetValue(_szKey) )
		return (int)m_dTemp;
   else
      return _iDefaultVal;
}


//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name:   ReadDword
//
/*!Access:        Public

   Parameters

   IN             Description

   <LPCTSTR>      The Key Name

   <DWORD>        Default Value

   OUT            Description

   <DWORD>        The result value.

   Notes
*/
//--------------------------------------------------------------------------
DWORD CRegisterWIN32::ReadDword(LPCTSTR _szKey, DWORD _dwDefaultVal)
{
   if ( GetValue(_szKey) )
	   return m_dTemp;
   else
      return _dwDefaultVal;
}


//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name:   ReadArray
//
/*!Access:        Public

   Parameters

   IN             Description

   <LPCTSTR>      The Key Name.

   <LPBYTE>       The Byte Array

   OUT            Description

   <DWORD>        The Number of BYTE readed         

   Notes:         Example: BYTE MyBytes[25];
					   MyReg.ReadArray("ArrayKey", MyBytes);
*/
//--------------------------------------------------------------------------
DWORD CRegisterWIN32::ReadArray(LPCTSTR _szKey, LPBYTE _pValue)
{
	if ( VerifyKey() )
	{
		if( RegQueryValueEx(m_hKey, _szKey, NULL, NULL, _pValue, &m_dTemp) == ERROR_SUCCESS )
         return m_dTemp;
      else
         return 0;
	}
   return 0;
}


//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name:   DeleteValue
//
/*!Access:        Public

   Parameters

   IN             Description

   <LPCTSTR>      The Key Name

   OUT            Description

   <BOOL>         TRUE if Succeeded.

   Notes:         Delete a Key Value 
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::DeleteValue(LPCTSTR _szKey)
{
	BOOL bResult = FALSE;
	//** The Key Value to Delete
   return ( RegDeleteValue(m_hKey, _szKey) == ERROR_SUCCESS );
}


//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name:   DeleteKey
//
/*!Access:        Public

   Parameters

   IN             Description

   <HKEY>         The KeyRoot

   <LPCTSTR>      Sub Key Name

   OUT            Description

   <BOOL>         TRUE if Succeeded.

   Notes:         With WinNT we need a special Function 
                  You don't need to use Open for this Function
         Example: MyReg.DeleteKey(HKEY_CURRENT_USER, _T("Software\\SevySoft"));
                  The Function will extract the Path = Software
                  Then delete the Key SevySoft with all SubDir and Value Inside
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::DeleteKey(HKEY _hKeyRoot, LPCTSTR _szPath)
{
	BOOL bResult  = FALSE;

	LPTSTR pszSubKey = new char[_MAX_PATH];
	LPTSTR pszPath = new char[_MAX_PATH];
	LPTSTR pDest= NULL;
	LPTSTR pTemp = NULL;
	//** If Path empty we get out
	if( strlen(_szPath) > 0)
   {
      pDest= strrchr(_szPath, '\\');
		if (pDest)
		{
			//** Must extract the Last Key
			pTemp = pDest;
			pDest++;
			//** Copy the Key or SubKey to delete
			strcpy(pszSubKey, pDest);
			//** We must now extract the Path
			pDest = pTemp;
			int iCount=0;
			int Result=0;
			Result = pDest - _szPath ;
			do
			{
				pszPath[iCount] = _szPath[iCount];
				iCount++;
			}while ( iCount < Result );
			//** Add end of string mark
			pszPath[iCount]= '\0';
		}
		else
		{
			//** We want to delete a path
			strcpy(pszSubKey, _szPath);
			//** There is no path
			strcpy(pszPath, "");
		}

		HKEY hKey;
		//** Check if Path exist ...
		if ( RegOpenKeyEx(_hKeyRoot, pszPath, 0L, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS )
      {
         //** Check NT or 2000
		   if ( GetOSversion() == 1 )
		   {
			   //** NT system must use Recursive Delete
			   if( DeleteNTway(hKey, pszSubKey) )
				bResult = TRUE;
		   }
		   else
		   {
			   //** Windows 95 or 98
			if ( RegDeleteKey(hKey , pszSubKey) == ERROR_SUCCESS )
				bResult= TRUE;
		   }
         RegCloseKey(hKey);
       }
   }

   if(pszSubKey)  
      delete pszSubKey;
   if (pszPath)
      delete pszPath;
	return bResult;
}


//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name:   WriteEncrypt
//
/*!Access:        Public

   Parameters

   IN             Description

   <LPCTSTR>      The Key Name

   <LPCTSTR>      The String to Write

   <LPCTSTR>      The Password Key

   OUT            Description

   <BOOL>         TRUE if Succeeded.

   Notes:         We use the Rijndael encryption
                  If no PassKey submited then take a GUID PassKey
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::WriteEncrypt(LPCTSTR _szKey, LPCTSTR _szString, 
                                  LPCTSTR _szPassKey)
{
	BOOL  bResult = FALSE;
   char  szDataOut[16]= {NULL};
   DWORD dLen;

	char* pszPassWord= new char[MAX_SIZE];
	if ( strlen(_szKey) > 0)
	{
		if( _szPassKey == NULL || strlen(_szPassKey) == 0 )
			//** No Password so use default
			strcpy(pszPassWord, "{6A67930C-7679-4e91-8580-3AEC80AFF8E0}");
		else
			strcpy(pszPassWord, _szPassKey);

		if( m_pMyCaes->MakeKey(pszPassWord) )
		{
			//** So far the Key is generated
			if( m_pMyCaes->EncryptBlock(_szString, szDataOut) )
			{
				//** Encrypt Block always 16
				dLen =16;
				//** We are now ready to Write
				bResult = ( RegSetValueEx(m_hKey, _szKey,0,
						   REG_BINARY, (CONST BYTE*)szDataOut,
						   dLen ) == ERROR_SUCCESS );
			}
		}
	}
	delete []pszPassWord;
	return bResult;
}


//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name:   ReadEncrypt
//
/*!Access:        Public

   Parameters

   IN             Description

   <LPCTSTR>      The Key Name.

   <LPCTSTR>      The PassWord.

   OUT            Description

   <LPCTSTR>      The result String.

   Notes:
*/
//--------------------------------------------------------------------------
LPCTSTR CRegisterWIN32::ReadEncrypt(LPCTSTR _szKey, LPCTSTR _szPassKey)
{
	char* pszPassWord= new char[MAX_SIZE];
   DWORD dwLen;

	//if ( StringIN(_szKey) )
   if(RegQueryValueEx(m_hKey, _szKey, NULL, NULL,(BYTE*)m_pszTemp,&dwLen)== ERROR_SUCCESS )
	{
      m_szTemp = m_pszTemp;
		//** Check the PassKey
		if( _szPassKey == NULL || strlen(_szPassKey) == 0 )
			//** No Password so use default
			strcpy(pszPassWord, "{6A67930C-7679-4e91-8580-3AEC80AFF8E0}");
		else
			strcpy(pszPassWord, _szPassKey);

		if( m_pMyCaes->MakeKey(pszPassWord) )
		{
			//** Now Decrypt the String
			if( m_pMyCaes->DecryptBlock(m_szTemp.c_str(), m_pszTemp) )
            m_szTemp =  m_pszTemp;
         else
            m_szTemp.erase();
		}
	}
	delete [] pszPassWord;
   return m_szTemp.c_str();
}

⌨️ 快捷键说明

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