Solved

Serialization

Posted on 2001-09-02
6
642 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

15 Experts available now in Live!

Get 1:1 Help Now