日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發(fā)布時間:2025/5/22 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Poco实现插件方式加载动态库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

動態(tài)庫封裝虛基類

//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 #endifclass ABSTRACT_PLUGIN_LIB AbstractPlugin { public:AbstractPlugin();virtual ~AbstractPlugin();virtual std::string name() const = 0; }; #endif//AbstractPlugin.cpp #include "AbstractPlugin.h"AbstractPlugin::AbstractPlugin() { }AbstractPlugin::~AbstractPlugin() { }

動態(tài)庫實現,需要實現虛基類中的所有虛函數

//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> //掃描指定文件夾下的文件(待加載動態(tài)庫) void getFiles(std::string iPath, std::vector<std::string> &oFileList) {intptr_t hFile = 0; //file handlestruct _finddata_t fileinfo; //file info structstd::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;//掃描待加載動態(tài)庫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);}//創(chuàng)建對象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; ///調用插件類中定義的函數///}//逐一釋放加載的動態(tài)庫for (int i = 0; i < pluginFileList.size(); i++){loader.unloadLibrary("E:\\my_plugin_path" + pluginFileList[i]);}return 0; }

需要加載 PocoFoundation.lib 庫。

總結

以上是生活随笔為你收集整理的使用Poco实现插件方式加载动态库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。