Solved

Serialization

Posted on 2001-09-02
6
648 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 126
how to monitor remote shell execution on linux 9 104
VBA "SendKeys" Syntax for Multiple Keystrokes 7 117
Way to decrease size of apk file 9 86
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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