- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
很多天以来,我一直在尝试解决派生嵌套类的 dll 序列化问题。我已经尝试了很多我发现的例子(即 export_keys ),但我总是遇到运行时错误。访问序列化函数存在一些问题。我写了一个简化的类,类似于我们根据 boost serialization tests (polymorphic_derived2) 使用的类.
//#############dll ###################
//PlugInClass_Derived_2.h//
#pragma once
#ifdef PLUGIN
// All functions in this file are exported
#else
// All functions in this file are imported
#define PLUGIN __declspec(dllimport)
#endif
#include "PlugInClass_Derived_1.h"
class PlugInClass_Derived_2 : public PlugInClass_Derived_1
{
public:
class DLL_DECL(BOOST_PP_EMPTY()) Parameter : public PlugInClass_Derived_1::Parameter
{
public:
Parameter(void);
virtual ~Parameter(void){};
private:
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version);
public:
virtual const char * get_key() const {
return "PlugInClass_Derived_2_Parameter";
}
};
public:
/** @brief standard constructor */
PlugInClass_Derived_2(){};
virtual ~PlugInClass_Derived_2(){};
PLUGIN virtual boost::shared_ptr<PlugInClass_Base::Parameter> getInitParameter();
};
extern "C" PLUGIN PlugInClass_Base* Create(/*void** params, const int number*/);
extern "C" PLUGIN void Destroy(PlugInClass_Base* p);
// we use this because we want to assign a key to this type
// but we don't want to explicitly instantiate code every time
// we do so!!!. If we don't do this, we end up with the same
// code in BOTH the DLL which implements polymorphic_derived2
// as well as the main program.
BOOST_CLASS_EXPORT_KEY(PlugInClass_Derived_2::Parameter)
// note the mixing of type_info systems is supported.
BOOST_CLASS_TYPE_INFO(
PlugInClass_Derived_2::Parameter,
boost::serialization::extended_type_info_typeid<PlugInClass_Derived_2::Parameter>
)
#undef DLL_DECL
//PlugInClass_Derived_2.cpp
/* boost headers go here */
/* plugin headers go here */
#define POLYMORPHIC_DERIVED2_EXPORT
#include "PlugInClass_Derived_2.h"
PlugInClass_Derived_2::Parameter::Parameter(){
d = -2.;
/*nothing*/
}
template <typename Archive>
void PlugInClass_Derived_2::Parameter::serialize(Archive &ar, const unsigned int version)
{
ar & boost::serialization::base_object<PlugInClass_Derived_1::Parameter>(*this);
}
boost::shared_ptr<PlugInClass_Base::Parameter> PlugInClass_Derived_2::getInitParameter(){
// get local data pointer;
boost::shared_ptr<PlugInClass_Base::Parameter> localParameter(new PlugInClass_Derived_2::Parameter());
// update parent information
// return local data
return localParameter;
}
PlugInClass_Base* Create(/*void** params, const int number*/) {
return new PlugInClass_Derived_2();
}
void Destroy(PlugInClass_Base* p) {
delete p;
}
// instantiate code for text archives
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
template EXPORT_DECL(void) PlugInClass_Derived_2::Parameter::serialize(
boost::archive::text_oarchive & ar,
const unsigned int version
);
template EXPORT_DECL(void) PlugInClass_Derived_2::Parameter::serialize(
boost::archive::text_iarchive & ar,
const unsigned int version
);
// instantiate code for polymorphic archives
#include <boost/archive/polymorphic_iarchive.hpp>
#include <boost/archive/polymorphic_oarchive.hpp>
template EXPORT_DECL(void) PlugInClass_Derived_2::Parameter::serialize(
boost::archive::polymorphic_oarchive & ar,
const unsigned int version
);
template EXPORT_DECL(void) PlugInClass_Derived_2::Parameter::serialize(
boost::archive::polymorphic_iarchive & ar,
const unsigned int version
);
// MWerks users can do this to make their code work
BOOST_SERIALIZATION_MWERKS_BASE_AND_DERIVED(
PlugInClass_Derived_1::Parameter
, PlugInClass_Derived_2::Parameter)
// note: export has to be AFTER #includes for all archive classes
BOOST_CLASS_EXPORT_IMPLEMENT(PlugInClass_Derived_2::Parameter)
//PlugInClass_Derived_1.h
#pragma once
#ifdef PLUGIN
// All functions in this file are exported
#else
// All functions in this file are imported
#define PLUGIN __declspec(dllimport)
#endif
#include "PlugInClass_Base.h"
class PlugInClass_Derived_1 : public PlugInClass_Base
{
public:
class DLL_DECL(BOOST_PP_EMPTY()) Parameter : public PlugInClass_Base::Parameter
{
public:
int a, b, c;
Parameter(void);
virtual ~Parameter(void){};
private:
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version);
public:
virtual const char * get_key() const {
return "PlugInClass_Derived_1_Parameter";
}
};
public:
/** @brief standard constructor */
PlugInClass_Derived_1(){};
virtual ~PlugInClass_Derived_1(){};
virtual boost::shared_ptr<PlugInClass_Base::Parameter> getInitParameter();
};
// we use this because we want to assign a key to this type
// but we don't want to explicitly instantiate code every time
// we do so!!!. If we don't do this, we end up with the same
// code in BOTH the DLL which implements polymorphic_derived2
// as well as the main program.
BOOST_CLASS_EXPORT_KEY(PlugInClass_Derived_1::Parameter)
// note the mixing of type_info systems is supported.
BOOST_CLASS_TYPE_INFO(
PlugInClass_Derived_1::Parameter,
boost::serialization::extended_type_info_typeid<PlugInClass_Derived_1::Parameter>
)
//PlugInClass_Derived_1.cpp
/* boost headers go here */
/* plugin headers go here */
#define POLYMORPHIC_DERIVED2_EXPORT
#include "PlugInClass_Derived_1.h"
PlugInClass_Derived_1::Parameter::Parameter():
a(1),
b(2),
c(3)
{
d = -1.;
/*nothing*/
}
template <typename Archive>
void PlugInClass_Derived_1::Parameter::serialize(Archive &ar, const unsigned int version)
{
ar & boost::serialization::base_object<PlugInClass_Base::Parameter>(*this);
ar & a;
ar & b;
ar & c;
}
boost::shared_ptr<PlugInClass_Base::Parameter> PlugInClass_Derived_1::getInitParameter(){
// get local data pointer;
boost::shared_ptr<PlugInClass_Base::Parameter> localParameter(new PlugInClass_Derived_1::Parameter());
// update parent information
// return local data
return localParameter;
}
// instantiate code for text archives
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
template EXPORT_DECL(void) PlugInClass_Derived_1::Parameter::serialize(
boost::archive::text_oarchive & ar,
const unsigned int version
);
template EXPORT_DECL(void) PlugInClass_Derived_1::Parameter::serialize(
boost::archive::text_iarchive & ar,
const unsigned int version
);
// instantiate code for polymorphic archives
#include <boost/archive/polymorphic_iarchive.hpp>
#include <boost/archive/polymorphic_oarchive.hpp>
template EXPORT_DECL(void) PlugInClass_Derived_1::Parameter::serialize(
boost::archive::polymorphic_oarchive & ar,
const unsigned int version
);
template EXPORT_DECL(void) PlugInClass_Derived_1::Parameter::serialize(
boost::archive::polymorphic_iarchive & ar,
const unsigned int version
);
// MWerks users can do this to make their code work
BOOST_SERIALIZATION_MWERKS_BASE_AND_DERIVED(
PlugInClass_Base::Parameter
, PlugInClass_Derived_1::Parameter)
// note: export has to be AFTER #includes for all archive classes
BOOST_CLASS_EXPORT_IMPLEMENT(PlugInClass_Derived_1::Parameter)
//PlugInClass_Base.h
#pragma once
#ifdef PLUGIN
// All functions in this file are exported
#else
// All functions in this file are imported
#define PLUGIN __declspec(dllimport)
#endif
// boost headers
#include <boost/serialization/serialization.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/assume_abstract.hpp>
#include <boost/serialization/tracking.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/type_info_implementation.hpp>
#include <boost/serialization/extended_type_info_no_rtti.hpp>
#include <boost/serialization/extended_type_info_typeid.hpp>
#include <boost/preprocessor/empty.hpp>
// export_decl, import_decl from http://www.boost.org/doc/libs/1_55_0/libs/serialization/test/
#ifdef BOOST_HAS_DECLSPEC // defined in config system
#if ! defined(EXPORT_DECL)
#if defined(__BORLANDC__)
#define EXPORT_DECL(T) T __export
#else
#define EXPORT_DECL(T) __declspec(dllexport) T
#endif
#endif
#if ! defined(IMPORT_DECL)
#if defined(__BORLANDC__)
#define IMPORT_DECL(T) T __import
#else
#define IMPORT_DECL(T) __declspec(dllimport) T
#endif
#endif
#else
#define IMPORT_DECL(T) T
#define EXPORT_DECL(T) T
#endif // BOOST_HAS_DECLSPEC
#if defined(POLYMORPHIC_DERIVED_IMPORT)
#define DLL_DECL IMPORT_DECL
#elif defined(POLYMORPHIC_DERIVED_EXPORT)
#define DLL_DECL EXPORT_DECL
#else
#define DLL_DECL(x)
#endif
class PlugInClass_Base
{
public:
class DLL_DECL(BOOST_PP_EMPTY()) Parameter
{
public:
double d;
virtual ~Parameter(void){};
private:
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version){
ar & d;
};
public:
virtual const char * get_key() const = 0;
};
public:
/** @brief standard constructor */
PlugInClass_Base(){};
virtual ~PlugInClass_Base(){};
PLUGIN virtual boost::shared_ptr<PlugInClass_Base::Parameter> getInitParameter(){return NULL;};
};
BOOST_SERIALIZATION_ASSUME_ABSTRACT(PlugInClass_Base::Parameter)
// we use this because we want to assign a key to this type
// but we don't want to explicitly instantiate code every time
// we do so!!!. If we don't do this, we end up with the same
// code in BOTH the DLL which implements polymorphic_derived2
// as well as the main program.
BOOST_CLASS_EXPORT_KEY(PlugInClass_Base::Parameter)
// note the mixing of type_info systems is supported.
BOOST_CLASS_TYPE_INFO(
PlugInClass_Base::Parameter,
boost::serialization::extended_type_info_typeid<PlugInClass_Base::Parameter>
)
///######### dll 结束 ###################///
//至少是主要的
#include <stdio.h>
#include <tchar.h>
#include <fstream>
#include <sstream>
#include <boost/serialization/serialization.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/assume_abstract.hpp>
#include <boost/serialization/tracking.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/type_info_implementation.hpp>
#include <boost/serialization/extended_type_info_no_rtti.hpp>
#include "PlugIn_Derived\PlugInClass_Base.h"
#include <windows.h>
int main(int argc, char** argv)
{
boost::shared_ptr<PlugInClass_Base> dll_;
/// ######### load dll ################### ///
std::string pluginPath_ = "PlugInClass_Derived_2.dll";
HINSTANCE currLibrary = LoadLibrary(pluginPath_.c_str());
if (!currLibrary) {
throw std::runtime_error("The plugin could not be loaded.");
}
// load the plugin class interface from the library using its factory functions
typedef PlugInClass_Base *(*CreatePluginFuncDef)();
CreatePluginFuncDef pluginCreateFactoryPtr = (CreatePluginFuncDef)GetProcAddress(currLibrary, "Create");
typedef void(*DestroyPluginFuncDef)(PlugInClass_Base*);
DestroyPluginFuncDef pluginDestroyPtr = (DestroyPluginFuncDef)GetProcAddress(currLibrary, "Destroy");
// create object within dll
dll_ = boost::shared_ptr<PlugInClass_Base>(pluginCreateFactoryPtr(), pluginDestroyPtr);
/// ######### get object from dll ################### ///
boost::shared_ptr<PlugInClass_Base::Parameter> parameterPtr = dll_->getInitParameter();
/// ######### serialization ################### ///
std::string s;
// create and open a character archive for output
{
std::ostringstream os;
// save data to archive
boost::archive::text_oarchive oa(os);
// write class instance to archive
oa << parameterPtr; // HERE RUNTIME ERROR
// Unhandled exception at xxxx in test_dll_simple_example.exe:
// Microsoft C++ exception: boost::archive::archive_exception at memory location xxxx
s = os.str();
}
// ... some time later restore the class instance to its orginal state
std::system("pause");
return 0;
}
如您所见,我想要的类始终是一个嵌套类“Parameter”,定义在“dll”接口(interface) (PlugInClass_Base) 的派生类中,并派生自基类的参数类(即 PlugInClass_Derived_1::Parameter)。 dll 函数“getInitParameter”返回指向我们要存储的参数类的基类的基指针。
你能看出我在连载过程中做错了什么吗?如果你能给我一些提示,我会很高兴。
亚历山德罗斯。
最佳答案
我发现,如果将基指针转换为派生指针,则序列化有效。
/// ######### get object from dll ################### ///
boost::shared_ptr<PlugInClass_Base::Parameter> parameterPtr = dll_->getInitParameter();
boost::shared_ptr<PlugInClass_Derived_2::Parameter> derivedParameterPtr = boost::static_pointer_cast<PlugInClass_Derived_2::Parameter>(parameterPtr);
/// ######### serialization ################### ///
std::string s;
// create and open a character archive for output
{
std::ostringstream os;
// save data to archive
boost::archive::text_oarchive oa(os);
oa << derivedParameterPtr; // It works !!!
s = os.str();
}
因此,序列化无法通过基指针中的所有派生指针。基类是一个抽象类
(BOOST_SERIALIZATION_ASSUME_ABSTRACT(PlugInClass_Base::Parameter))
并且 key 被导出
BOOST_CLASS_EXPORT_KEY(PlugInClass_Base::Parameter)
并且导出每个派生类的实现(在cpp文件中)
BOOST_CLASS_EXPORT_IMPLEMENT(PlugInClass_Derived_1::Parameter)
嵌套类的键有问题吗?否则所有派生类都有自己的 get_key() 函数。
有什么假设吗?
关于c++ - 在 dll 中定义的派生嵌套类的序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26218507/
我有一个 Windows Service(一个发布版本),我替换了一个在 Debug模式下构建的 DLL 并尝试启动服务。它引发了错误无法加载文件或程序集“名称”或其依赖项之一。尝试加载格式不正确的程
我使用 Microsoft Visual Basic 6.0 Enterprise Edition SP6 开发了一个 VB6 应用程序。 我已将可执行文件复制到未安装 VB6 的计算机 C。 我已在
我有很多小的 DLL,我想将它们组合成一个大的(更)DLL(如 suggested here)。我可以通过合并我的项目来做到这一点,但我想要一种不那么干扰的方式。 可以将多个 DLL 合并为一个单元吗
我想将非托管 DLL 和图像文件与托管 DLL 合并。我该怎么做?可能吗? 最佳答案 很有可能。这可以通过 bxilmerge 来完成此解决方案可以将非托管 DLL 和图像或其他文件与托管 DLL 合
如何在Windows Server 2003上安装msvcr71.dll,这是我的软件所需的。我真的不想将此dll复制到system32文件夹,因为它可能会破坏此目标系统。 最佳答案 只需将其复制到程
我最近遇到了一个安装在我的系统上的 DLL,Dependancy Walker(以及我尝试过的所有其他实用程序)说按名称或顺序导出为零,但文件大小约为 4mb。我认为 DLL 的唯一目的是导出供其他代
我终于让我的本地主机在本地显示该站点。一切似乎都在朝着这个方向努力。我的下一步是当网站使用 ActiveX.dll 中的函数时,实际上能够从 VB6 IDE 进入代码 更新: 我更新了代码并删除了我在
首先,我指的是Windows环境和VC++编译器。 我想要做的是重建一个Vc++ dll并与已经链接到该lib的exe保持兼容性,而不必重建该exe或使用LoadLibrary动态加载该dll。换句话
我以前这样做过,我不记得我是从网上下载的 DLL 还是其他东西,但我不想感染病毒。我需要访问这个命名空间,以便我可以拥有 Webbrowswer 控件不提供的额外功能。 如何准确添加 Com 引用。还
我正在尝试为依赖于 ghostscript 的库创建 Nuget 包,因此引用 gsdll32.dll - 一个非托管库。我不能只包含一个标准的 dll 引用。我应该把它放在 nuget 目录结构中的
如果我有 Windows 可执行文件,我如何找出它将加载哪些 dll? 我只是谈论哪些将静态加载,而不是那些可能使用 LoadLibrary 等动态加载的内容。 最佳答案 dumpbin是VC++自带
这可能是一个非常菜鸟的问题,但在当今的网络应用程序开发世界中,许多程序员不需要过多处理 dll,因此也懒得去了解它们的用途。 那么什么是dll? 它有什么用? 它是如何工作的? 如何创建一个? 在什么
所以我刚刚开始使用 OpenTK,并将此代码放在一个继承 GameWindow 类的类中: protected override void OnRenderFrame(FrameEventArgs e
如何调试未由 java 应用程序加载的 dll。 场景是这样的:我的 java 应用正在加载正在使用另一个 dll 的 jni.dll,而那个 dll 正在使用另一个 dll。 javajni.dll
我将“更好”放在引号中,因为这是一个定性问题。几年来我一直在编写 COM DLL,直到最近才发现并成功使用了带有 Typelib 概念的标准 DLL。 使用 COM DLL 代替 DLL+Typeli
这种用户机器可能没有msvcp100.dll、msvcp100.dll之类dll的情况怎么处理?我不希望我的软件因为这种错误而无法安装在用户的机器上。我一直在考虑要么找到一个工具并将每个需要的 dll
在 VS2012 中使用 C# .NET 和 COM 互操作,我正在开发一个用于其他几个程序的公共(public)库。为了简化集成,我想将整个库缩减为一个 DLL。该库的特点之一是本地化。它有包含多种
我在 .Net 中使用 FieldTalk Modbus。当我运行应用程序时,我在为 MbusTcpMasterProtocol 创建对象时遇到错误。 MbusTcpMasterProtocol mb
我想指出,我知道如何向/从 GAC 添加/删除程序集。我要问的是,是否有人可以从技术角度向我解释它是如何工作的。将 dll 放在那里有什么意义 - 从节省资源的角度来看。 亲切的问候 最佳答案 将东西
我继承了一个传统的经典 ASP 应用程序,它使用 VB6 ActiveX DLL 来执行业务逻辑。 我想跟踪加载和卸载 DLL 的点。有没有办法在 VB6 DLL 中拦截这些事件? 在相关说明中,Cl
我是一名优秀的程序员,十分优秀!