|
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
|
|
|
|
|
Ok,
I put a breakpoint inside the MS version of SerializeElements and it hit. So as suspected my version is not being used.
I guess I don't really understand how to override SerializeElements so the code uses my version. What are the rules?
The CList is still required to use .Serialize() of course.
Thanks
modified on Wednesday, September 23, 2009 4:38 PM
|
|
|
|
|
Ok,
Need to review Templates. You have to make the template explicit by adding the class name before (.
|
|
|
|
|
Is it possible to make the shell notify you when the user opens a folder and eventually prevent it? I am trying to build a folder guard app. Notification objects does not seem to cover this functionality. The ICopyHook interface also seems like not the solution. Any suggestions will be highly appreciated.
Martin
|
|
|
|
|
Have you read through Michael Dunn's Shell-related articles?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
See this article[^] by Raymond Chen? Although not exactly the same situation, the advice is still applicable.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Yes, just use a SW hook (classic code, Win32)
|
|
|
|
|
Hi Gurus,
I have a simple application, which pops a message box if it is invoked in a pre-logon mode (i.e. when the system displays the login screen).
How do I get to know if the application is invoked before the user is logged on??
I tried using
GetProcessWindowStation() - but this doesn't help.
NOTE: The application will be invoked from a service.
Thanks in advance.
Sunil
|
|
|
|
|
How about GetUserName() or NetWkstaGetInfo(102) or <code>NetWkstaUserEnum() ?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hello,
I have written automation to start excel charts but the createdispatch doesnt start excel
_Application app;
if(!app.CreateDispatch(CreateDispatch("Excel.Application")
{
AfxMessageBox(
"Couldn't start Excel and get an application 0bject");
return;
}
wht can be the problem
Prithaa
|
|
|
|
|
prithaa wrote: if(!app.CreateDispatch(CreateDispatch("Excel.Application")
This is both syntactically wrong and just plain weird looking. How about:
COleException e;
if (! app.CreateDispatch("Excel.Application", &e))
... prithaa wrote: wht can be the problem
Did you remember to call AfxOleInit() ?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hello
Oh sorry CreateDispatch was written twice
I wrote
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't start Excel and get an application 0bject");
return;
}
The above is not working
Prithaa
|
|
|
|
|
prithaa wrote: The above is not working
Why not? Are you looking at the exception object (specificaly it's getter method) like I suggested?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hello
Yes still excel doesnt start
COleException e;
if(!app.CreateDispatch("Excel.Application",&e))
{
AfxMessageBox("Couldn't start Excel and get an application 0bject");
return;
}
i wrote the above
|
|
|
|
|
Did you somehow think that simply creating the object would make it all better? What's the "value" of e ?
What type of object is app ?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
it is like this
_Application app;
e is not initialised
|
|
|
|
|
Ok, and how did you go about creating the _Application class? In other words, how did you import it from Excel's type library? See the latter part of this article for one way.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I have include the type library through class wisard automation
and
#include "excel.h"
on top of the file
|
|
|
|
|
i need not write the import command since i have already included through
automation type library
isnt it
|
|
|
|
|
Correct. There are two ways of getting access to Excel's COM interface.
If you look at the COleException object when CreateDispatch() fails, does it hint that the COM library has not been initialized?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
In the watch window the value of e doesnt change even after the function is called
|
|
|
|
|
prithaa wrote: In the watch window the value of e doesnt change
It's created on the stack so why would it change? You have to call it's GetErrorMessage() method.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
When i call the GetErrorMessage() the string is blank
LPTSTR f;
e.GetErrorMessage(f,0);
f is blank
|
|
|
|
|
Of course it is! You've not only given it an invalid buffer, you said the buffer's size is 0.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
when i give 50 or 100 it gives me debug assertion failed
|
|
|
|