Read \ write boost :: binary_oarchive to the channel
I keep building two simple processes, throwing class objects to each other (see my previous post) through simple (anonymous) pipes. Now I showed myself boost :: serialization (thanks people answered) and tried to make some class serialized via :: WriteFile :: ReadFile. So - what am I 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 a binary archive and write its contents to one end of the pipe ...
boost::archive::binary_oarchive oa(ofs);
oa << tc;
::WriteFile(hPipe, &oa, dwRead, &dwWritten, NULL) == FALSE
It won't work, right? So how is it going to be?
3) The same action on the other side?
source to share
I think the problem is that you are trying to pass a pointer to an archive object in the WriteFile function. Instead, you must point to a pointer to the 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 a binary_oarchive constructor with an ostream implementation that writes directly to the file descriptor.
source to share
Assuming you are passing the correct value for dwRead, I think the problem is that the stream has not been flushed. Make sure you create binary_oarchive inside the block, so that when it goes out of scope, its destructor clears up 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);
source to share