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

📄 bcgpregistry.cpp

📁 远程网络监视程序的源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	return FALSE;
}

BOOL CBCGPRegistry::Read (LPCTSTR pszKey, CString& sVal)
{
    ASSERT (m_hKey != NULL); 
    ASSERT (pszKey != NULL); 
	
    UINT  nBytes = 0; 
    DWORD dwType = 0; 
    DWORD dwCount = 0;
	
    LONG lResult = ::RegQueryValueEx (m_hKey, pszKey, NULL, &dwType, 
		NULL, &dwCount); 
	
    if (lResult == ERROR_SUCCESS && dwCount > 0)
    { 
		nBytes = dwCount; 
		ASSERT (dwType == REG_SZ || dwType == REG_EXPAND_SZ);

		
		BYTE* pData = new BYTE [nBytes + 1];
		
		lResult = ::RegQueryValueEx (m_hKey, pszKey, NULL, &dwType, 
			pData, &dwCount); 
		
		if (lResult == ERROR_SUCCESS &&  dwCount > 0) 
		{ 
			ASSERT (dwType == REG_SZ || dwType == REG_EXPAND_SZ);
			sVal = (TCHAR*)pData; 
		} 
		else
		{
			sVal.Empty ();
		}
		
		delete [] pData;
		pData = NULL; 
    } 
	else
	{
		sVal.Empty ();
	}
	
    m_Info.lMessage = lResult; 
    m_Info.dwType = dwType; 
    m_Info.dwSize = nBytes; 
	
    return lResult == ERROR_SUCCESS;
}

BOOL CBCGPRegistry::Read (LPCTSTR pszKey, CStringList& scStringList)
{
	scStringList.RemoveAll ();
	return Read (pszKey, (CObject&) scStringList);
}

BOOL CBCGPRegistry::Read (LPCTSTR pszKey, CByteArray& bcArray)
{
	bcArray.RemoveAll();
	return Read (pszKey, (CObject&) bcArray);
}

BOOL CBCGPRegistry::Read (LPCTSTR pszKey, CDWordArray& dwcArray)
{
	dwcArray.RemoveAll ();
	dwcArray.SetSize (0);
	return Read (pszKey, (CObject&) dwcArray);
}

BOOL CBCGPRegistry::Read (LPCTSTR pszKey, CWordArray& wcArray)
{
	wcArray.SetSize (0);

	BOOL	bSucess = FALSE;
	BYTE*	pData = NULL;
	UINT	uDataSize;

	if (!Read (pszKey, &pData, &uDataSize))
	{
		ASSERT (pData == NULL);
		return FALSE;
	}

	ASSERT (pData != NULL);

	try
	{
		CMemFile file (pData, uDataSize);
		CArchive ar (&file, CArchive::load);

		int iSize;
		ar >> iSize;

		wcArray.SetSize (iSize);
		for (int i = 0; i < iSize; i ++)
		{
			ar >> wcArray [i];
		}

		bSucess = TRUE;
	}
	catch (CMemoryException* pEx)
	{
		pEx->Delete ();
		TRACE(_T("Memory exception in CBCGPRegistry::Read ()!\n"));
	}
	catch (CArchiveException* pEx)
	{
		pEx->Delete ();
		TRACE(_T("CArchiveException exception in CBCGPRegistry::Read ()!\n"));
	}

	delete pData;
	return bSucess;
}

BOOL CBCGPRegistry::Read (LPCTSTR pszKey, CStringArray& scArray)
{
	scArray.RemoveAll ();
	return Read (pszKey, (CObject&) scArray);
}

BOOL CBCGPRegistry::Read(LPCTSTR pszKey, CRect& rect)
{
	BOOL	bSucess = FALSE;
	BYTE*	pData = NULL;
	UINT	uDataSize;

	if (!Read (pszKey, &pData, &uDataSize))
	{
		ASSERT (pData == NULL);
		return FALSE;
	}

	ASSERT (pData != NULL);

	try
	{
		CMemFile file (pData, uDataSize);
		CArchive ar (&file, CArchive::load);

		ar >> rect;
		bSucess = TRUE;
	}
	catch (CMemoryException* pEx)
	{
		pEx->Delete ();
		TRACE(_T("Memory exception in CBCGPRegistry::Read ()!\n"));
	}
	catch (CArchiveException* pEx)
	{
		pEx->Delete ();
		TRACE(_T("CArchiveException exception in CBCGPRegistry::Read ()!\n"));
	}

	delete pData;
	return bSucess;
}

BOOL CBCGPRegistry::Read(LPCTSTR pszKey, LPPOINT& lpPoint)
{
	ASSERT(m_hKey);
	const int iMaxChars = 20;
	CDWordArray dwcArray;
	DWORD dwType;
	DWORD dwData = iMaxChars;
	BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
	ASSERT(byData);

	LONG lReturn = RegQueryValueEx(m_hKey, pszKey, NULL, &dwType,
		byData, &dwData);

	if(lReturn == ERROR_SUCCESS && dwType == REG_BINARY)
	{
		ASSERT(dwData < iMaxChars);
		CMemFile file(byData, dwData);
		CArchive ar(&file, CArchive::load);
		ar.m_bForceFlat = FALSE;
		ASSERT(ar.IsLoading());
		ASSERT(dwcArray.IsSerializable());
		dwcArray.RemoveAll();
		dwcArray.SetSize(5);
		dwcArray.Serialize(ar);
		ar.Close();
		byData = file.Detach ();
		lpPoint->x = dwcArray.GetAt(0);
		lpPoint->y = dwcArray.GetAt(1);
	}

	m_Info.lMessage = lReturn;
	m_Info.dwType = REG_POINT;
	m_Info.dwSize = sizeof(POINT);

	if(byData)
	{
		free(byData);
		byData = NULL;
	}

	if(lReturn == ERROR_SUCCESS)
		return TRUE;

	return FALSE;
}

BOOL CBCGPRegistry::Read (LPCTSTR pszKey, BYTE** ppData, UINT* pBytes)
{
	ASSERT (m_hKey != NULL);
	ASSERT (pszKey != NULL);
	ASSERT(ppData != NULL);
	ASSERT(pBytes != NULL);
	*ppData = NULL;
	*pBytes = 0;

	DWORD dwType, dwCount;
	LONG lResult = ::RegQueryValueEx (m_hKey, pszKey, NULL, &dwType,
		NULL, &dwCount);

	if (lResult == ERROR_SUCCESS &&
		dwCount > 0) 
	{
		*pBytes = dwCount;
		ASSERT (dwType == REG_BINARY);

		*ppData = new BYTE [*pBytes];

		lResult = ::RegQueryValueEx (m_hKey, pszKey, NULL, &dwType,
			*ppData, &dwCount);

		if (lResult == ERROR_SUCCESS)
		{
			ASSERT (dwType == REG_BINARY);
		}
		else
		{
			delete [] *ppData;
			*ppData = NULL;
		}
	}

	m_Info.lMessage = lResult;
	m_Info.dwType = REG_BINARY;
	m_Info.dwSize = *pBytes;

	return (lResult == ERROR_SUCCESS);
}

BOOL CBCGPRegistry::Read (LPCTSTR pszKey, CObList& list)
{
	while (!list.IsEmpty ())
	{
		delete list.RemoveHead ();
	}

	return Read (pszKey, (CObject&) list);
}

BOOL CBCGPRegistry::Read (LPCTSTR pszKey, CObject& obj)
{
	BOOL	bSucess = FALSE;
	BYTE*	pData = NULL;
	UINT	uDataSize;

	if (!Read (pszKey, &pData, &uDataSize))
	{
		ASSERT (pData == NULL);
		return FALSE;
	}

	ASSERT (pData != NULL);

	try
	{
		CMemFile file (pData, uDataSize);
		CArchive ar (&file, CArchive::load);

		obj.Serialize (ar);
		bSucess = TRUE;
	}
	catch (CMemoryException* pEx)
	{
		pEx->Delete ();
		TRACE(_T("Memory exception in CBCGPRegistry::Read ()!\n"));
	}
	catch (CArchiveException* pEx)
	{
		pEx->Delete ();
		TRACE(_T("CArchiveException exception in CBCGPRegistry::Read ()!\n"));
	}

	delete pData;
	return bSucess;
}

BOOL CBCGPRegistry::Read (LPCTSTR pszKey, CObject*& pObj)
{
	BOOL	bSucess = FALSE;
	BYTE*	pData = NULL;
	UINT	uDataSize;

	if (!Read (pszKey, &pData, &uDataSize))
	{
		ASSERT (pData == NULL);
		return FALSE;
	}

	ASSERT (pData != NULL);

	try
	{
		CMemFile file (pData, uDataSize);
		CArchive ar (&file, CArchive::load);

		ar >> pObj;

		bSucess = TRUE;
	}
	catch (CMemoryException* pEx)
	{
		pEx->Delete ();
		TRACE(_T("Memory exception in CBCGPRegistry::Read ()!\n"));
	}
	catch (CArchiveException* pEx)
	{
		pEx->Delete ();
		TRACE(_T("CArchiveException exception in CBCGPRegistry::Read ()!\n"));
	}

	delete pData;
	return bSucess;
}

BOOL CBCGPRegistry::DeleteValue (LPCTSTR pszValue)
{
	ASSERT(m_hKey);
	LONG lReturn = RegDeleteValue(m_hKey, pszValue);


	m_Info.lMessage = lReturn;
	m_Info.dwType = 0L;
	m_Info.dwSize = 0L;

	if(lReturn == ERROR_SUCCESS)
		return TRUE;

	return FALSE;
}

BOOL CBCGPRegistry::DeleteKey (LPCTSTR pszPath, BOOL bAdmin)
{
	if (m_bReadOnly)
	{
		return FALSE;
	}
	
	ASSERT (pszPath != NULL);
	
	CString strPath = pszPath;

	int iPathLen = strPath.GetLength ();
	if (iPathLen > 0 && strPath [iPathLen - 1] == _T('\\'))
	{
		strPath = strPath.Left (iPathLen - 1);
	}
	
	// open the key
	HKEY hSubKey;
	LONG lReturn = ::RegOpenKeyEx (bAdmin ? HKEY_LOCAL_MACHINE :
	HKEY_CURRENT_USER,
		strPath, 0L, KEY_ALL_ACCESS, &hSubKey);
	
	if(lReturn != ERROR_SUCCESS)
		return FALSE;
	
	// first, delete all subkeys (else it won't work on NT!)
	for( DWORD dwSubKeys = 1; dwSubKeys > 0; )
	{
		dwSubKeys = 0;
		
		// first get an info about this subkey ...
		DWORD dwSubKeyLen;
		if( ::RegQueryInfoKey( hSubKey, 0,0,0, &dwSubKeys, &dwSubKeyLen,
			0,0,0,0,0,0) != ERROR_SUCCESS)
		{
			::RegCloseKey(hSubKey);
			return FALSE;
		}
		
		if( dwSubKeys > 0 )
		{
			// call DeleteKey() recursivly
			LPTSTR szSubKeyName = new TCHAR[dwSubKeyLen + 1];
			
			if( ::RegEnumKey( hSubKey, 0, szSubKeyName, dwSubKeyLen+1) !=
				ERROR_SUCCESS
				|| ! DeleteKey( strPath + "\\" + szSubKeyName, bAdmin ) )
			{
				delete szSubKeyName;
				::RegCloseKey(hSubKey);
				return FALSE;
			}
			
			delete szSubKeyName;
		}
	}
	::RegCloseKey(hSubKey);
	
	// finally delete the whole key
	lReturn = ::RegDeleteKey (bAdmin ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
		strPath);
	m_Info.lMessage = lReturn;
	m_Info.dwType = 0L;
	m_Info.dwSize = 0L;
	
	if(lReturn == ERROR_SUCCESS)
		return TRUE;
	
	return FALSE;
}

BOOL CBCGPRegistry::ReadSubKeys(CStringArray& SubKeys)
{
	BOOL result = TRUE;
	DWORD rc = ERROR_SUCCESS;
	TCHAR szSubKey[ 1024  ] = _T("\0");
	DWORD length = sizeof( szSubKey );

	ASSERT(m_hKey);

	int index = 0;
	rc = RegEnumKeyEx(m_hKey, index, szSubKey, &length, NULL, NULL, NULL, NULL);

	if( rc == ERROR_NO_MORE_ITEMS) {
		result = false;
	}
	else while(rc == ERROR_SUCCESS) {
		SubKeys.Add( szSubKey );		
		length = sizeof( szSubKey );
		index++;
		rc = RegEnumKeyEx(m_hKey, index, szSubKey, &length, NULL, NULL, NULL, NULL);		
		
	} // while
	return( result );
}


BOOL CBCGPRegistry::ReadKeyValues(CStringArray &Values)
{
	DWORD rc = ERROR_SUCCESS;
	BOOL result = FALSE;
	TCHAR szValue[ 1024 ];
	DWORD length = sizeof( szValue );
	int index = 0;

	ASSERT(m_hKey);

	rc = RegEnumValue(m_hKey, index, szValue, &length, NULL, NULL, NULL, NULL);
	if( rc == ERROR_NO_MORE_ITEMS) {
	  result = FALSE;
	}
	else while( rc == ERROR_SUCCESS )  {
		result = TRUE;
		Values.Add( szValue );
		length = sizeof( szValue );
		index++;
		rc = RegEnumValue(m_hKey, index, szValue, &length, NULL, NULL, NULL, NULL);		
	}
	return( result );

}

//////////////////////////////////////////////////////////////////////////////
// CBCGRegistrySP - Helper class that manages "safe" CBCGRegistry pointer

CRuntimeClass* CBCGPRegistrySP::m_pRTIDefault = NULL;

BOOL CBCGPRegistrySP::SetRuntimeClass (CRuntimeClass* pRTI)
{
	if (pRTI != NULL &&
		!pRTI->IsDerivedFrom (RUNTIME_CLASS (CBCGPRegistry)))
	{
		ASSERT (FALSE);
		return FALSE;
	}

	m_pRTIDefault = pRTI;
	return TRUE;
}

CBCGPRegistry& CBCGPRegistrySP::Create (BOOL bAdmin, BOOL bReadOnly)
{
	if (m_pRegistry != NULL)
	{
		ASSERT (FALSE);
		ASSERT_VALID (m_pRegistry);

		return *m_pRegistry;
	}

	if (m_pRTIDefault == NULL)
	{
		m_pRegistry = new CBCGPRegistry;
	}
	else
	{
		ASSERT (m_pRTIDefault->IsDerivedFrom (RUNTIME_CLASS (CBCGPRegistry)));
		m_pRegistry = DYNAMIC_DOWNCAST (CBCGPRegistry, 
										m_pRTIDefault->CreateObject ());
	}

	ASSERT_VALID (m_pRegistry);

	m_pRegistry->m_bReadOnly = bReadOnly;
	m_pRegistry->m_hKey = bAdmin ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
	m_pRegistry->m_bAdmin = bAdmin;
	m_pRegistry->m_dwUserData = m_dwUserData;

	return *m_pRegistry;
}

⌨️ 快捷键说明

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