Click here to Skip to main content
15,886,963 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
QuestionShell notification when opening a folder Pin
mkoroudjiev23-Sep-09 6:15
mkoroudjiev23-Sep-09 6:15 
QuestionRe: Shell notification when opening a folder Pin
David Crow23-Sep-09 6:37
David Crow23-Sep-09 6:37 
AnswerRe: Shell notification when opening a folder Pin
Stuart Dootson23-Sep-09 11:53
professionalStuart Dootson23-Sep-09 11:53 
AnswerRe: Shell notification when opening a folder Pin
kilt24-Sep-09 7:05
kilt24-Sep-09 7:05 
QuestionHow to determine the desktop type?? Pin
Sunil P V23-Sep-09 3:09
Sunil P V23-Sep-09 3:09 
QuestionRe: How to determine the desktop type?? Pin
David Crow23-Sep-09 3:47
David Crow23-Sep-09 3:47 
Questionexcel automation not starting Pin
prithaa23-Sep-09 3:03
prithaa23-Sep-09 3:03 
QuestionRe: excel automation not starting Pin
David Crow23-Sep-09 3:52
David Crow23-Sep-09 3:52 
AnswerRe: excel automation not starting Pin
prithaa23-Sep-09 4:01
prithaa23-Sep-09 4:01 
QuestionRe: excel automation not starting Pin
David Crow23-Sep-09 4:03
David Crow23-Sep-09 4:03 
AnswerRe: excel automation not starting Pin
prithaa23-Sep-09 4:09
prithaa23-Sep-09 4:09 
QuestionRe: excel automation not starting Pin
David Crow23-Sep-09 4:14
David Crow23-Sep-09 4:14 
AnswerRe: excel automation not starting Pin
prithaa23-Sep-09 4:18
prithaa23-Sep-09 4:18 
QuestionRe: excel automation not starting Pin
David Crow23-Sep-09 4:22
David Crow23-Sep-09 4:22 
AnswerRe: excel automation not starting Pin
prithaa23-Sep-09 4:28
prithaa23-Sep-09 4:28 

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.