Solved

Serialization

Posted on 2001-09-02
6
647 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand LInux Boot partition remotly 3 90
Named range not carried over 10 63
Complete beginner needs help making a cron job 9 116
Problem to event 3 92
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now