Boost logo

Boost Users :

Subject: [Boost-users] [Boost Serialization] Problem serializing derived class declared in DLL
From: kmdarkmaster (kmdarkmaster_at_[hidden])
Date: 2011-04-05 15:08:03


Dear Boost community,

My problem is simple : I need to serialize a derived class (named Plugin) of
a c++ class (named Base) in my main program.

The derived class Plugin is declared in a DLL which is load explicitely
during execution.

I tried to use the macro BOOST_CLASS_EXPORT_GUID (as in the doc), this works
perfectly if the derived class is in the main execution, but will give me an
exception if the derived class is in the plugin DLL.

Forgive me if the problem is too obvious, this is my first time using Boost
and I'm still a big noob in c++.

Here is my code :

IN THE MAIN EXECUTION :

Class Base (parent class) :

Base.h :
#pragma once;
#include "boost\serialization\access.hpp"
#include <boost\serialization\nvp.hpp>

class Base
{
private:
    friend class boost::serialization::access;
    template
    void serialize(Archive & ar, const unsigned int version){}
public:
        int a, b;
        Base();
        virtual ~Base()
        {
        }
        virtual void func()= 0; // this is an abstract base class
}

Base.cpp:

#include "StdAfx.h"
#include "Base.h"

Base::Base(){
        a=0;
        b=0;
}

IN THE DLL (loaded explicitely)

Class Plugin (Child Class) :

Plugin.h:

#pragma once
#include "boost\serialization\access.hpp"
#include <boost\serialization\nvp.hpp>
#include "boost\serialization\export.hpp"
#include "Base.h"

class Plugin: public Base
{
private:

    friend class boost::serialization::access;
    template
    void serialize(Archive & ar, const unsigned int version){
                ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
                ar & BOOST_SERIALIZATION_NVP(a);
                ar & BOOST_SERIALIZATION_NVP(b);
     }

public:
        virtual void func();
};

BOOST_CLASS_EXPORT_GUID(Plugin, "Plugin")

Plugin.cpp:

#include "StdAfx.h"
#include "Plugin.h"

void Plugin::func(){
        a= 5;
        b= 6;
}

THE MAIN FUNCTION (IN THE MAIN EXECUTION):

.......................................

        HINSTANCE hinst= LoadLibrary(L"monDLLdynamique.dll");
        if (hinst)
        {
                CreatePlugin ctor= (CreatePlugin)GetProcAddress(hinst, "CreatePlugin");
                Base *test= ctor();

                        std::ofstream ofs3("FichierPointeursDerivedDLL.xml");
                        {
                                boost::archive::xml_oarchive oa(ofs3);
                                oa << BOOST_SERIALIZATION_NVP(test);
                        }
                        ofs3.close();

                delete test; // calls Plugin's dtor

                FreeLibrary(hinst);
        }

............................................

Any help will be very very appreciated !!!
Thanks

--
View this message in context: http://boost.2283326.n4.nabble.com/Boost-Serialization-Problem-serializing-derived-class-declared-in-DLL-tp3428913p3428913.html
Sent from the Boost - Users mailing list archive at Nabble.com.

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