Professional UI Solutions
Site Map   /  Register
 
 

Forum

Please Log In to post a new message or reply to an existing one. If you are not registered, please register.

NOTE: Some forums may be read-only if you are not currently subscribed to our technical support services.

Forums » Prof-UIS General Discussion » Saving parameters Collapse All
Subject Author Date
Adelin Nov 25, 2002 - 10:37 AM

Hi,

I have a problem with saving parameters in the registry or in a file.
No problems with windows positioning and toolbars.

Can you gave an example how to do it.

Regards
Adelin

Sergiy Lavrynenko Nov 26, 2002 - 1:36 AM

Dear Adelin,

Please let me know what exactly you want to load/save with
the registry or file. I will code a simple application for you
which will do requested operations.


If you need to serialize any CObject class instance
(for example CDocument) - ready to use functions are here:

1) LoadAnyCObjectFromFile() - loads any CObject from file with
the file dialog prompt and exception handling

2) SaveAnyCObjectToFile() - saves any CObject to file with
the file dialog prompt and exception handling

3) AnyCObjectRegistrySerialization() - loads or saves
any CObject from/to registry


Please notice: pMyObject parameter in these functions should
be pointer to really valid CObject.


Reqistry location parameters of AnyCObjectRegistrySerialization()
function:

1) LPCTSTR sProfileName - registry folder for your
CObject data
(under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct%)

2) LPCTSTR sSectionNameCompany - company name,
usually obtained from (::AfxGetApp()->m_pszRegistryKey)
(under HKEY_CURRENT_USER \ Software)

3) LPCTSTR sSectionNameProduct - program name,
usually obtained from (::AfxGetApp()->m_pszProfileName)
(under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany%)


Your CObject will be stored in registry subtree under
HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct% \ %sProfileName%

You can pass complex path as sProfileName parameter like this:
_T("My folder 1\\My subsystem 2\\One of my documents")


Best regards,
Sergiy Lavrynenko





//===================================================
//===================================================

static LPCTSTR g_lpszDefExtForFileDialogs =
_T("*.data")
;

static LPCTSTR g_lpszFilterForFileDialogs =
_T("My CObject data files (*.data)|*.data")
_T("|All files (*.*)|*.*")
_T("||")
;

//
// function to load any CObject from file
// with exception handling
//
void LoadAnyCObjectFromFile( CObject * pMyObject )
{
ASSERT_VALID( pMyObject );
CFileDialog dlg(
TRUE,
g_lpszDefExtForFileDialogs,
NULL,
OFN_ENABLESIZING|OFN_EXPLORER|OFN_PATHMUSTEXIST
|OFN_HIDEREADONLY
|OFN_FILEMUSTEXIST
|OFN_LONGNAMES
,
g_lpszFilterForFileDialogs,
this
);
dlg.m_ofn.lpstrTitle = _T("Load any CObject from file ...");
if( dlg.DoModal() != IDOK )
return;
CString sFullPath = dlg.GetPathName();
try
{
CFile file(
sFullPath,
CFile::modeRead|CFile::typeBinary
);
CArchive ar( &file, CArchive::load );
pMyObject->Serialize( ar );
} // try
catch( CException * pException )
{
CString sErrorDescription;
if( !pException->GetErrorMessage(
sErrorDescription.GetBuffer(4096),
4096
)
)
{
sErrorDescription.ReleaseBuffer();
sErrorDescription = _T("no error description");
}
else
sErrorDescription.ReleaseBuffer();
pException->Delete();
CString sMsg;
sMsg.Format(
_T("Failed to load CObject data:\n\n%s\n"),
(LPCTSTR)sErrorDescription
);
MessageBox(
sMsg,
_T("Error"),
MB_OK|MB_ICONERROR
);
} // catch( CException * pException )
catch( ... )
{
MessageBox(
_T("Failed to load CObject data (unknown error)"),
_T("Error"),
MB_OK|MB_ICONERROR
);
} // catch( ... )
}

//
// function to save any CObject to file
// with exception handling
//
void SaveAnyCObjectToFile( CObject * pMyObject )
{
ASSERT_VALID( pMyObject );
CFileDialog dlg(
FALSE,
g_lpszDefExtForFileDialogs,
g_lpszDefSaveFileName,
OFN_ENABLESIZING|OFN_EXPLORER|OFN_PATHMUSTEXIST
|OFN_HIDEREADONLY
|OFN_OVERWRITEPROMPT
|OFN_LONGNAMES
,
g_lpszFilterForFileDialogs,
this
);
dlg.m_ofn.lpstrTitle = _T("Save any CObject to file ...");
if( dlg.DoModal() != IDOK )
return;
CString sFullPath = dlg.GetPathName();
try
{
CFile file(
sFullPath,
CFile::modeWrite|CFile::typeBinary|CFile::modeCreate
);
CArchive ar( &file, CArchive::store );
pMyObject->Serialize( ar );
} // try
catch( CException * pException )
{
CString sErrorDescription;
if( !pException->GetErrorMessage(
sErrorDescription.GetBuffer(4096),
4096
)
)
{
sErrorDescription.ReleaseBuffer();
sErrorDescription = _T("no error description");
}
else
sErrorDescription.ReleaseBuffer();
pException->Delete();
CString sMsg;
sMsg.Format(
_T("Failed to save CObject data:\n\n%s\n"),
(LPCTSTR)sErrorDescription
);
MessageBox(
sMsg,
_T("Error"),
MB_OK|MB_ICONERROR
);
} // catch( CException * pException )
catch( ... )
{
MessageBox(
_T("Failed to save CObject data (unknown error)"),
_T("Error"),
MB_OK|MB_ICONERROR
);
} // catch( ... )
}

//===================================================
//===================================================

// helper function
static CString productsection2regkeypath(
LPCTSTR sProfileName, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct%
LPCTSTR sSectionNameCompany, // under HKEY_CURRENT_USER \ Software
LPCTSTR sSectionNameProduct // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany%
)
{
ASSERT( sProfileName != NULL );
ASSERT( sSectionNameCompany != NULL );
ASSERT( sSectionNameProduct != NULL );
CString s;
s.Format(
_T("Software\\%s\\%s\\%s"),
sSectionNameCompany,
sSectionNameProduct,
sProfileName
);
s.Replace(’\r’,’_’);
s.Replace(’\t’,’_’);
s.Replace(’\n’,’_’);
s.Replace(’?’,’_’);
s.Replace(’*’,’_’);
s.Replace(’.’,’_’);
s.Replace(’,’,’_’);
return s;
}

// helper function
static bool fileobj_to_registry(
CFile & _file,
LPCTSTR sProfileName, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct%
LPCTSTR sSectionNameCompany, // under HKEY_CURRENT_USER \ Software
LPCTSTR sSectionNameProduct // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany%
)
{
ASSERT( sProfileName != NULL );
ASSERT( sSectionNameCompany != NULL );
ASSERT( sSectionNameProduct != NULL );
CString sRegKeyPath =
productsection2regkeypath(
sProfileName,
sSectionNameCompany,
sSectionNameProduct
);

return CExtCmdManager::FileObjToRegistry( _file, sRegKeyPath );
}

// helper function
static bool fileobj_from_registry(
CFile & _file,
LPCTSTR sProfileName, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct%
LPCTSTR sSectionNameCompany, // under HKEY_CURRENT_USER \ Software
LPCTSTR sSectionNameProduct // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany%
)
{
ASSERT( sProfileName != NULL );
ASSERT( sSectionNameCompany != NULL );
ASSERT( sSectionNameProduct != NULL );
ASSERT( _file.GetLength() == 0 );
CString sRegKeyPath =
productsection2regkeypath(
sProfileName,
sSectionNameCompany,
sSectionNameProduct
);
return CExtCmdManager::FileObjFromRegistry( _file, sRegKeyPath );
}

//
// function to load/save any CObject from/to registry folder
// with exception handling
//
//
bool AnyCObjectRegistrySerialization(
CObject * pMyObject,
LPCTSTR sProfileName, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct%
LPCTSTR sSectionNameCompany, // under HKEY_CURRENT_USER \ Software
LPCTSTR sSectionNameProduct, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany%
bool bSave // true - save, false - load
)
{
ASSERT_VALID( pMyObject );
ASSERT( sProfileName != NULL );
ASSERT( sSectionNameCompany != NULL );
ASSERT( sSectionNameProduct != NULL );
bool bRetVal = false;
try
{
CMemFile _file;
if( bSave )
{
{ // BLOCK: CArchive usage
CArchive ar(
&_file,
CArchive::store
);
pMyObject->Serialize( ar );
ar.Flush();
} // BLOCK: CArchive usage

// ... write _file to registty
_file.Seek(0,CFile::begin);
if( !fileobj_to_registry(
_file,
sProfileName,
sSectionNameCompany,
sSectionNameProduct
)
)
return false;

} // if( bSave )
else
{
// ... read _file from registty
if( !fileobj_from_registry(
_file,
sProfileName,
sSectionNameCompany,
sSectionNameProduct
)
)
return false;
_file.Seek(0,CFile::begin);

CArchive ar(
&_file,
CArchive::load
);
pMyObject->Serialize( ar );
} // else from if( bSave )

bRetVal = true;
} // try
catch( CException * pXept )
{
pXept->Delete();
ASSERT( FALSE );
} // catch( CException * pXept )
catch( ... )
{
ASSERT( FALSE );
} // catch( ... )
return bRetVal;
}

//===================================================
//===================================================


Dear Adelin,

Please let me know what exactly you want to load/save with
the registry or file. I will code a simple application for you
which will do requested operations.


If you need to serialize any CObject class instance
(for example CDocument) - ready to use functions are here:

1) LoadAnyCObjectFromFile() - loads any CObject from file with
the file dialog prompt and exception handling

2) SaveAnyCObjectToFile() - saves any CObject to file with
the file dialog prompt and exception handling

3) AnyCObjectRegistrySerialization() - loads or saves
any CObject from/to registry


Please notice: pMyObject parameter in these functions should
be pointer to really valid CObject.


Reqistry location parameters of AnyCObjectRegistrySerialization()
function:

1) LPCTSTR sProfileName - registry folder for your
CObject data
(under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct%)

2) LPCTSTR sSectionNameCompany - company name,
usually obtained from (::AfxGetApp()->m_pszRegistryKey)
(under HKEY_CURRENT_USER \ Software)

3) LPCTSTR sSectionNameProduct - program name,
usually obtained from (::AfxGetApp()->m_pszProfileName)
(under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany%)


Your CObject will be stored in registry subtree under
HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct% \ %sProfileName%

You can pass complex path as sProfileName parameter like this:
_T("My folder 1\\My subsystem 2\\One of my documents")


Best regards,
Sergiy Lavrynenko





//===================================================
//===================================================

static LPCTSTR g_lpszDefExtForFileDialogs =
_T("*.data")
;

static LPCTSTR g_lpszFilterForFileDialogs =
_T("My CObject data files (*.data)|*.data")
_T("|All files (*.*)|*.*")
_T("||")
;

//
// function to load any CObject from file
// with exception handling
//
void LoadAnyCObjectFromFile( CObject * pMyObject )
{
ASSERT_VALID( pMyObject );
CFileDialog dlg(
TRUE,
g_lpszDefExtForFileDialogs,
NULL,
OFN_ENABLESIZING|OFN_EXPLORER|OFN_PATHMUSTEXIST
|OFN_HIDEREADONLY
|OFN_FILEMUSTEXIST
|OFN_LONGNAMES
,
g_lpszFilterForFileDialogs,
this
);
dlg.m_ofn.lpstrTitle = _T("Load any CObject from file ...");
if( dlg.DoModal() != IDOK )
return;
CString sFullPath = dlg.GetPathName();
try
{
CFile file(
sFullPath,
CFile::modeRead|CFile::typeBinary
);
CArchive ar( &file, CArchive::load );
pMyObject->Serialize( ar );
} // try
catch( CException * pException )
{
CString sErrorDescription;
if( !pException->GetErrorMessage(
sErrorDescription.GetBuffer(4096),
4096
)
)
{
sErrorDescription.ReleaseBuffer();
sErrorDescription = _T("no error description");
}
else
sErrorDescription.ReleaseBuffer();
pException->Delete();
CString sMsg;
sMsg.Format(
_T("Failed to load CObject data:\n\n%s\n"),
(LPCTSTR)sErrorDescription
);
MessageBox(
sMsg,
_T("Error"),
MB_OK|MB_ICONERROR
);
} // catch( CException * pException )
catch( ... )
{
MessageBox(
_T("Failed to load CObject data (unknown error)"),
_T("Error"),
MB_OK|MB_ICONERROR
);
} // catch( ... )
}

//
// function to save any CObject to file
// with exception handling
//
void SaveAnyCObjectToFile( CObject * pMyObject )
{
ASSERT_VALID( pMyObject );
CFileDialog dlg(
FALSE,
g_lpszDefExtForFileDialogs,
g_lpszDefSaveFileName,
OFN_ENABLESIZING|OFN_EXPLORER|OFN_PATHMUSTEXIST
|OFN_HIDEREADONLY
|OFN_OVERWRITEPROMPT
|OFN_LONGNAMES
,
g_lpszFilterForFileDialogs,
this
);
dlg.m_ofn.lpstrTitle = _T("Save any CObject to file ...");
if( dlg.DoModal() != IDOK )
return;
CString sFullPath = dlg.GetPathName();
try
{
CFile file(
sFullPath,
CFile::modeWrite|CFile::typeBinary|CFile::modeCreate
);
CArchive ar( &file, CArchive::store );
pMyObject->Serialize( ar );
} // try
catch( CException * pException )
{
CString sErrorDescription;
if( !pException->GetErrorMessage(
sErrorDescription.GetBuffer(4096),
4096
)
)
{
sErrorDescription.ReleaseBuffer();
sErrorDescription = _T("no error description");
}
else
sErrorDescription.ReleaseBuffer();
pException->Delete();
CString sMsg;
sMsg.Format(
_T("Failed to save CObject data:\n\n%s\n"),
(LPCTSTR)sErrorDescription
);
MessageBox(
sMsg,
_T("Error"),
MB_OK|MB_ICONERROR
);
} // catch( CException * pException )
catch( ... )
{
MessageBox(
_T("Failed to save CObject data (unknown error)"),
_T("Error"),
MB_OK|MB_ICONERROR
);
} // catch( ... )
}

//===================================================
//===================================================

// helper function
static CString productsection2regkeypath(
LPCTSTR sProfileName, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct%
LPCTSTR sSectionNameCompany, // under HKEY_CURRENT_USER \ Software
LPCTSTR sSectionNameProduct // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany%
)
{
ASSERT( sProfileName != NULL );
ASSERT( sSectionNameCompany != NULL );
ASSERT( sSectionNameProduct != NULL );
CString s;
s.Format(
_T("Software\\%s\\%s\\%s"),
sSectionNameCompany,
sSectionNameProduct,
sProfileName
);
s.Replace(’’\r’’,’’_’’);
s.Replace(’’\t’’,’’_’’);
s.Replace(’’\n’’,’’_’’);
s.Replace(’’?’’,’’_’’);
s.Replace(’’*’’,’’_’’);
s.Replace(’’.’’,’’_’’);
s.Replace(’’,’’,’’_’’);
return s;
}

// helper function
static bool fileobj_to_registry(
CFile & _file,
LPCTSTR sProfileName, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct%
LPCTSTR sSectionNameCompany, // under HKEY_CURRENT_USER \ Software
LPCTSTR sSectionNameProduct // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany%
)
{
ASSERT( sProfileName != NULL );
ASSERT( sSectionNameCompany != NULL );
ASSERT( sSectionNameProduct != NULL );
CString sRegKeyPath =
productsection2regkeypath(
sProfileName,
sSectionNameCompany,
sSectionNameProduct
);

return CExtCmdManager::FileObjToRegistry( _file, sRegKeyPath );
}

// helper function
static bool fileobj_from_registry(
CFile & _file,
LPCTSTR sProfileName, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct%
LPCTSTR sSectionNameCompany, // under HKEY_CURRENT_USER \ Software
LPCTSTR sSectionNameProduct // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany%
)
{
ASSERT( sProfileName != NULL );
ASSERT( sSectionNameCompany != NULL );
ASSERT( sSectionNameProduct != NULL );
ASSERT( _file.GetLength() == 0 );
CString sRegKeyPath =
productsection2regkeypath(
sProfileName,
sSectionNameCompany,
sSectionNameProduct
);
return CExtCmdManager::FileObjFromRegistry( _file, sRegKeyPath );
}

//
// function to load/save any CObject from/to registry folder
// with exception handling
//
//
bool AnyCObjectRegistrySerialization(
CObject * pMyObject,
LPCTSTR sProfileName, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct%
LPCTSTR sSectionNameCompany, // under HKEY_CURRENT_USER \ Software
LPCTSTR sSectionNameProduct, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany%
bool bSave // true - save, false - load
)
{
ASSERT_VALID( pMyObject );
ASSERT( sProfileName != NULL );
ASSERT( sSectionNameCompany != NULL );
ASSERT( sSectionNameProduct != NULL );
bool bRetVal = false;
try
{
CMemFile _file;
if( bSave )
{
{ // BLOCK: CArchive usage
CArchive ar(
&_file,
CArchive::store
);
pMyObject->Serialize( ar );
ar.Flush();
} // BLOCK: CArchive usage

// ... write _file to registty
_file.Seek(0,CFile::begin);
if( !fileobj_to_registry(
_file,
sProfileName,
sSectionNameCompany,
sSectionNameProduct
)
)
return false;

} // if( bSave )
else
{
// ... read _file from registty
if( !fileobj_from_registry(
_file,
sProfileName,
sSectionNameCompany,
sSectionNameProduct
)
)
return false;
_file.Seek(0,CFile::begin);

CArchive ar(
&_file,
CArchive::load
);
pMyObject->Serialize( ar );
} // else from if( bSave )

bRetVal = true;
} // try
catch( CException * pXept )
{
pXept->Delete();
ASSERT( FALSE );
} // catch( CException * pXept )
catch( ... )
{
ASSERT( FALSE );
} // catch( ... )
return bRetVal;
}

//===================================================
//===================================================


Sergiy Lavrynenko Nov 26, 2002 - 1:39 AM

//
// function to save any CObject to file
// with exception handling
//
void SaveAnyCObjectToFile( CObject * pMyObject )
{
ASSERT_VALID( pMyObject );
CFileDialog dlg(
FALSE,
g_lpszDefExtForFileDialogs,
g_lpszDefSaveFileName,
OFN_ENABLESIZING|OFN_EXPLORER|OFN_PATHMUSTEXIST
|OFN_HIDEREADONLY
|OFN_OVERWRITEPROMPT
|OFN_LONGNAMES
,
g_lpszFilterForFileDialogs,
this
);
dlg.m_ofn.lpstrTitle = _T("Save any CObject to file ...");
if( dlg.DoModal() != IDOK )
return;
CString sFullPath = dlg.GetPathName();
try
{
CFile file(
sFullPath,
CFile::modeWrite|CFile::typeBinary|CFile::modeCreate
);
CArchive ar( &file, CArchive::store );
pMyObject->Serialize( ar );
} // try
catch( CException * pException )
{
CString sErrorDescription;
if( !pException->GetErrorMessage(
sErrorDescription.GetBuffer(4096),
4096
)
)
{
sErrorDescription.ReleaseBuffer();
sErrorDescription = _T("no error description");
}
else
sErrorDescription.ReleaseBuffer();
pException->Delete();
CString sMsg;
sMsg.Format(
_T("Failed to save CObject data:\n\n%s\n"),
(LPCTSTR)sErrorDescription
);
MessageBox(
sMsg,
_T("Error"),
MB_OK|MB_ICONERROR
);
} // catch( CException * pException )
catch( ... )
{
MessageBox(
_T("Failed to save CObject data (unknown error)"),
_T("Error"),
MB_OK|MB_ICONERROR
);
} // catch( ... )
}

Sergiy Lavrynenko Nov 26, 2002 - 1:40 AM

//===================================================
//===================================================

// helper function
static CString productsection2regkeypath(
LPCTSTR sProfileName, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct%
LPCTSTR sSectionNameCompany, // under HKEY_CURRENT_USER \ Software
LPCTSTR sSectionNameProduct // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany%
)
{
ASSERT( sProfileName != NULL );
ASSERT( sSectionNameCompany != NULL );
ASSERT( sSectionNameProduct != NULL );
CString s;
s.Format(
_T("Software\\%s\\%s\\%s"),
sSectionNameCompany,
sSectionNameProduct,
sProfileName
);
s.Replace(’\r’,’_’);
s.Replace(’\t’,’_’);
s.Replace(’\n’,’_’);
s.Replace(’?’,’_’);
s.Replace(’*’,’_’);
s.Replace(’.’,’_’);
s.Replace(’,’,’_’);
return s;
}

// helper function
static bool fileobj_to_registry(
CFile & _file,
LPCTSTR sProfileName, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct%
LPCTSTR sSectionNameCompany, // under HKEY_CURRENT_USER \ Software
LPCTSTR sSectionNameProduct // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany%
)
{
ASSERT( sProfileName != NULL );
ASSERT( sSectionNameCompany != NULL );
ASSERT( sSectionNameProduct != NULL );
CString sRegKeyPath =
productsection2regkeypath(
sProfileName,
sSectionNameCompany,
sSectionNameProduct
);

return CExtCmdManager::FileObjToRegistry( _file, sRegKeyPath );
}

// helper function
static bool fileobj_from_registry(
CFile & _file,
LPCTSTR sProfileName, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct%
LPCTSTR sSectionNameCompany, // under HKEY_CURRENT_USER \ Software
LPCTSTR sSectionNameProduct // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany%
)
{
ASSERT( sProfileName != NULL );
ASSERT( sSectionNameCompany != NULL );
ASSERT( sSectionNameProduct != NULL );
ASSERT( _file.GetLength() == 0 );
CString sRegKeyPath =
productsection2regkeypath(
sProfileName,
sSectionNameCompany,
sSectionNameProduct
);
return CExtCmdManager::FileObjFromRegistry( _file, sRegKeyPath );
}

Sergiy Lavrynenko Nov 26, 2002 - 1:41 AM

// function to load/save any CObject from/to registry folder
// with exception handling
//
bool AnyCObjectRegistrySerialization(
CObject * pMyObject,
LPCTSTR sProfileName, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany% \ %sSectionNameProduct%
LPCTSTR sSectionNameCompany, // under HKEY_CURRENT_USER \ Software
LPCTSTR sSectionNameProduct, // under HKEY_CURRENT_USER \ Software \ %sSectionNameCompany%
bool bSave // true - save, false - load
)
{
ASSERT_VALID( pMyObject );
ASSERT( sProfileName != NULL );
ASSERT( sSectionNameCompany != NULL );
ASSERT( sSectionNameProduct != NULL );
bool bRetVal = false;
try
{
CMemFile _file;
if( bSave )
{
{ // BLOCK: CArchive usage
CArchive ar(
&_file,
CArchive::store
);
pMyObject->Serialize( ar );
ar.Flush();
} // BLOCK: CArchive usage

// ... write _file to registty
_file.Seek(0,CFile::begin);
if( !fileobj_to_registry(
_file,
sProfileName,
sSectionNameCompany,
sSectionNameProduct
)
)
return false;

} // if( bSave )
else
{
// ... read _file from registty
if( !fileobj_from_registry(
_file,
sProfileName,
sSectionNameCompany,
sSectionNameProduct
)
)
return false;
_file.Seek(0,CFile::begin);

CArchive ar(
&_file,
CArchive::load
);
pMyObject->Serialize( ar );
} // else from if( bSave )

bRetVal = true;
} // try
catch( CException * pXept )
{
pXept->Delete();
ASSERT( FALSE );
} // catch( CException * pXept )
catch( ... )
{
ASSERT( FALSE );
} // catch( ... )
return bRetVal;
}