Boost logo

Boost Users :

Subject: [Boost-users] Serializing derived class in dll
From: Christian Bergstrand (christian_at_[hidden])
Date: 2009-06-12 15:54:58


I am working on a programwith pluggable classes delivered in DLLs, so I
have an virtual abstract base class in my EXE and both abstract and
concrete classes in a hiearchy in my first DLL. I am trying ot serialize
the derived class instances through a base class pointer. I am not able
to get it to work after reading the samples in the documentation and the
most likely looking threads here.

So I broke it down in to a sample EXE/DLL and can still not get it to work.

I am using version 1_38 currently, also tried with 1_36_0

I am getting the exeption on oserializer.hpp:374 where the comment says
            // note:if this exception is thrown, be sure that derived
pointer
            // is either registered or exported.

In my opinion I am exporting my derived class, but I am feeling that it
is probably going into the wrong instance, since the export code is
going into the DLL, but I can not find any other recomendation.

So what am I missing ...

------------------------
serializedll\base.h
------------------------
#pragma once

class Base
{
public:
    virtual void f() = 0;
private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
    }
};
BOOST_SERIALIZATION_ASSUME_ABSTRACT( Base )

------------------------
serializedll\serializedll.cpp, in the EXE
------------------------
#include <tchar.h>
#include <windows.h>

#include <fstream>

#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/nvp.hpp>

#include "base.h"

int _tmain(int argc, _TCHAR* argv[])
{
    typedef Base * ( GetObjFun )();
    GetObjFun * f = (GetObjFun*)GetProcAddress( LoadLibrary( TEXT(
"plugin.dll" ) ), "getobj" );
    Base * obj = f();

    std::ofstream ofs( "c:\\test.xml" );
    boost::archive::text_oarchive oa(ofs);

    oa << obj;

    return 0;
}

------------------------
plugin\plugin.cpp, in the DLL
------------------------
#include <windows.h>

#include <boost/serialization/export.hpp>
#include <boost/archive/text_oarchive.hpp>

#include "../serializedll/base.h"

BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID
lpReserved )
{
    return TRUE;
}

class Derived : public Base
{
public:
    virtual void f()
    {
    }
private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & boost::serialization::base_object< Base >( *this );
    }
};

BOOST_CLASS_EXPORT( Derived )

Base * getobj()
{
    return new Derived();
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net