views:

647

answers:

2

Hello. I am continue to build two simple processes throwing class objects one to another (see my previous post) through simple (anonymous) pipes. Now I revealed for myself boost::serialization (thanks answered people) and have tried to make some class be serialized through ::WriteFile\::ReadFile. So - what I am doing wrong?


1) I created some class

 #pragma once
 #include "wtypes.h"
 #include <boost\archive\binary_oarchive.hpp>
 #include <boost\archive\binary_iarchive.hpp>
 #include <boost\serialization\binary_object.hpp>

 class CTextContainer
 {
 friend class boost::serialization::access;
     template<class Archive>
     void serialize(Archive & ar, const unsigned int version)
     {
         ar & m_sText;
         ar & m_dwCRC;
         ar & m_dwSize;
     }

 public:
      CTextContainer() : m_dwCRC(0), m_dwSize(0)
      {
          ZeroMemory(m_sText, sizeof(m_sText));
          m_dwSize = sizeof(*this);
      }
      CTextContainer(LPCTSTR sText) : m_dwCRC(0), m_dwSize(0)
      {
         ZeroMemory(m_sText, sizeof(m_sText));
         wcsncpy_s(m_sText, 1024, sText, wcslen(sText));
         m_dwSize = sizeof(*this);
      }
      virtual ~CTextContainer(){}
      LPTSTR GetText() const{return (LPTSTR) m_sText;}
      protected:
      DWORD m_dwCRC;
      DWORD m_dwSize;
      TCHAR m_sText[1024];
 }; //end of class

2) And now I am trying to read from this class into binary archive and to write its content to one end of pipe...

boost::archive::binary_oarchive oa(ofs);
oa << tc;
::WriteFile(hPipe, &oa, dwRead, &dwWritten, NULL) == FALSE

It won't work in that way, right? So, how it will?

3) Same operation on other side?

A: 

Assuming you're passing a correct value for dwRead, I think the problem is that the stream hasn't been flushed. Make sure you create the binary_oarchive inside a block, so that when it goes out of scope, its destructor flushes the stream.

std::ofstream ofs("filename");
{
    boost:archive::binary_oarchive oa(ofs);
    oa << tc;
}

// Set up your pipe and assign a value to dwRead
// ...

::WriteFile(hPipe, &oa, dwRead, &dwWritten, NULL);
Tony
+1  A: 

I think the problem here is that you're trying pass a pointer the the archive object in the WriteFile function. What you should do instead is provide a pointer to serialized data.

std::stringstream ss;
boost::archive::binary_oarchive oa(ss);
oa << tc;
::WriteFile(hPipe, ss.str().data(), ss.str().data().size(), &dwWritten, NULL)

As a better alternative you should provide the binary_oarchive constructor with an ostream implementation that writes directly into your file handle.

hvintus