使用Poco实现插件方式加载动态库

时间:2022-12-07 05:31:40

动态库封装虚基类

//AbstractPlugin.h
#ifndef __ABSTRACTPLUGIN_H__
#define __ABSTRACTPLUGIN_H__

#include <string>

#ifdef WIN32
#ifdef ABSTRACT_PLUGIN
#define ABSTRACT_PLUGIN_LIB __declspec(dllexport)
#else
#define ABSTRACT_PLUGIN_LIB __declspec(dllimport)
#endif
#else
#define ABSTRACT_PLUGIN
#endif

class ABSTRACT_PLUGIN_LIB AbstractPlugin
{

public:
AbstractPlugin();
virtual ~AbstractPlugin();
virtual std::string name() const = 0;
};
#endif

//AbstractPlugin.cpp
#include "AbstractPlugin.h"

AbstractPlugin::AbstractPlugin()
{
}

AbstractPlugin::~AbstractPlugin()
{
}

动态库实现,需要实现虚基类中的所有虚函数

//PluginA.h
#ifndef __PLUGIN_A_H__
#define __PLUGIN_A_H__

#include "AbstractPlugin.h"
#include <string>

class PluginA : public AbstractPlugin
{
public:
PluginA();
~PluginA();
std::string name() const;
};
#endif

//PluginA.cpp
#include "PluginA.h"
#include "Poco/ClassLibrary.h"
#include <iostream>

PluginA::PluginA()
{

}

PluginA::~PluginA()
{

}

std::string PluginA::name() const
{
return "This is PluginA";
}

//用POCO提供的宏来生成类清单
//这个宏展开实际是个函数声明,该函数由POCO在加载dll时自动调用,完成清单的加载
POCO_BEGIN_MANIFEST(AbstractPlugin)
POCO_EXPORT_CLASS(PluginA)
POCO_END_MANIFEST

// optional set up and clean up functions
void pocoInitializeLibrary()
{
std::cout << "PluginLibrary initializing" << std::endl;
}

void pocoUninitializeLibrary()
{
std::cout << "PluginLibrary uninitializing" << std::endl;
}

主程序

//main.cpp
#include "Poco/ClassLoader.h"
#include "Poco/Manifest.h"
#include "AbstractPlugin.h"

#include <iostream>
typedef Poco::ClassLoader<AbstractPlugin> PluginLoader;
typedef Poco::Manifest<AbstractPlugin> PluginManifest;

#include <io.h>
#include <string>
#include <vector>
//扫描指定文件夹下的文件(待加载动态库)
void getFiles(std::string iPath, std::vector<std::string> &oFileList)
{
intptr_t hFile = 0; //file handle
struct _finddata_t fileinfo; //file info struct
std::string p;

if ((hFile = _findfirst(p.assign(iPath).append("/*").c_str(), &fileinfo)) != -1)
{
do {
if ((fileinfo.attrib & _A_SUBDIR))
{
}
else
{
oFileList.push_back(fileinfo.name);
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}

int main(int argc, char** argv)
{
PluginLoader loader;
std::vector<std::string> pluginFileList;
std::vector<AbstractPlugin*> pluginObjVec;
//扫描待加载动态库
getFiles("E:\\my_plugin_path", pluginFileList);
//逐一加载
for (int i = 0; i < pluginFileList.size(); i++)
{
loader.loadLibrary("E:\\my_plugin_path" + pluginFileList[i]);
}

PluginLoader::Iterator it = loader.begin();
for (; it != loader.end(); ++it)
{
//输出该dll路径
printf("Plugin library path: %s\n", it->first.c_str());
PluginManifest::Iterator itMan(it->second->begin());
int classCount = 0;
//遍历该dll的类清单
for (; itMan != it->second->end(); ++itMan)
{
std::string className = itMan->name();
//导出类计数,本例只支持导出1个类
classCount++;
if (classCount > 1)
{
printf("OnlySupport one exported class.\n");
exit(-1);
}
//创建对象
AbstractPlugin* pluginTmp = loader.create(className);
//将对象所有权交给loader 当loader析构时会自动释放
loader.classFor(className).autoDelete(pluginTmp);
pluginObjVec.push_back(pluginTmp);
}
}
for (int i = 0; i< pluginObjVec.size(); i++)
{
std::cout << pluginObjVec[i]->name() << std::endl; ///调用插件类中定义的函数///
}
//逐一释放加载的动态库
for (int i = 0; i < pluginFileList.size(); i++)
{
loader.unloadLibrary("E:\\my_plugin_path" + pluginFileList[i]);
}
return 0;
}

需要加载 PocoFoundation.lib 库。