Solved

Serialization

Posted on 2001-09-02
6
653 Views
Last Modified: 2013-11-20
Hi,
I use CMemFile and CArchive
First store in ar and save buffer from file in Registry key
What I store is CObArray
The object is:
class Sites
{
private:
CString first,second;
public:
CString GetFirst();
CString GetSecong();
...
}
Here is my OnCreate() where I whant to read stored data and init CListBox

const INT nSIZE = 4096;
HKEY hKey;
DWORD dwType;
DWORD dwData  = nSIZE;
BYTE* bData   = (BYTE*)::calloc(nSIZE, sizeof(TCHAR));
ASSERT(bData);

     if(ERROR_SUCCESS !=
   
     ::RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Pltodorov\\ReplCheck\\",0,KEY_ALL_ACCESS,&hKey))
   
    MessageBox("Error Open");



const LONG retValue = ::RegQueryValueEx(hKey, "proba", NULL, &dwType,bData, &dwData);
     
     
if(retValue == ERROR_SUCCESS && REG_BINARY == dwType)
     {

  // Load Read values in file
  CMemFile file(bData, nSIZE);
 
  //Attach file to archive
  CArchive ar(&file, CArchive::load);

  //Load "object"
  object.Serialize(ar);

  ar.Close();
  file.Close();

     }

// Loop and load elements to List box
     if(object.GetSize())
     {
     
     for(int i=0;i < object.GetSize();i++)
          {
     
          Sites*     pSite=(Sites*)object[i];
               
          CRuntimeClass*     pRt=pSite->GetRuntimeClass();
         
          pRt->m_nObjectSize;


          if(pSite->IsKindOf(RUNTIME_CLASS(Sites))) MessageBox("It Is");
         
          CListBox* pList=(CListBox*)dlgbar.GetDlgItem(IDC_LIST);
         
          pList->AddString(pSite->GetFirst());
          }
My problem is that pRt->m_nObjectSize;
in not 0 but string stay empty
0
Comment
Question by:plamtod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6448961
what is the full definition of Sites look like?

CObList::Serialize requires that you provide friend operators for << and >> for non CObject classes (eg CString)


friend CArchive& AFXAPI operator<<(CArchive& ar, const Sites& sites)
{
  ar << sites.first;
  ar << sites.second;
}

friend CArchive& AFXAPI operator>>(CArchive& ar, Sites& sites)
{
  ar >> sites.first;
  ar >> sites.second;
}



0
 

Author Comment

by:plamtod
ID: 6448977
class Sites : public CObject  
{
public:
     void SetSecond(CString nSecond);
     CString GetSecond();
     CString first;
     void SetFirst(CString nFirst);
     CString GetFirst();
     Sites();
     virtual ~Sites();

private:
     CString second;
//     CString first;

     DECLARE_SERIAL(Sites)//!!!!!!!
};

It's possible
CObArray object;
object.Serializa(ar);
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6449165
yes but whan you do DECLARE_SERIAL you will see that MFC defines that you require the above >> friend operator (afx.h) - the above operators I mentioned above - should work (Maybe with a few mods) but the essence is there

> It's possible

it is working now ? or it compiles ?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:plamtod
ID: 6449878
I use the idea from
http://camis.kaist.ac.kr/~mkshin/msvc_source/misc/stringlist_to_reg.shtml

Should I predefine <<(>>).
0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 50 total points
ID: 6449967
I see - but if you look in mfc/src/list_s.cpp you will see that CStringList::Serialize(CArchive& ar) understands that it is dealing with CStrings and will therefore serialize them correctly - what I am saying is that MFC does not undestand how to serialize Sites and will need help (from you)
0
 

Author Comment

by:plamtod
ID: 6450037
Thanks a lot.
I wasn't overload Serialize for Sites class
Now O.K.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question