Click here to Skip to main content
15,894,539 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Memory mapped file I/O Vs Disk I/O Pin
Stuart Dootson24-Sep-09 6:23
professionalStuart Dootson24-Sep-09 6:23 
Questiona bother about windows service Pin
jinjiashan23-Sep-09 16:16
jinjiashan23-Sep-09 16:16 
AnswerRe: a bother about windows service Pin
jinjiashan23-Sep-09 21:50
jinjiashan23-Sep-09 21:50 
GeneralRe: a bother about windows service Pin
jinjiashan24-Sep-09 0:04
jinjiashan24-Sep-09 0:04 
AnswerRe: a bother about windows service Pin
Rolf Kristensen26-Sep-09 7:49
Rolf Kristensen26-Sep-09 7:49 
GeneralRe: a bother about windows service Pin
jinjiashan27-Sep-09 13:29
jinjiashan27-Sep-09 13:29 
QuestionProblem with initializing Main args! Pin
Largo6523-Sep-09 12:58
Largo6523-Sep-09 12:58 
AnswerRe: Problem with initializing Main args! Pin
Game-point23-Sep-09 18:40
Game-point23-Sep-09 18:40 
AnswerRe: Problem with initializing Main args! Pin
Saurabh.Garg23-Sep-09 18:56
Saurabh.Garg23-Sep-09 18:56 
AnswerRe: Problem with initializing Main args! Pin
«_Superman_»23-Sep-09 19:23
professional«_Superman_»23-Sep-09 19:23 
AnswerRe: Problem with initializing Main args! Pin
Richard MacCutchan23-Sep-09 22:32
mveRichard MacCutchan23-Sep-09 22:32 
QuestionC++ with in-code Assembly Pin
paolosh23-Sep-09 10:22
paolosh23-Sep-09 10:22 
AnswerRe: C++ with in-code Assembly Pin
ied23-Sep-09 10:28
ied23-Sep-09 10:28 
AnswerRe: C++ with in-code Assembly Pin
Rick York23-Sep-09 10:37
mveRick York23-Sep-09 10:37 
QuestionLoadImage fails to locate file Pin
raich23-Sep-09 8:32
raich23-Sep-09 8:32 
QuestionRe: LoadImage fails to locate file Pin
David Crow23-Sep-09 9:03
David Crow23-Sep-09 9:03 
AnswerRe: LoadImage fails to locate file Pin
raich23-Sep-09 9:42
raich23-Sep-09 9:42 
QuestionRe: LoadImage fails to locate file Pin
David Crow23-Sep-09 10:00
David Crow23-Sep-09 10:00 
AnswerRe: LoadImage fails to locate file Pin
norish23-Sep-09 21:43
norish23-Sep-09 21:43 
AnswerRe: LoadImage fails to locate file Pin
ied23-Sep-09 9:56
ied23-Sep-09 9:56 
GeneralRe: LoadImage fails to locate file Pin
raich23-Sep-09 10:02
raich23-Sep-09 10:02 
AnswerRe: LoadImage fails to locate file Pin
CPallini23-Sep-09 10:02
mveCPallini23-Sep-09 10:02 
QuestionOverriding void AFXAPI SerializeElements [modified] Pin
al250023-Sep-09 7:22
al250023-Sep-09 7:22 
Hi all,

As you probably know, there is a problem with CString types in CList, CMap or CArray with regards to serialization. I have attempted to remedy this by overriding void AFXAPI SerializeElements. Before I post my code though, I'd like to ask if any other MFC container class can serialize CStrings correctly, for example CTypedPtrList? I'm not real particular on the container class for my current app and figuring out the peculiarities of serialize is exhausting.

The problem with the code below is that even after overriding SerializeElements, I get a mem copy error when doing a serialize load operation. A breakpoint in SerializeElements is never hit so for some reason it's not being used. I also have a serialize() function for the base class elements but it's not used either, presumably because all base elements are CSTRINGs? There are 2 classes, one for the base elements and another with the CList and functions. BTW, the functions that save and load the list are called from inside the Doc class serialize function.

Thanks in advance -

///////.h file

class CChartInfo : public CObject
{

DECLARE_SERIAL(CChartInfo)
public:

CString m_ChartName;
CString m_Portfolio;
CString m_DataPath;
CString m_DataFile;



CChartInfo(const CChartInfo &s)
{m_ChartName = s.m_ChartName;
m_Portfolio = s.m_Portfolio;
m_DataPath = s.m_DataPath;
m_DataFile = s.m_DataFile;}

CChartInfo& operator=(const CChartInfo &s)
{m_ChartName = s.m_ChartName;
m_Portfolio = s.m_Portfolio;
m_DataPath = s.m_DataPath;
m_DataFile = s.m_DataFile;
return *this;}

public:
void Serialize(CArchive& ar);

public:
CChartInfo();
virtual ~CChartInfo();
};

class CChartManager : public CObject
{
DECLARE_SERIAL(CChartManager)

public:

CList<cchartinfo, cchartinfo&=""> m_ChartList;



void AddChart(CChartInfo ChartInfo);
bool GetChart(CString ChartName, CChartInfo &ChartInfo);
bool LoadChartList();
bool SaveChartList();


CChartManager();
virtual ~CChartManager();
};


void AFXAPI SerializeElements(CArchive& ar,CChartInfo* pChartInfo, int nCount);

////////////////////////////////////////.cpp file


IMPLEMENT_SERIAL(CChartInfo, CObject, VERSION_NUMBER)
IMPLEMENT_SERIAL(CChartManager, CObject, VERSION_NUMBER)


CChartInfo::CChartInfo()
{
}

CChartInfo::~CChartInfo()
{
}

void CChartInfo::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
if (ar.IsStoring()){
ar << m_ChartName
<< m_Portfolio
<< m_DataPath
<< m_DataFile;
}
else{
ar >> m_ChartName
>> m_Portfolio
>> m_DataPath
>> m_DataFile;
}
}

CChartManager::CChartManager()
{
}

CChartManager::~CChartManager()
{
}


// CChartManager member functions


bool CChartManager::SaveChartList()
{

//assumes m_ChartList has been loaded with correct data

char acPath[256];
if ( GetModuleFileName( NULL, acPath, 256 ) != 0) {
// guaranteed file name of at least one character after path
* ( strrchr( acPath, '\\' ) + 1 ) = '\0';
//AfxMessageBox( acPath ); // Use it
}
strcat(acPath, "ChartManager.dat");

CFile ChartManagerFile;
CFileException Err;
if (ChartManagerFile.Open(acPath, CFile::modeCreate | CFile::modeWrite, &Err))
{
CArchive archive(&ChartManagerFile, CArchive::store);
m_ChartList.Serialize(archive);
archive.Close();
ChartManagerFile.Close();

return 1;

}
else {
Err.ReportError();//for debug only
return 0;
}

}

bool CChartManager::LoadChartList()
{

//read chartlist file and load data into list
//if file not found returns false

m_ChartList.RemoveAll();

char acPath[256];
if ( GetModuleFileName( NULL, acPath, 256 ) != 0) {
// guaranteed file name of at least one character after path
* ( strrchr( acPath, '\\' ) + 1 ) = '\0';
//AfxMessageBox( acPath ); // Use it
}
strcat(acPath, "ChartManager.dat");

CFile ChartManagerFile;
CFileException Err;
if (ChartManagerFile.Open(acPath, CFile::modeRead, &Err))
{
CArchive archive(&ChartManagerFile, CArchive::load);
m_ChartList.Serialize(archive);
archive.Close();
ChartManagerFile.Close();
return 1;

}
else {
Err.ReportError();//for debug only
return 0;
}



}


void AFXAPI SerializeElements(CArchive& ar, CChartInfo* pChartInfo, int nCount)
{
for(int i=0;nCount;i++,pChartInfo++)
{
pChartInfo->Serialize(ar);
}
}

modified on Wednesday, September 23, 2009 3:09 PM

AnswerRe: Overriding void AFXAPI SerializeElements [modified] Pin
al250023-Sep-09 10:24
al250023-Sep-09 10:24 
GeneralRe: Overriding void AFXAPI SerializeElements Pin
al250023-Sep-09 12:47
al250023-Sep-09 12:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.