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

📄 registryserialize.cpp

📁 visual c++ 实例编程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		bDeleteOnUnInstall);
}

int CRegistrySerialize::SearchPath( LPCTSTR lpszRegistryPath)
{
	int nCount = keys.GetSize();
	for ( int i=0 ; i< nCount ; i++) {
		CSerializedKeyPath *pPath = keys.GetAt(i);
		if (pPath) {
			CString str = pPath->GetPath();
			if (!str.CompareNoCase(lpszRegistryPath))
				return i;
		}
	}
	return -1;
}

BOOL CRegistrySerialize::Install( BOOL bInstall)
{
	int nCount = keys.GetSize();
	BOOL bResult = TRUE;
	for (int i=0 ; i< nCount ;i++) {
		CSerializedKeyPath *pPath = keys.GetAt(i);
		if ((pPath) &&	(!pPath->Install(bInstall)))
			bResult = FALSE;//some problem occured
	}
	return bResult;
}

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////// class CSerializedKeyPath
CSerializedKeyPath::CSerializedKeyPath()
{
	values.SetSize(0,1);
	bDeleteOnUnInstall = TRUE;
	pReg = NULL;
}
CSerializedKeyPath::CSerializedKeyPath( CRegistry *p)
{
	values.SetSize(0,1);
	bDeleteOnUnInstall = TRUE;
	pReg = p;
}
CSerializedKeyPath::~CSerializedKeyPath()
{
	for (int nCount = values.GetSize() , i = nCount-1 ; i>=0 ; i--) {
		delete values.GetAt(i);
	}
	values.RemoveAll();
}

#ifdef _DEBUG //look at CRegistrySerialize::Info
void CSerializedKeyPath::Info()
{
	CString strIDs[] = { _T("REGEMPTY"),  _T("REGSTRING"),
		_T("REGDWORD"),  _T("REGINT"), _T("REGPBYTE"),
		_T("REGWORD"), _T("REGFLOAT"), _T("REGPOINT"),
		_T("REGSIZE") , _T("REGTIME") ,_T("REGRECT"),_T("REGBYTE") };

	int nCount = values.GetSize();
	CString strMessage = strPath + _T("\n");

	for (int i=0 ; i<nCount ; i++) {
		CSerializedValue *pValue = values.GetAt(i);
		if (pValue) {
			CString str;
			str.Format("\"%s\"  del=%u id=%s",pValue->GetValueName(),
				pValue->GetDelete(),strIDs[pValue->GetType()]);
			strMessage += str;
			strMessage += _T("\n");
		}
	}
	pReg->msg( strMessage , _T("Path ..."));
}

#endif

void CSerializedKeyPath::Serialize(CArchive &ar)
{
	int nCount;
	if (ar.IsLoading()) { //loading
		ar>>strPath;
		ar>>nCount;
		for (int i=0 ; i<nCount ; i++) {
			CSerializedValue  *pValue = new CSerializedValue( this , pReg);
			if (pValue) {
				pValue->Serialize(ar);
				if (!InsertCSerializedValue(pValue))
					delete pValue;
			}
		} // end for
	}
	else { //storing
		ar<<strPath;
		nCount = values.GetSize();
		ar<<nCount;
		for (int i=0 ; i<nCount ; i++) {
			CSerializedValue  *pValue = values.GetAt(i);
			if (pValue)	pValue->Serialize(ar);
		}		
	}
}

BOOL CSerializedKeyPath::InsertCSerializedValue(CSerializedValue *pNewValue)
{
	CString value = pNewValue->GetValueName();
	int nIndex = Search4Value(value);
	if (nIndex == -1) {
		values.Add(pNewValue);
		return TRUE;
	}
	return FALSE;
}

IMPLEMENT_SERIAL(CSerializedKeyPath , CObject , 1)

int CSerializedKeyPath::Search4Value( LPCTSTR lpszValueName)
{
	int nCount = values.GetSize();
	for (int i=0 ; i<nCount ; i++) {
		CSerializedValue *pValue = values.GetAt(i);
		if (pValue) {
			CString str = pValue->GetValueName();
			if (!str.CompareNoCase( lpszValueName))
				return i;
		}
	}
	return -1;
}

BOOL CSerializedKeyPath::InsertValue( LPCTSTR lpszValueName ,
		 REGDATA iDataType , BOOL bDeleteOnUnInstall)
{
	CSerializedValue *pValue = new CSerializedValue( this , pReg);
	if (!pValue)   return FALSE;
	pValue->SetInfo( lpszValueName, iDataType, bDeleteOnUnInstall);
	values.Add(pValue);

	return TRUE;
}

// TRUE = Install  FALSE = UnInstall
BOOL CSerializedKeyPath::Install( BOOL bInstall)
{
	if (strPath.IsEmpty())
		return FALSE;

	if (bInstall) {
		if (!pReg->CreateKey(strPath))
			return FALSE;
		//creating the values
		int nCount = values.GetSize();
		for (int i=0 ; i<nCount ; i++ ) {
			CSerializedValue *pValue = values.GetAt(i);
			if (pValue)
				pValue->Install();
		}
		return TRUE;
	}

	// here we remove

	//if we're here delete value
	if (bDeleteOnUnInstall) 
		return pReg->DeleteKey(strPath);
	else { //uninstalling only values
		//deleting the values : but not the key
		int nCount = values.GetSize();
		for (int i=0 ; i<nCount ; i++ ) {
			CSerializedValue *pValue = values.GetAt(i);
			if (pValue)
				pValue->UnInstall();
		}
	}

	return TRUE;
}

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////
/////// class CSerializedValue
CSerializedValue::CSerializedValue ()
{
	pReg = NULL;
	pathOwner = NULL ;
	iDataType = REGEMPTY;
	bDeleteOnUnInstall = TRUE;
}
CSerializedValue::CSerializedValue ( CSerializedKeyPath *pOwner , CRegistry *pRegistry )
{
	pReg = pRegistry;
	pathOwner = pOwner ;
	iDataType = REGEMPTY;
	bDeleteOnUnInstall = TRUE;
}

CSerializedValue::~CSerializedValue()
{
}

IMPLEMENT_SERIAL(CSerializedValue , CObject , 1)
void CSerializedValue::Serialize(CArchive &ar)
{
	LONG iType;
	if (ar.IsLoading()) { //Loading
		ar>>iType;	iDataType = (REGDATA)iType;
		ar>>strValueName;
		ar>>bDeleteOnUnInstall;
	}
	else { // Saving
		ar<<(LONG)iDataType;
		ar<<strValueName;
		ar<<bDeleteOnUnInstall;
	}
}

BOOL CSerializedValue::SetInfo( LPCTSTR lpszValueName,
				REGDATA iType , BOOL bDeleteOnUnInstall )
{
	ASSERT(pathOwner!=NULL);
	SetValueName(lpszValueName);
	SetType( iType);
	SetDelete( bDeleteOnUnInstall);
	return TRUE;
}
///////////////////////////////////////////////////////////////////
/////// Persist functions
BOOL CSerializedValue::Persist( CString *pstrDest , BOOL bLoading ) {
	if (iDataType == REGSTRING) {
		CString path = pathOwner->GetPath();
		return (bLoading) ? pReg->GetValue( path , strValueName , pstrDest)
					: pReg->SetValue( path , strValueName , (LPCTSTR) *pstrDest);
	}
	return FALSE;
}
BOOL CSerializedValue::Persist( DWORD *dwDest , BOOL bLoading ) {
	if (iDataType == REGDWORD) {
		CString path = pathOwner->GetPath();
		return (bLoading) ? pReg->GetValue( path, strValueName, dwDest)
					: pReg->SetValue( path, strValueName, dwDest);
	}
	return FALSE;
}
BOOL CSerializedValue::Persist( int *iDest , BOOL bLoading ) {
	if (iDataType == REGINT) {
		CString path = pathOwner->GetPath();
		return (bLoading) ? pReg->GetValue( path, strValueName, iDest)
					: pReg->SetValue( path, strValueName, iDest);
	}
	return FALSE;
}
BOOL CSerializedValue::Persist( float *pfDest, BOOL bLoading) {
	if (iDataType == REGFLOAT) {
		CString path = pathOwner->GetPath();
		return (bLoading) ? pReg->GetValue( path, strValueName, pfDest)
					: pReg->SetValue( path, strValueName, pfDest);
	}
	return FALSE;
}
BOOL CSerializedValue::Persist( CRect *pRect, BOOL bLoading) {
	if (iDataType == REGRECT) {
		CString path = pathOwner->GetPath();
		return (bLoading) ? pReg->GetValue( path, strValueName, pRect)
					: pReg->SetValue( path, strValueName, pRect);
	}
	return FALSE;
}
BOOL CSerializedValue::Persist( CPoint *pPoint, BOOL bLoading) {
	if (iDataType == REGPOINT) {
		CString path = pathOwner->GetPath();
		return (bLoading) ? pReg->GetValue( path, strValueName, pPoint)
					: pReg->SetValue( path, strValueName, pPoint);
	}
	return FALSE;
}
BOOL CSerializedValue::Persist( CSize *pSize, BOOL bLoading) {
	if (iDataType == REGSIZE) {
		CString path = pathOwner->GetPath();
		return (bLoading) ? pReg->GetValue( path, strValueName, pSize)
					: pReg->SetValue( path, strValueName, pSize);
	}
	return FALSE;
}
BOOL CSerializedValue::Persist( CTime *ptDest, BOOL bLoading) {
	if (iDataType == REGTIME) {
		CString path = pathOwner->GetPath();
		return (bLoading) ? pReg->GetValue( path, strValueName, ptDest)
					: pReg->SetValue( path, strValueName, ptDest);
	}
	return FALSE;
}

BOOL CSerializedValue::Persist( LPBYTE *lpByte, DWORD cbBuffLen, BOOL bLoading) {
	if (iDataType == REGPBYTE) {
		CString path = pathOwner->GetPath();
		return (bLoading) ? pReg->GetValue( path, strValueName, *lpByte, cbBuffLen)
					: pReg->SetValue( path, strValueName, *lpByte, cbBuffLen);
	}
	return FALSE;
}	

//////////////////////////////////////////////////////////////
/////// Install + UnInstall functions
BOOL CSerializedValue::Install()
{
	CString path = pathOwner->GetPath();
	
	BYTE data[10]; //enough for all data types
	memset(&data ,0 ,sizeof(data));
	CString strData = _T(""); //you may change to an explicit default string
	size_t  size = 0;

	switch (iDataType)
	{
	case REGEMPTY: return FALSE; //Error no type

	case REGSTRING: return pReg->CreateValue( path, strValueName , (LPCTSTR)strData);
	case REGINT:   size = sizeof(int);
	case REGDWORD: size = sizeof(DWORD);
	case REGPBYTE: size = 0;
	case REGFLOAT:  size = sizeof(float);
	case REGPOINT:  size = sizeof(POINT);
	case REGSIZE:  size = sizeof(SIZE);
	case REGTIME:  size = sizeof(time_t);
	case REGRECT:  size = sizeof(RECT);
	case REGBYTE:  size = sizeof(BYTE);
	}
	return pReg->CreateValue( path, strValueName, (const LPBYTE)&data, size );
}

BOOL CSerializedValue::UnInstall() 
{
	if (bDeleteOnUnInstall) {
		CString path = pathOwner->GetPath();
		pReg->GetPathObj().Trim(path);
		pReg->DeleteValue( path, strValueName);
	} 
	return TRUE;
}








⌨️ 快捷键说明

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